Visual Basic Concepts

Using Default Properties

See Also

Many objects have default properties. You can use default properties to simplify your code, because you don't have to refer explicitly to the property when setting its value. For an object where Value is the default property, these two statements are equivalent:

object = 20

and

object.Value = 20

To see how this works, draw a command button and a text box on a form. Add the following statement to the command button's Click event:

Text1 = "hello"

Run the application and click the command button. Because Text is the default property of the text box, the text box will display the text, "hello."

Using Default Properties with Object Variables

When a reference to an object is stored in an object variable, you can still use the default property. The following code fragment demonstrates this.

Private Sub Command1_Click()
   Dim obj As Object
   ' Place a reference to Text1 in the object
   '   variable.
   Set obj = Text1
   ' Set the value of the default property (Text).
   obj = "hello"
End Sub

In the code above, obj = "hello" is exactly the same as typing obj.Text = "hello".

Using Default Properties with Variants

Accessing default properties is different when an object reference is stored in a variable of type Variant, instead of in an object variable. This is because a Variant can contain data of many different types.

For example, you can read the default property of Text1 using a reference in a Variant, but trying to assign the string "goodbye" to the default property doesn't work. Instead, it replaces the object reference with the string, and changes the Variant type.

To see how this works, enter the following code in the Click event of the command button from the previous example:

Private Sub Command1_Click()
   Dim vnt As Variant
   ' Set the default property (Text) to "hello".
   Text1 = "hello"
   ' Place a reference to Text1 in the Variant.
   Set vnt = Text1
   ' Display the default property of Text1, and show
   '   that the Variant contains an object reference.
   MsgBox vnt, , "IsObject? " & IsObject(vnt)
   ' Attempt to set the default property of Text1.
   vnt = "goodbye"
   MsgBox vnt, , "IsObject? " & IsObject(vnt)
End Sub

When you run the application and click the command button, you first get a message box displaying the current value of the default property of Text1, "hello," which you can verify by looking at Text1. The caption of the message box confirms that the Variant contains an object reference — that is, a reference to Text1.

When you click the OK button on the message box, "goodbye" is assigned to the Variant, destroying the reference to Text1. Another message box is then displayed, showing the contents of the Variant — which as you can see doesn't match the current value of Text1.Text.

The caption of the message box confirms that the Variant no longer contains an object reference — it now contains the string "goodbye."

For More Information   For details on Variants and other data types, see "Introduction to Variables, Constants, and Data Types" in "Programming Fundamentals."

Other aspects of using objects with Variants are discussed in "The Visual Basic Collection Object."