Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

VB .NET - Learn Visual Basic .NET

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 3

Referring to a Property Within Code

If you need to read or write to a property within code, you need to refer to the object's name in front of the property name using the general syntax:

ControlName.PropertyName

For example, if you want to evaluate a CommandButton's Enabled property within an If condition, you can do it in one of two ways, as illustrated in the following examples:

If cmdAdd.Enabled = True Then

or

If cmdAdd.Enabled Then

Notice that, in this case, the Enabled property is a Boolean type and, therefore, you can imply a True value, as in the second example. You can also assign a value to the default property without naming the property as long as you assign the correct data type for the property:

cmdAdd.Enabled = True

Each control has a default property (usually the most important property for the control in question). The default properties of the three controls under discussion here are:

  • CommandButton Value property
  • Label Caption property
  • TextBox Text property

This property can be set or read in code simply by using the name of the control without the property name. So, for example, you could write the following code to set a CommandButton's Value property, a TextBox control's Text property, and a Label's Caption property:

cmdOK = True
txtName = "Jones"
lblName = "Name"

Some programmers might argue against this type of implicit reference to the default property on the grounds that it's a bit less clear in code. On the other hand, however, implicitly referring to the default property actually makes for faster performance at runtime.


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved