Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A simple explanation of Classes and Instances

New Language Features

A simple explanation of Classes and Instances

by  ThatRickGuy  Posted    (Edited  )
Think of a class as a blue print for a house.

Although you have the plans for the house, it doesn't physically exist. You can't live in it, you can't open the front door, you can't do anything with it.

Think of an instance as the house that is built from those blue prints. It physically exists. You can now .MoveIn() and .ThrowAParty()

There are somethings you can do without an instance. These are called "Shared" methods in VB.Net. In the house example, think of these like .getSquareFootage() and .getNumberOfBathrooms(). Since the blueprints describe the house, we know how many square feet and bathrooms there will be even before the house is built.




One of the most obvious changes from VB6 to .Net is the Form. In VB6 the form object would automatically create itself, and you could just use Form.Show. In .Net the form you create is a class. It is a description of what that object will be when it's instantiated.

The first step in showing a form now is to Instantiate the form. To instantiate an object means to assign it memory, luckily this is done for us by using the "New" keyword.

Dim myform as New Form1

Now that the form object exists in memory, we can use it's functions.

myform.show()

One of the next big changes from VB6 is scope. In VB6 you could access myform from anywheres. But if we go back to the house example, you can build two houses from the same blue prints. The houses will be physically identical, but will have different addresses. The same is true for forms in VB.Net. If you declare myform as New Form1 in two different places, you will have two identical forms at two different addresses. If you want to use one of those forms, you need to have access to that specific object. This FAQ: faq796-5773 goes into more depth on how to work between forms.

-Rick
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top