JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Fields, Properties, Methods, and Events

Fields, Properties, Methods, and Events are called the members of a class. Inside the class, members are declared as either Public, Private, Protected, Friend, or Protected Friend:

  • Public— Gives variables public access, which means there are no restrictions on their accessibility.

  • Private— Gives variables private access, which means they are accessible only from within their class, including any nested procedures.

  • Protected— Gives variables protected access, which means they are accessible only from within their own class or from a class derived from that class. Note that you can use Protected only at class level (which means you can't use it inside a procedure), because you use it to declare members of a class.

  • Friend— Gives variables friend access, which means they are accessible from within the program that contains their declaration, as well as anywhere else in the same assembly.

  • Protected Friend— Gives variables both protected and friend access, which means they can be used by code in the same assembly, as well as by code in derived classes.

The fields of a class, also called the class's data members, are much like built-in variables (although they also may be constants). For example, I can declare a field named value to the DataClass class we just saw by declaring a variable with that name:

Public Class DataClass
    Public value As Integer
End Class

Now I can refer to that field in an object of this class using the familiar object.field syntax of Visual Basic:

Dim data As New DataClass()
data.value = 5

You also can make fields hold constant values with Const:

Public Class Class1
    Public Const Field1 As Integer = 0
        
End Class

Using fields like this can give you direct access to the data stored inside an object, and that's unusual in OOP because you usually want to check the data being stored in your objects to make sure it's legal first. (For example, you might want to make sure the number of computers stored in your warehouse is not assigned negative numbers, and so on.) An easy way of guarding access to the data in your objects is to use properties. We're all familiar with properties of objects, like this in the code we saw earlier in this chapter:

TextBox1.Size = New Size(150, 20)
TextBox1.Location = New Point(80, 20)
TextBox1.Text = "Hello from Visual Basic"

Properties are retrieved and set like fields, but are handled with the Property Get and Property Set procedures, which provide more control on how values are set or returned. We've first saw how to create properties in Chapter 3. We'll see more on properties in this chapter, such as creating write-only or read-only properties.

Methods represent the object's built-in procedures. For example, a class named Animal may have methods named Sleeping and Eating. You define methods by adding procedures, either Sub routines or functions, to your class; for example, here's how I might implement the Sleeping and Eating methods:

Public Class Animal
    Public Sub Eating()
        MsgBox("Eating...")
    End Sub

    Public Sub Sleeping()
        MsgBox("Sleeping...")
    End Sub
End Class

Now I can create a new object of the Animal class and call the Eating method in the familiar way:

Dim pet As New Animal()
pet.Eating()

And, as we all know, events allow objects to perform actions whenever a specific occurrence takes place. For example, when you click a button, a Click event occurs, and you can handle that event in an event handler, as we already have done so many times. As an example, I'll create a custom event, ThreeClick, in this chapter, which will happen when you click a button three times. We'll be able to set up an event handler for that event that looks like this:

Private Sub tracker_ThreeClick(ByVal Message As String) _
    Handles tracker.ThreeClick
    TextBox1.Text = Message
End Sub
Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor