This documentation is archived and is not being maintained.

The Basics of Working with Objects

Visual Studio .NET 2003

Almost everything you do in Visual Basic .NET involves objects. This topic shows you some ways that the elements of objects are used.

Member Access

You access the members of an object by specifying, in order, the name of the object, a period, and the name of the member you want to use. For example, if you have an object named Obj1 that has a property named Prop1 you could use the syntax Obj1.Prop1 to access that property. In some cases you need to use several dots because objects can contain other objects. For example, you could use the syntax Obj1.Prop1.X if the property Prop1 is an object that contains a field named X.

Fields and Properties

Properties and fields represent information stored in an object. The values of fields and properties are retrieved and set with assignment statements the same way that variables are retrieved and set. The following example sets and retrieves the value of a field and a property.

Dim X As Integer = 8
Dim Obj1 As New Class1   ' Declares an instance of a class.
Obj1.Prop1 = X   ' Property assignment. Assigns the number 8 to Prop1.
X = Obj1.Prop1 + 1   ' Retrieval. The variable X contains the number 9.
Obj1.Field1 = X    ' Field assignment. Assigns the number 9 to Field1.
X = Obj1.Prop1 + 1   ' Retrieval. The variable X contains the number 10.

Methods

Methods are procedures defined within a class. Methods are used like subroutines or functions, but are accessed by way of the object to which they belong. The following code fragment calls a function and a subroutine declared in an object:

Dim Obj2 As New Class2   ' Declares an instance of a class
Dim Y As Integer
' Retrieves a value from a method declared as a function.
Y = Obj2.GetNumber   
Obj2.StoreNumber(Y)   ' Calls a method declared as a subroutine.

Events

Events are notifications that cause a procedure to execute. Objects can both handle and generate events. Events are generated using the RaiseEvent statement and handled using either the Handles keyword or the AddHandler statement. The following example raises and handles an event declared using the WithEvents keyword:

 
' Declares a WithEvents variable in the declarations section.
   Dim WithEvents EClass As EventClass

   Class EventClass
      Public Event AnEvent()   ' Declares an event.

      Sub CauseEvent()
         RaiseEvent AnEvent()   'Raises an event.
      End Sub
   End Class

' Declares an event handler
   Sub EventHandler() Handles EClass.AnEvent
      MessageBox.Show("Received Event.")
   End Sub

' Test the class and the event handler.
   Sub TestEvents()
      EClass = New EventClass()   ' Creates an instance of the class.
      EClass.CauseEvent()   ' Calls a method that raises an event.
   End Sub

See Also

What Is an Object? | Creating and Using Objects | Setting and Retrieving Properties | Performing Actions with Methods | Events and Event Handlers | Objects from Visual Basic and Other Sources | RaiseEvent Statement | Handles | AddHandler Statement | WithEvents

Show: