Important alert: (current site time 7/15/2013 7:28:30 AM EDT)
 

article

Learn VB .NET fast! -updated again-

Email
Submitted on: 3/21/2002 6:09:44 AM
By: Sahand  
Level: Beginner
User Rating: By 44 Users
Compatibility: VB.NET, ASP.NET
Views: 54344
(About the author)
 
     What's new in VB .NET? How can I use Classes in VB .NET? What does a shared method do?What's the usage of inheritance? How can i override a sub? How can i add events to my classes? Find your answers here! Sample files now ready

This article has accompanying files
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				

 

Part I

Learn VB .NET fast!

So what's the difference between vb 6 and vb .net?

you may have asked yourself that but when you find the answer you will be almost surprised.

The first difference and the most important one is that  vb .NET is a complete Object Oriented Programming language with all the options in an OOP language like classes , Implementation , Inheritance etc.

Here is a brief description of each of these new features:

(Notice that I removed the Public statement from the beginning of the class declarations because it is not almost needed when working with only one module)

Class

A class is a set of properties , and methods in one piece of code that can be used multiple times. a class itself is not an object but objects can be created from classes.

The following code demonstrates a simple class:

Class SimpleClass

        Public Sub SimpleMethod()

                System.Console.Write("Simple Class")

        End Sub

End Class

The above code shows the declaration of the SimpleClass(note that this class have no use unless you make an Instance of it)

The following code shows how to use the SimpleClass class:

Class Prog1

        Public  Shared Sub Main()

                Dim obj as New SimpleClass()

                obj.SimpleMethod()

        End Sub

End Class

Any program in vb .NET must have at least one class (or module) with a 'Main' method which is where the program starts. This sub must be shared (explained later)

in the above example the program's class is called "Prog1" and it has a main method and an instance of the SimpleClass class is created using the 'Dim' statement , Dim statement is used to declare Variables and Objects. 'New' statement makes a new instance and gives it the memory space it needs , remember that if you don't use the 'New' statement you will only get a reference no an object unless you set it to an object again using the 'New' statement.

To separate a method or property from its owner object you must use dot ('.')  like obj.SimpleMethod()

 

Class members

Class members can be Public , Protected and Private.

Members that are public are visible to objects derived from the class, classes that inherit this class and the class itself.

Members that are private are only visible to the class itself.

Member Type/What can access it

The class itself

Derived Classes

Objects derived from the class

Public

Yes

Yes

Yes

Protected

Yes

Yes

No

Private

Yes

No

No

Members that are protected can be used in the class itself and classes that inherit the class.

The following example shows how to use different member kinds

Class SimpleClass2

        Private MemberVar1 as Long

        Protected MemberVal2 as Long

        Public MemberVal3 as Long

End Class

Class Prog2

        Public  Shared Sub Main()

                Dim obj as New Simpleclass2()

                'obj.MemberVal1 = 1           'wrong because it's a private member

                'obj.MemberVal2 = 2           'wrong because it's a protected member

                obj.MemberVal3 = 3           'correct because it's a public member

        End Sub       

End Class

 

Constructors

Sometimes you want to give some members of a class some initialization value when the object is created . This can be done by using the 'New' as sub.

The following code shows how to use them:

Class SimpleClass3

        Public Var as Long 

        Public Sub New()

                Var = 10  'Initialization value

        End Sub

End Class

Class Prog4

        Public Shared  Sub Main()

                Dim obj as New Simpleclass3()

                System.Console.Write(obj.Var) 

        End Sub       

End Class

When the object is created , the value of Var is set to 10.

You also send parameters to the constructor like this:

Class SimpleClass4

        Public Var as Long 

        Public Sub New(InitValue as Long)

                Var = InitValue  'Initialization value

        End Sub

End Class

Class Prog5

        Public  Shared Sub Main()

                Dim obj as New Simpleclass4(1000)   'The initialization value is 1000

                System.Console.Write(obj.Var) 

        End Sub       

End Class

Shared Methods

