| You are here: | About>Computing & Technology>Visual Basic> Learn VB.NET> Shared Members and Instance Members in VB.NET |
![]() | Visual Basic |
Shared Members and Instance Members in VB.NETThere are two kinds of class "members" in VB.NETI'm reminded of the old programmer joke, "There are 10 kinds of programmers. Those who understand binary and those who don't." In a similar way, there are 10 kinds of class members in VB.NET. Shared members and instance members. But before we get into that, let's review the whole concept of classes and a class members. A class is the code for an object. Classes are really just collections of methods and properties that are bundled together for your convenience in using them. These methods and properties are collectively called the class members. The code for a property in the class looks something like this: Public Property aSampleProperty() As datatype And the code for a method might look like this: Public Sub aSampleMethodSub( _ Or this: Public Function aSampleMethodFunc( _ After an object (that is, the compiled class code) is included in your project, you normally have to create an instance of the the object to use the members, something like this: Dim myInstance as New aSampleObject You can then use the methods and properties of the object: myInstance.aSampleProperty = myVariable These types of members are called "instance members" because you have to create an instance, a copy of the object exclusively for your code, to use the members. This is the most common code that you'll see and write when you code your own classes. SideBar Topic One way to prove to yourself that you're using a copy would be to code a method that contains a property and then change the property in copies of the object. For example code these members and in the class: Private m_Property As Integer Then create two instances of the same class in the calling code: Dim myInstance = New testClass Then set the value of the m_Property to different values in each of them and check them out: myInstance.TestMethod(True) Each instance of the object now contains a different property value. End SideBar Topic |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


