Quick Search for:  in language:    
NET,Whats,Classes,does,shared,method,doWhats,
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
.Net Stats

 Code: 140,189. lines
 Jobs: 376. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for .Net
Click here to see a screenshot of this code!Ray Picking for 3d Objects
By Jay1_z on 11/23

(Screen Shot)

Embed Images into Xml Files
By Chris Richards on 11/23


Encryption and Alternate Data Stream
By Philip Pierce on 11/23


Click here to see a screenshot of this code!AddressBook
By Ekong George Ekong on 11/23

(Screen Shot)

Click here to see a screenshot of this code!Command Line Redirection
By kaze on 11/23

(Screen Shot)

Fader
By Ahmad Hammad on 11/23


Click here to see a screenshot of this code!Get content of a web page (simple)
By Tin Trung Dang on 11/22

(Screen Shot)

Retrieve the long path name for a short path name.
By Özgür Aytekin on 11/22


Easy Randomizer
By Christian Müller on 11/22


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

Learn VB .NET fast! -updated again-

Print
Email
 

Submitted on: 3/21/2002 6:09:44 AM
By: Sahand 
Level: Beginner
User Rating: By 33 Users
Compatibility:VB.NET, ASP.NET

Users have accessed this article 15451 times.
 
(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 7 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
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 AM:CC
Great, I needed some starting information on VB .Net. Good job
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/21/2002 3:15:42 PM:Max Raskin
Great tutorial, keep up the good work !
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/23/2002 11:39:27 PM:sreejith
great!!!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/21/2002 12:56:39 PM:Gellet
Why don't you put it into a doc/zip file will be easy for people to download and refer.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/15/2002 10:28:03 PM:Zlatan
Very good tutorial!... keep up the good work!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
8/14/2002 4:25:19 AM:barbarq
excellent work !! i voted
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/12/2002 8:01:33 AM:
Greate work!!! keep it updated!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/23/2002 5:54:52 AM:
This is fine job
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
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!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/24/2003 2:36:10 PM:nfs
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
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/26/2003 4:28:06 PM:Sahand
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! :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/9/2003 7:11:20 AM:
cool tutorial! I like it very much.. i will vote it as 'Excellent'!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/21/2003 11:58:32 AM:
Brilliant Just What I Wanted Thanks Sahand
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
10/21/2003 12:00:00 PM:
Brillant Just what i was looking for thanks Sahand
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | .Net Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.