Sometimes you want to write a class that has methods which are not related to a specific object e.g. a class that has math methods. So you want to use the methods without creating an object first. In this case you can declare it using the 'Shared' statement the following code is an example of shared methods:

NOTE: shared methods and properties can not access methods, properties or variables that are not shared.

Class SimpleClass5

        Public Shared Function Multiply (ByVal Num1 as Long , ByVal Num2 as Long) as Long

                Return Num1*Num2

        End Sub

End Class

 

Class Prog6

        Public  Shared Sub Main()

                System.Console.Write(SimpleClass5.Multiply(10,10)) 

        End Sub       

End Class

Part II

Inheritance

This subject is almost the most important in OOP, it makes writing codes a lot faster because you don't have to write code for each of the classes and instead you write a general class and other classes will inherit it. for example in a game you write a general character class then you can write more specific classes that inherit this class for example enemy class and friend class and again more detailed classes which inherit these items.

And the great thing is that inheritance is available in VB .NET. the following example shows how to use it:

Class A     ' parent class

        Public Sub A()

                System.Console.Write("A")

        End Sub

End Class

 Class B     ' fist child class

        Inherits A    ' this line tells VB that this class inherits A

        Public Sub B()

                System.Console.Write("B")

        End Sub

End Class

Class C     'second child class

        Inherits B    ' this line tells VB that this class inherits A

        Public Sub C()

                System.Console.Write("C")

        End Sub

End Class

Class Prog6

        Public  Shared Sub Main()

                Dim objC as New C()  ' always remember to use parentheses when using the New statement

                objC.A()  'because C inherits B and B inherits A so it has A() too

                objC.B()  'inherits B() directly from B

                objC.C()  'classes own sub

        End Sub       

End Class

Abstract classes (MustInherit)

Some classes are not used to create object directly from them but are used to write classes that inherit them. Following code shows this matter:

MustInherit Class A     ' parent class

        Public Sub A()

                System.Console.Write("A")

        End Sub

End Class

 Class B     ' fist child class

        Inherits A    ' this line tells VB that this class inherits A

        Public Sub B()

                System.Console.Write("B")

        End Sub

End Class

Class Prog7

        Public Shared  Sub Main()

                'Dim objA as New A()       'this is wrong because you can not create an instance from a mustinherit class

                Dim objB as New B()  ' always remember to use parentheses when using the New statement

                objB.A() 

                objB.B() 

        End Sub       

End Class

Overriding

When writing a class that inherits another class you may want to change what a sub or function in the base class does  in this case you can use overriding:

Class A     ' parent class

        Public Overridable Sub A()

                System.Console.Write("A.A")

        End Sub

End Class

 Class B     ' fist child class

        Inherits A    ' this line tells VB that this class inherits A

        Public Overrides Sub A()

                System.Console.Write("B.A")

        End Sub

End Class

Class Prog8

        Public Shared Sub Main()

                Dim objB as New B() 

                objB.A()  ' the output will be "B.A" not "A.A"

        End Sub       

End Class

Events

If you have used VB 6 or any other windows programming language then you must be already familiar with events.

An event is a piece of code (a procedure) which executes when something especial happens such as the users clicks a button , or text of a textbox changes.

classes can have events. Following code shows how to use events:

Class A

        Public Event OnInitialize  ' declares the event as public

        Public Sub New()

                RaiseEvent OnInitialize() ' causes the event to be executed

        End Sub

        Public Overridable Sub A()

                System.Console.Write("A.A")

        End Sub

End Class

Class Prog9

        Private WithEvents objA as New A()  ' notice 'WithEvents' it tells vb that this class has events

        Public Shared Sub Main()

                Dim objB as New B() 

                objB.A()  ' the output will be "B.A" not "A.A"

        End Sub       

        Private sub objA_OnInitialize() Handles objA.OnInitialize    ' "Handles" tells vb that this sub is the event handler for the "objA.onInitialize" event

                System.Console.WriteLine("objA initialized")

        End Sub

