44834621 Vb Net Tutorials
-
Upload
jagrati-shrivastava -
Category
Documents
-
view
227 -
download
0
Transcript of 44834621 Vb Net Tutorials
-
8/2/2019 44834621 Vb Net Tutorials
1/51
Launch your Visual Basic .NET or Visual Studio software. When the software firstloads, you'll see a screen something like this one:
There's a lot happening on the start page. But basically, this is where you can start a
new project, or open an existing one. The first Tab, Projects, is selected. At themoment, the area labelled "Open an Existing Project" is blank. This is what you'll see
when you run the software for the first time (because you haven't created a projectyet). When you create a project, the Name you gave it will be displayed on this
page, as a hyperlink. Clicking the link will open the project.
At the bottom of the screen, there are two buttons: "New Project" and "OpenProject". To get started, click the "New Project" button. When you do, you'll see this
dialogue box appear:
-
8/2/2019 44834621 Vb Net Tutorials
2/51
Give Your Project Name and Location. Click on ok
-
8/2/2019 44834621 Vb Net Tutorials
3/51
In the Visual Basic NET design time environment, the first thing to concentrate on is
that strange, big square in the top left. That's called a form. It's actually the prettybit of your programme, the part that others will see when they launch your
masterpiece. Granted, it doesn't look too attractive at the moment, but you'll soon
discover ways to lick it into shape.
To run the form, try this:
From the menu bar, click Debug
From the drop down menu, click Start Alternatively, press the F5 key on your keyboard
Your programme is launched
Congratulations! You have now created your very first programme. It should look likethis:
-
8/2/2019 44834621 Vb Net Tutorials
4/51
Click the Red X on the form to stop it from running. You will then be returned to thesoftware environment.
If you compare the first form with the one above, you'll see that they look verysimilar. But the one above is actually a real programme, something you could
package and sell to unsuspecting village idiots
So what's going on? Why the two different views? Well, Visual Basic has two distinct
environments, a Design environment and a Debug environment. Design Time iswhere you get to play about with the form, spruce it up, add textboxes, and buttons,
and labels (and code, of course ); Debug is where you can test your programme andsee how well it performs. Or doesn't perform, as is usually the case.
But don't worry about the terminology, for the time being. Just be aware that there'sa two step process to VB programming: designing and debugging.
So, let's get on and do some designing! Before we can start designing a form,
though, we need some tools. And where are tools kept? In a toolbox!
-
8/2/2019 44834621 Vb Net Tutorials
5/51
Things like buttons, textboxes, and labels are all things that you can add to your Forms. They aand are kept in the Toolbox for ease of use.
The Toolbox can be found on the left of the screen. In the picture below, you can see the toolbo
Form1:
To display all the tools, move your mouse over the toolbox icon. You'll see the following automa
There are seven categories of tools available. The toolbox you'll be working with first is the Com
toolbox. To see the tools, click on the plus symbol next to Common Controls. You'll see a long
-
8/2/2019 44834621 Vb Net Tutorials
6/51
As you can see, there are an awful lot of tools to choose from! For this first section, we'll only bethe TextBox and the Label.
If you want to keep the toolbox displayed, click the Pin icon next to the X. To close the toolbox, mouse away.
Let's start by adding a textbox to our form. With the tools displayed, do thefollowing:
Locate the TextBox tool
Double click the icon
A textbox is added to your form
-
8/2/2019 44834621 Vb Net Tutorials
7/51
The textbox gets added to the top left position of your form. To move it down, holdyour mouse over the textbox and drag to a new position:
Notice the small squares around the textbox. These are sizing handles. Move yourmouse over one of them. The mouse pointer turns into an extended line with
arrowheads. Hold your left mouse button down and drag outwards. The textbox isresized. Play around with the sizing handles until you're happy with the size of your
textbox.
One thing you will notice is that you can't make the size any higher, but you can
make it wider. The reason why you can't make it any higher is because the defaultaction of a textbox is to have it contain only a single line of text. If it's only going to
contain one line of text, Microsoft reasoned, there's no reason why you should beable to change its height. A textbox can only be made higher if it's set to contain
multiple lines of text. You'll see how to do this soon.
Create two more textboxes by double clicking on the textbox icon inthe toolbar (Or Right-click on the selected textbox and choose Copy. Then
Right-click on the Form and choose Paste.)
Resize them to the same size as your first one
Line them up one below the other with space in between
Try to create something that looks like the one below
To see what your Form looks like as a programme, click Debug > Start from the
menu bar. Or press F5 on your keyboard:
-
8/2/2019 44834621 Vb Net Tutorials
8/51
To stop the programme from running, you can do one of the following:
1. Click the Red X at the top right of your Form
2. Click Debug > Stop Debugging from the menu bar
3. Press Shift + F5 on your keyboard
You can also click the Stop button on the VB toolbars at the top, as in the imagebelow
-
8/2/2019 44834621 Vb Net Tutorials
9/51
You've probably noticed the area to the right of the design environment, the areawith all the textboxes in a grid, the one that has those daunting names like
"AccessibleDescription", "AccessibleName", "AccessibleRole". That's the Propertiesbox.
Click anywhere on the form that is not a label or a textbox, somewhere on the form'sgrey areas. The form should have the little sizing handles now, indicating that the
form is selected.
On the right of the design environment there should be the following Properties box:
-
8/2/2019 44834621 Vb Net Tutorials
10/51
If your Properties box says "Textbox1 Textbox" or "Label1 Label" then you haven'tyet selected the Form. Click away from the textbox or label until the Properties box
reads "Form1 Form"
What you are looking at is a list of the properties that a form has: Name , BackColor,Font, Image, Text, etc. Just to the right of these properties are the values for them.
These values are the default values, and can be changed. We're going to change thevalue of the Text property.
First, you might want to display the list of Properties in a more accessible form. You
can display the list properties alphabetically. To do that, click the Alphabetic icon atthe top of the Properties box, as in the image below:
This will make the properties easier to find.
Before we change any in the Properties box, let's clear up what we mean by
"Property".
No more reading these lessons online - get the eBook here!
What is a Property?
Those controls you added to the form (textboxes and labels), and the form itself, are
called control objects. You can think of controls as things, something solid that youcan pick up and move about. Controls (things) have properties. If your television
http://www.homeandlearn.co.uk/bookshop/net.htmlhttp://www.homeandlearn.co.uk/bookshop/net.html -
8/2/2019 44834621 Vb Net Tutorials
11/51
were a control, it too would have properties: an On/Off button property, a colourproperty, a volume property, and a ... well, what other properties would your
television have? Think about it.
The properties of your television will have values. The On/Off button would have justtwo values - On or Off. The volume property could have a range of values, from zero
to ten, for example. If the value of the volume property was set to ten, the loudestvalue, then you'd probably have some very angry neighbours!
In VB.NET, you can change a property of a control from the Properties Box. (You can
also change a property using code, which you'll do quite a lot.) If we go back to ourForm object, and the properties and values it has, we can see how to change them
using the Properties Box. We'll change only one of these values for now - the valueof the Text property . So, do this:
Locate the word "Text" in the Property box, as in the image below
"Text" is a Property of Form1. Don't be confused by the word "Form1" next to theword "Text". All this means is that the current value of the Text property is set to the
word "Form1". This is the default.To change this to something of your own, do this:
Click inside the area next to "Text", and delete the word "Form1" by
hitting the backspace key on your keyboard
When "Form1" has been deleted, type the words "My First Form"
-
8/2/2019 44834621 Vb Net Tutorials
12/51
Click back on the form itself (the one with the labels and textboxes),
or hit the return key on your keyboard
The words "My First Form" will appear as white text on a bluebackground at the top of the form
If you have a look in the top right of the Design Environment, you'll see the SolutionExplorer. (If you can't see it, click View > Solution Explorer.)
The Solution Explorer shows you all the files you have in your project (Notice that
the name of your project is at the top of the tree - "My First Project").
At first glance, it looks as though there are not many files in the project. But click theShow All Files icon, circled below:
When you click Show All Files, the Solution Explorer will look something like this:
-
8/2/2019 44834621 Vb Net Tutorials
13/51
When you save your project, you are saving all these files.
To save your work, click File > Save All and you'll see the following dialogue box
(we've chopped ours down a bit):
The files are usually saved in the My Document folder in XP (Document folder in
Vista), under Visual Studio. If you want to save your projects elsewhere, click theBrowse button.
To actually save your work as you go along, just click File > Save All from the menubar. Or press Ctrl + Shift + S on your keyboard. Or click the icon in the Toolbar (the
stack of floppy disks). If you save often then you won't lose any of your work ifanything goes wrong with your computer.
VARIABLES:
Why are we discussing variables? And what is a variable?
With Visual Basic, and most programming languages, what you are doing is storing
things in the computer's memory, and manipulating this store. If you want to addtwo numbers together, you put the numbers into storage areas and "tell" Visual
Basic to add them up. But you can't do this without variables.
-
8/2/2019 44834621 Vb Net Tutorials
14/51
So a variable is a storage area of the computer's memory. Think of it like this: avariable is an empty cardboard box. Now, imagine you have a very large room, and
in this room you have a whole lot of empty cardboard boxes. Each empty cardboardbox is a single variable. To add two numbers together, write the first number on a
piece of paper and put the piece of paper into an empty box. Write the secondnumber on a piece of paper and put this second piece of paper in a different
cardboard box.
Now, out of all your thousands of empty cardboard boxes two of them contain pieces
of paper with numbers on them. To help you remember which of the thousands ofboxes hold your numbers, put a sticky label on each of the two boxes. Write
"number1" on the first sticky label, and "number2" on the second label.
What have we just done? Well, we've created a large memory area (the room and
the cardboard boxes), and we've set up two of the boxes to hold our numbers (twovariables). We've also given each of these variables a name (the sticky labels) so
that we can remember where they are.
Now examine this:
Dim number1 As Integer
Dim number 2 As Integer
number1 = 3number2 = 5
That's code from Visual Basic Net. It's VB's way of setting up (or declaring) variables.
ADDING A BUTTON AND UNDERSTANDING EVENTS:
Instead of double clicking the Button tool in the toolbox to add the control to theform, we'll explore another way to do it.
With your Form displayed in the Visual Basic Design environment, do the following:
Click on the Button tool in the toolbox with the left hand mouse
button, but click only once
Move your mouse to a blank area of your form - the mouse pointer willturn into a cross
Press and hold down the left mouse button Drag across the form with the button held down
Let go of the mouse button when you're happy with the size
A Button is drawn
You can use the above method to draw most of the controls onto the form - labels,
Buttons, textboxes, etc.
-
8/2/2019 44834621 Vb Net Tutorials
15/51
The Button control, just like all the other controls we've seen so far, has a list ofproperties. One of these properties is the Text property. At the moment, your button
will say "Button 1". You can change that to anything you like.
Click on the Button to highlight it
Click on Text in the Property Box
Click in the box next to the word "Text" Delete the word "Button 1"
Type "Add two numbers"
Click back on the Form
Now add a Textbox to your form using one of the methods outlined (either double-
click, or draw).
Your Form should now look something like this:
The Font property of the Button has also been changed, here, in exactly the sameway as we changed the Font property of the Label and Textbox previously. The Text
for the Textbox control has had its default Text (Textbox 1) deleted.
To get our first look at the code window, double click your Button control. The code
window will appear, and will look like this:
-
8/2/2019 44834621 Vb Net Tutorials
16/51
Notice that we've used the underscore character ( _ ) to spread the code over morethan one line. You can do this in your own code, too, if it becomes to long. But you
don't have to.
Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) _
Handles Button1.Click
End Sub
The part of the code we're interested in is highlighted in red in the code above.
Notice, too, that the underscore character ( _ ) has been used to spread the codeover more than one line. You can do this in your own code, too, if it becomes to
long:
Private
Private means that no other part of the programme can see this codeexcept for our button
SubShort for Subroutine. The "Sub" word tells VB that some code follows, and
that it needs to be executed
Button1This is the name of our button. You might think that we've just erased the
word "Button1" when we changed the Text property, so why does VB insist
that it's still called Button1? We'll, the Name property of the control is theimportant one. If you change the Name property, VB will change this
button name for you_Click ( )
This is something called an Event. In other words, when the button isclicked, the Click Event will fire, and the code we're going to write will be
executedEnd Sub
Click your mouse on the blank line after Private Sub Button1_Click, etc, butbefore End Sub. Type the following code:
Dim number1 As IntegerDim number 2 As Integer
Dim answer As Integer
number1 = 3
number2 = 5
answer = number1 + number2
MsgBox answer
After typing all that, your code window should now look like this:
-
8/2/2019 44834621 Vb Net Tutorials
17/51
Before we explore what's happening here, save your work and then click Debug >Start from the Visual Basic Menu, or press F5 on your keyboard. This will launch
your programme. Click the Button once, and you should get the following:
Stop your programming, and return to the Design Environment. If you can't see yourcode, you can click the Tabs at the top of the Window, as in the image below:
Click the "Form1.vb [Design]" tab to see your Form.
-
8/2/2019 44834621 Vb Net Tutorials
18/51
OK, what happened there? Well, what happened is we've just wrote a programme toadd two numbers together, and we displayed the result by using a Message Box -
you very first real programme! But let's break that code down a little more.
First, we started with the Dim word, indicating to Visual Basic that wewanted to set up a variable
Then we gave the variable a name (number1) Next, we "told" VB that what is going inside the variable is a number
(As Integer)
Two more variable were set up in the same way, number2 and answer
After setting up the three variables, here's what we did:
Told Visual Basic that what is going into the first variable was the
number 3, and what is going into the second variable was the number 5. Toput something into a variable, you use the equals ( = ) sign. But it's not
really an equals sign - it's an assignment operator. You are assigning thevalue of 3 to the variable called number1
number1 = 3number2 = 5
The next part is a little bit more complicated, but not too complicated. What wewanted to do was to add two numbers together. So we said
number1 + number2
Visual Basic already knows how to add up: all we need to do is "tell" it to add up. We
do the "telling" in the traditional, mathematical way - with the plus sign (+). WhatVisual Basic will do is to look at what we've stored inside number1, and look at
what's inside number2. It's sees the 3, sees the five, and also sees the plus sign.Then Visual basic adds them up for you.
Except we also did something else. We said to Visual Basic "When you've finishedadding up the two variables number1 and number2, store the result in that other
variable we set up, which is called answer."
So, the whole line
answer = number1 + number2
means: "Add the variable called number1 to the variable called number2. Then store
the result in the variable called answer."
Think of it as working from the right-hand side of the equals sign first. Then when
you have the answer, assign it the variable on the left of the equals sign.
The final part of the programme used Visual Basic's in-built Message Box. We'll learn
a more about the Message Box later. For now, think of it as a handy way to displayresults.
-
8/2/2019 44834621 Vb Net Tutorials
19/51
Message boxes are quite handy when you want to display the result of some code.But we have a textbox on the form, and we might as well use that.
So delete the line: MsgBox answer. Type the word Textbox1, then type a full stop.
You should see a drop-down box appear. This is a list of the Properties and Methodsthat the Textbox can use.
Scroll down until you see the word "Text". Double click the Text property and the
drop-down box will disappear. (This drop-down box is known as IntelliSense, and isvery handy. It means you can just select a property or method from the list without
having to type anything.)
The Text property you have chosen is the same Text property that you set from theProperties Window earlier. Here, we're setting the property with our code; before, we
set it at design time. But the result is the same - the Text property of the textboxwill be set to a value of our choosing.
To set a value, type an equals sign, then type a value for the Text property. We wantthe contents of the variable called answer to appear in the textbox. So the rest of the
code is just this:
Textbox1.Text = answer
Your code window should then look like this:
-
8/2/2019 44834621 Vb Net Tutorials
20/51
Run your code again, and press the Button on the form. You should see the number
8 appear in the textbox.
String Concatenation:
FullName = FirstName & " " & LastName
& -> used for string concatenation.
Syntax For If:If Condition then
Statement
End if
Ex:
If firstname = Bill Then
MsgBox "firstname is Bill"End If
Syntax for if else:
If Condition then
Statement
Else
StatementEnd if
Example:
-
8/2/2019 44834621 Vb Net Tutorials
21/51
If firstname = "Bill" ThenMsgBox "firstname is Bill"
ElseMsgBox "firstname is not Bill"
End If
Example for Select:
Dim creamcake As StringDim DietState As String
creamcake = TextBox1.Text
Select Case creamcake
Case "Eaten"DietState = "Diet Ruined"
Case "Not Eaten"DietState = "Diet Not Ruined"
Case Else
DietState = "Didn't check"
End Select
Create a new project for this section. Add a button to your new form. Then, locate
the Combo Box on the Visual Basic .NET toolbar. It looks like this:
Double click the icon to add a Combo Box to your form. Or click once with the lefthand mouse button, and then draw one on the form.
A combo box is a way to limit the choices your user will have. When a black down-pointing arrow is clicked, a drop down list of items appears. The user can then select
one of these options. So let's set it up to do that.
-
8/2/2019 44834621 Vb Net Tutorials
22/51
Click on your Combo Box to select it. Then locate the Item propertyfrom the Properties Box:
Click the grey button, as above. The one with the three dots in it.When you do, you'll get the following box popping up:
To use the String Collection Editor, type an item and press Return (it's
just like a normal textbox. Each item will be one item in your drop-downbox.)
Enter five items, as in the image below:
-
8/2/2019 44834621 Vb Net Tutorials
23/51
Then click the OK button at the bottom.
The Editor will close, and it will look like nothing has happened. However, run yourprogramme and test out your new Combo Box. You should have something like this:
Conditional Operators:
The Conditional Operators allow you to refine what you are testing for. Instead of saying "If X is
specify whether it's greater than, less than, and a whole lot more. Examine the list of Operators
Operator Meaning
-
8/2/2019 44834621 Vb Net Tutorials
24/51
> This symbol means Is Greater Than and is used like this:
If number > 10 Then
MsgBox "The Number was Greater Than 10"
End If
< This symbol means Is Less Than and is used like this:
If number = These symbols mean Is Greater Than or Equal to, and are used like
If number >= 10 ThenMsgBox "The Number was 10 or Greater"End If
5 Or number < 15 ThenMsgBox "Greater than 5 Or Less than 15"
End If
-
8/2/2019 44834621 Vb Net Tutorials
25/51
These symbols mean Is Not Equal to, and are used like this:
If number1 number2 Then
MsgBox "number1 is not equal to number2"
End If
Loops:
Dim answer As Integer
Dim startNumber As Integer
answer = 0
For startNumber = 1 To 4
answer = answer + startNumber
Next startNumber
MsgBox answer
Do While:
Dim number as Integer
number = 1
Do While number < 5MsgBox number
number = number + 1Loop
Do Loop:
Do Until number < 5
MsgBox number
number = number + 1
Loop
Menus:
-
8/2/2019 44834621 Vb Net Tutorials
26/51
Start a new project. To your new form, use the toolbox to add a MenuStrip control:
Double click the control to add one to your form. When you do, you'll notice twothings. At the top of your form, you'll see this:
We'll see how to construct our menu soon. But notice the other things that gets
added to your project. Examine the bottom of your screen, on the left. You'll seethis:
This is the control itself. If you click on this (it's highlighted above), you'll see that
the Properties box on the right changes. There are many properties for the control.But there are lots of properties for the MenuItem object. The MenuItem object is the
one at the top of the form - The one that says Type Here.
To start building your menu, click inside the area that says "Type Here". Type the
word File:
-
8/2/2019 44834621 Vb Net Tutorials
27/51
Now press the enter key on your keyboard. Your menu will look like this:
To create items on your File menu, click inside the Type Here box. Enter the wordNew, and press the enter key on your keyboard again. Your menu will then look like
this:
Add an "Open" and a "Save" item to your menu in the same way. It should look like
this:
The final item we'll add to our menu is an "Exit" item. But you can add a separatorbetween the "Save" and "Exit".
To add a separator, click inside the blue "Type Here" box. Instead of typing a letter,type the minus character "-" (in between the "0" key and the "+/=" key on your
keyboard). When you hit your return key, you'll see the separator appear:
-
8/2/2019 44834621 Vb Net Tutorials
28/51
Click inside the "Type Here" area, and add an Exit (or Quit) item. Click back on your
form, away from the menu, to finish off. You should now have a File menu like thisone:
To see what your menu look like, Run your programme. Click the File menu. We
haven't added any code to the menu yet, so nothing will happen if you click an itemon the menu. But it does look quite good. Very professional!
This tutorial assumes that you have completed the first one.
In the previous tutorial, you learned how to add Sub Menus to your VB .NET forms.In this part, you'll see how to add shortcuts to your menu items
Underline Shortcut
To add an underline, do this:
http://www.homeandlearn.co.uk/NET/nets4p1.htmlhttp://www.homeandlearn.co.uk/NET/nets4p3.htmlhttp://www.homeandlearn.co.uk/NET/nets4p1.htmlhttp://www.homeandlearn.co.uk/NET/nets4p3.html -
8/2/2019 44834621 Vb Net Tutorials
29/51
Click on your New menu item once. This will select it
Position your cursor before the "N" ofNew
Type an ampersand symbol (&)
Hit the return key on your keyboard
You should see this:
Notice that "N" ofNew is now underlined. If you want an underline shortcut, theampersand character should be typed before the letter you want underlined.
Add underlines for the "F" of you File menu, the "O" ofOpen, the "S" ofSave, and
the "X" ofExit. When you're done, your menu should look like this one:
Time to see if your shortcut works. Run your programme. To use the underline
shortcuts on menus, you first hold down the Alt key on your keyboard. Then type the
underline character.
OBJECT ORIENTED CONCEPTS
The following paragraphs explain OOPS concepts in VB.NET and C#.NET.
-
8/2/2019 44834621 Vb Net Tutorials
30/51
1) Class:
It is template
It is designed at design time
Ex: paper work (drawing of building plan on paper by civil engineers)
2) Object:
Object can be defined as real entity,
Which occupy some memory and with have life
Ex: constructed building on the basis of paper plan by civil engineers
Class:
Template
No memory
No life
Physical entity
Object:
It has life
It has memory
It design at run time
3) Access keyword in oops (.net)
Access keywords
Access keywords define the access to class members from the same class and from
other classes. The most common access keywords are:
Public: Allows access to the class member from any other class.
Private: Allows access to the class member only in the same class.
Protected: Allows access to the class member only within the same class and from
inherited classes.
-
8/2/2019 44834621 Vb Net Tutorials
31/51
Internal: Allows access to the class member only in the same assembly.
Protected internal: Allows access to the class member only within the same class,
from inherited classes, and other classes in the same assembly.
Static: Indicates that the member can be called without first instantiating the class.
Public
Imports system
Public class class1
Public i as integer
Public function raghu () as integer
Console.writeline (i)
End function
End class
Public class class2
Shared sub main ()
Dim a as sub class1
a.i=10
a.raghu ()
End class
Output=10
Reason: Allows access to the class member from any other class.
Private:
-
8/2/2019 44834621 Vb Net Tutorials
32/51
Imports system
Public class class1
Private i as integerPublic function raghu () as integer
Console.writeline (i)
End function
End class
Public class class2
Shared sub main ()
Dim a as sub class1
a.i=10
a.raghu()
End class
Out put: error
Reason: Allows access to the class member only in the same class.
Conclusion
Public class class1
Private i as integer
Public function raghu () as integer
Console.writeline (i)
End function
Shared sub main()
Dim a as new class1
a.i=10
-
8/2/2019 44834621 Vb Net Tutorials
33/51
a.raghu ()
End sub
End class
Output:10
Protected:
Imports system
Public class class1
Protected i as integer
Public function raghu () as integer
Console.writeline (i)
End function
End class
Public class class2
Shared sub main ()
Dim a as sub class1
a.i=10
a.raghu ()
End class
Output=error
Reason: Allows access to the class member only within the same class and from
inherited classes.
-
8/2/2019 44834621 Vb Net Tutorials
34/51
Conclusion:
Imports systemPublic class class1
Protected i as integer=10
Public function raghu () as integer
Console.writeline (i)
End function
End class
Public class class2
Shared sub main ()
Dim a as sub class1
a.raghu ()
End class
Ouput: 10
Friend:
Imports system
Public class class1
friend i as integer
public sub hello()
Console.writeline (i)
end sub
End class
Public class class2
Shared sub main ()
-
8/2/2019 44834621 Vb Net Tutorials
35/51
Dim a as new class1 ()
a.i=10
a.hello()
End subEnd class
Ouput: 10
Reason: Allows access to the class member only in the same assembly.
Protected friend:
Imports system
Public class class1
protected friend i as integer
public sub hello()
Console.writeline (i)
end sub
End class
Public class class2
Shared sub main ()
Dim a as new class1 ()
a.i=10
a.hello ()
End sub
End class
output:10
-
8/2/2019 44834621 Vb Net Tutorials
36/51
Reason: Allows access to the class member only within the same class, from
inherited classes, and other classes in the same assembly
Static: Indicates that the member can be called without first instantiating the class.
1) Load into memory once (we can save memory)
2) Instance not required
Constructor:
It is a method
It name same as a class (in c#, c++)
In vb.net it is declared as sub new ()
In wont return any value
see for program
Imports system
Public class class1
Private i as integer
Public sub new ()
Console.writeline (i)
End sub
End class
Public class class2
Shared sub main ()
Dim a as new class1
-
8/2/2019 44834621 Vb Net Tutorials
37/51
End sub
End class
Output :0
Reason: default: it will take 0
please see this example
Imports system
Public class class1Private i as integer=10
Public sub new ()
Console.writeline (i)
End sub
End class
Public class class2
Shared sub main ()
Dim a as new class1
End sub
End class
output=10
However our .net support two types of constructor
1) Shared
2) Instant
-
8/2/2019 44834621 Vb Net Tutorials
38/51
Shared:
1) It wont accept any parameter
2) So it cant be overloaded
Instant:
1) It accept parameter
2) So it can be overloaded
Note:
One constructor can be called from other constructor that can be done in vb.net
myclass.new ()
Example:
Imports system
Public class class1
Public sub new ()
Console.writeline ("hai")
End sub
Public sub new (i as integer)
Console.writeline (i)
End sub
End class
Public class class2
Shared sub main ()
Dim a as new class1 (10)
End sub
End class
Output=10
-
8/2/2019 44834621 Vb Net Tutorials
39/51
Reason: because of when passing arguments from object only parameter constructor
Will be called
Note : if u want to display two constructor values
Let us see:
Imports system
Public class class1
Public sub new ()
Console.writeline ("hai")
End sub
Public sub new (i as integer)
Myclass.new ()
Console.writeline (i)
End sub
End class
Public class class2
Shared sub main ()
Dim a as new class1 (10)
End sub
End class
Output:
Hai
10
Let us consider example
-
8/2/2019 44834621 Vb Net Tutorials
40/51
Imports system
Public class class1
Public sub new ()Console.writeline ("hai")
End sub
Public sub new (i as integer)
Myclass.new ()
Console.writeline (i)
End sub
Shared sub new ()
Console.writeline (this is raghu)
End sub
End class
Public class class2
Shared sub main ()
Dim a as new class1 (10)
End sub
End class
Output:
This is raghu
Hai
10
Reason:
Because of shared constructor will be called first when object is created
Overloading of constructor ()
-
8/2/2019 44834621 Vb Net Tutorials
41/51
Imports system
Public class class1
Public sub new (j as string)Console.writeline ("this is raghu)
End sub
Public sub new (i as integer)
Myclass.new (raghu)
Console.writeline (i)
End sub
End class
Public class class2
Shared sub main ()
Dim a as new class1 (10)
End sub
End class
Output:
This is raghu
10
Calling of methods while creating one object with help of constructor:
Imports system
Public class class1
Public sub new ()
Console.writeline ("this is raghu")
-
8/2/2019 44834621 Vb Net Tutorials
42/51
End sub
End class
Public class class2: inherits class1Public sub new ()
Console.writeline ("this is raghu1")
End sub
End class
Public class class3: inherits class2
Public sub new ()
Console.writeline ("this is raghu2")
End sub
End class
Public class class4
Shared sub main ()
Dim a as new class3 ()
End sub
End class
Output:
This is raghu
This is raghu1
This is raghu2
Conclusion: when ever object is created constructor will be called first
class &structures
Class: it will take help of heap memory
-
8/2/2019 44834621 Vb Net Tutorials
43/51
It is reference type
It can inheritable
It can be over loadable
Structures:
It will take help of stack memory
It is value type
It cant be inheritable
It can't be over loadable
Inheritance:
Inheritance in a beautiful concept supported by oops (.net)
Mainly usage of:
1) Reusability
2) Data redundancy
Example:
Imports system
Public class class1
Public sub raghu ()
Console.writeline (this is raghu)
End sub
End class
Public class class2: inherits class1
Public sub raghu1
Console.writeline (this is placid man)
End sub
End class
-
8/2/2019 44834621 Vb Net Tutorials
44/51
Public class class2
Shared sub main ()
Dim a as new class2 ()a.raghu1 ()
a.raghu ()
End sub
End class
Output:
This is raghu
This is placid man
When ever taking about inheritance so many topicsLike:
1) Overriding2) Overload able
3) Dynamic dispatcher
Let us consider below example
When a make super class reference is pointing to sub class object then superclass methods will be called
But not sub classLet us see with example:
Imports systemPublic class class1
Public sub raghu ()Console.writeline (this is raghu)
End subEnd class
Public class class2: inherits class1
Public sub raghu1 ()Console.writeline (this is placid man")
End sub
End class
Public class class3Shared sub main ()Dim a as class1=new class2 ()
a.raghu1 ()a.raghu()
End subEnd class
Output: error
-
8/2/2019 44834621 Vb Net Tutorials
45/51
Above error can be resolved like as fallow:Imports system
Public class class1Public sub raghu ()
Console.writeline (this is raghu)End sub
End classPublic class class2: inherits class1
Public sub raghu1 ()
Console.writeline (this is placid man")End sub
End class
Public class class3Shared sub main ()
Dim a as class1=new class2 ()
a.raghu ()End sub
End class
Output:This is raghu:
When a super class reference is pointing to sub class object and if a method isdesigned in both classes
In this scenario to get the sub class implementation the sub class can override superclass method
To override super class method super class method should be given as a virtualmethod
To declare virtual methods in the vb.net use key word override able, while overridingin the subclass
Use keyword in the sub class override
Note: 1) to stop overriding use shadows2) To stop inheritance use keyword not inheritable (final in java)
Let us see example:
Imports system
Public class class1Public sub raghu ()
Console.writeline (this is raghu)
End subEnd class
Public class class2: inherits class1Public sub raghu ()
Console.writeline (this is placid man")End sub
End classPublic class class3
Shared sub main ()Dim a as class1=new class2 ()
a.raghu ()End sub
End class
-
8/2/2019 44834621 Vb Net Tutorials
46/51
Output: there is a conflict of same method names of raghuUse keyword like shadows
With help of shadows:
Imports systemPublic class class1
Public sub raghu ()Console.writeline (this is raghu)
End sub
End classPublic class class2: inherits class1
Public sub raghu ()
Console.writeline (this is placid man")End sub
End classPublic class class3
Shared sub main ()Dim a as class1=new class2 ()
a.raghu ()
End subEnd classOutput
This is raghu
Imports system
Public class class1Public sub raghu ()
Console.writeline (this is raghu)End sub
End class
Public class class2: inherits class1Shadows Public sub raghu ()Console.writeline (this is placid man")
End sub
End classPublic class class3
Shared sub main ()Dim a as new class2 ()
a.raghu()
End subEnd class
Output:
This is placid man
With help of overridable overridesImports system
Public class class1Public overridable sub raghu ()
Console.writeline (this is raghu)End sub
End class
-
8/2/2019 44834621 Vb Net Tutorials
47/51
Public class class2: inherits class1Public overrides sub raghu ()
Console.writeline (this is placid man")End sub
End classPublic class class3
Shared sub main ()Dim a as new class2 ()
a.raghu ()
End subEnd class
Output:This is placid man
Suppose we want to display super class member with help of (mybase.methodname)
Imports systemPublic class class1
Public overridable sub raghu ()
Console.writeline (this is raghu)End subEnd class
Public class class2: inherits class1Public overrides sub raghu ()
Mybase.raghu ()Console.writeline (this is placid man")
End subEnd class
Public class class3Shared sub main ()
Dim a as new class2 ()
a.raghu ()End subEnd class
Output:This is raghu
This is placid man
Abstraction
Abstraction can be defined the way of representing of a class or method with out any
background,Or without any explanation
To create an abstract class in VB.NET, the class declaration should be done as:Must inherit class class1
Abstract and interfaces
Abstract:1)It allow two methods
a) Concrete methodsb) Non concrete methods
-
8/2/2019 44834621 Vb Net Tutorials
48/51
2) Not possible to create object for abstract classes3) It can inherited
Interfaces:
1) By default interfaces are public2) Same as class but except in methods should be abstract
3)And variable should be final4)It can inherited
See for example:
Imports system
Public interface in1
Function setname () as stringSub raghu (person name as string)
End interface
Public class class1Implements in1Dim name as string
Sub raghu (person name as string) implements in1.raghuName=person name
End sub
Function setname () as string implements in1.setnameConsole.writeline (name)
End functionEnd class
Public class class2Shared sub main ()Dim a as new class1 ()
a.raghu ("raghu Nethikunta")
Username ()End sub
End class
Conclusion:
1) In my above program I have 2absrtact methods setname (), raghu ()2)Interface cannot be private
3)By default it is public4)In interface body dont use access keyword for methods, functions
Late Binding:
Everything is resolved at runtime is called as late binding
But vb support only early binding
Let us see for program better to understand:
Option strict off
-
8/2/2019 44834621 Vb Net Tutorials
49/51
Imports system
Public class class1Public sub raghu ()
Console.writeline ("this is raghu")End sub
End class
Public class class2
Public sub raghu ()Console.writeline ("this is placid man")
End sub
End classPublic class class3
Public shared sub Display (o as object)o.raghu()
End subShared sub main ()
Dim a as new class1 ()
Dim b as new class2 ()Display (a)Display (b)
End subEnd class
Output:
This is raghu
This is placid man
Conclusion:
If use option strict is on our vb.net complier throw a beautiful errorSo take care of option strict while programming for late binding
Pass by (Val, Ref, Out)
Pass by Val
using System;
Public class class1{
public void hello (int i){
i=i+1;Console.WriteLine (i);
}static void Main ()
{Int j=10;
class1 a =new class1();
-
8/2/2019 44834621 Vb Net Tutorials
50/51
a. hello (j);Console.WriteLine (j);
}}
Output: 11
10
Reason: arguments, parameters both are pointing to different location
Pass by ref
using System;Public class class1
{public void hello (ref int i)
{i=i+1;
Console.WriteLine (i);
}static void Main (){
Int j=10;class1 a =new class1 ();
a. hello (ref j);Console.WriteLine (j);
}}
Output:
11
11
Reason: arguments, parameters both are pointing to same location
Pass byout:
using System;
Public class class1
{public void hello (out int i)
{i=i+1;
Console.WriteLine (i);}
static void Main (){
Int j=10;class1 a =new class1 ();
a. hello (ref j);Console.WriteLine (j);
}
-
8/2/2019 44834621 Vb Net Tutorials
51/51
}output: error:
Reason: 1)it never accepts any input parameters
2)So it will act as pass by ref (but it not accepts any input parameters)
Let us check:
using System;Public class class1
{
public void hello (out int i){
i=100i=i+1;
Console.WriteLine (i);}
static void Main ()
{Int j=10;class1 a =new class1 ();
a. hello (ref j);Console.WriteLine (j);
}}
output:
101101