End Class

This time it has the sample files!

More coming soon!

 

 

 

 

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 9 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
3/21/2002 6:38:54 AMCC

Great, I needed some starting information on VB .Net. Good job
(If this comment was disrespectful, please report it.)

 
3/21/2002 3:15:42 PMMax Raskin

Great tutorial, keep up the good work !
(If this comment was disrespectful, please report it.)

 
3/23/2002 11:39:27 PMsreejith

great!!!
(If this comment was disrespectful, please report it.)

 
4/21/2002 12:56:39 PMGellet

Why don't you put it into a doc/zip file will be easy for people to download and refer.
(If this comment was disrespectful, please report it.)

 
7/15/2002 10:28:03 PMZlatan

Very good tutorial!... keep up the good work!
(If this comment was disrespectful, please report it.)

 
8/14/2002 4:25:19 AMbarbarq

excellent work !!
i voted
(If this comment was disrespectful, please report it.)

 
9/12/2002 8:01:33 AM

Greate work!!! keep it updated!

(If this comment was disrespectful, please report it.)

 
10/23/2002 5:54:52 AM

This is fine job
(If this comment was disrespectful, please report it.)

 
1/9/2003 11:39:27 AM

Excellent-Straight to the point for those of us in the trenches trying to addapt our applications to this new structure.

Thanks!
(If this comment was disrespectful, please report it.)

 
2/24/2003 2:36:10 PMnfs

Cool work.

Do you want to make money with your programming skills ?

Software Objects provide following services :

1)Sell your software.
2)Post a software to be done.
3)Bid on the software projects.
4)Buy software

Thanks and have a nice day.

Software Objects

http://www.thesoftwareobjects.com

(If this comment was disrespectful, please report it.)

 
4/26/2003 4:28:06 PMSahand

A quick tip guys: make sure you know how to use addhandler and removehandler
keywords and use events as much as you can in your classes. they make your life much easier . take a look at sIRC to see why i'm sayng this! :)
(If this comment was disrespectful, please report it.)

 
5/9/2003 7:11:20 AM

cool tutorial! I like it very much..
i will vote it as 'Excellent'!
(If this comment was disrespectful, please report it.)

 
10/21/2003 11:58:32 AM

Brilliant Just What I Wanted
Thanks Sahand
(If this comment was disrespectful, please report it.)

 
10/21/2003 12:00:00 PM

Brillant Just what i was looking for
thanks Sahand
(If this comment was disrespectful, please report it.)

 
10/12/2004 5:34:10 AM

I felt lost, not anymore...
(If this comment was disrespectful, please report it.)

 
3/27/2005 5:38:20 PMimageofgod

SaHand; wanted to let you know that in the "Shared" Topic you have this:
Public Shared Function Multiply (ByVal Num1 as Long , ByVal Num2 as Long) as Long

Return Num1*Num2

End Sub ''''''I thought it should be "End Function"
-will
(If this comment was disrespectful, please report it.)

 
10/24/2005 10:50:21 PMRaghavendra R

Nice to have site like this to help developer...I really very pleased...
(If this comment was disrespectful, please report it.)

 
6/26/2006 3:31:59 AMSam

Good job. It is short but COMPLETE.
Give us more. Thank you.
(If this comment was disrespectful, please report it.)

 
9/17/2006 7:12:26 AMsam

Thanks...It is short but complete
(If this comment was disrespectful, please report it.)

 
12/6/2006 6:34:39 PMAna

Thanks. The information is very helpful. Please keep it updated
(If this comment was disrespectful, please report it.)

 
12/6/2006 6:35:16 PMAna

Thanks for the info. Its very helpful. Pls keep it updated
(If this comment was disrespectful, please report it.)

 
12/16/2008 3:54:30 PMBaron

Thanks
Like it very straight to the point
(If this comment was disrespectful, please report it.)

 
3/4/2009 8:31:28 PMGopinath-Karyadath

Great ! ,Thanq for starting in vb.net
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.