Website Design United States, Website Design California, Website Designing United States, Website Designing California

Using the Set Command

Visual Basic 6

This short tutorial will explain why you need to use the Set command when referencing objects.

We will start by creating a simple form to help us visualize what is going on. Create a new Standard EXE Project. That will give us the form we need so now we need some controls. Add three (3) labels and two (2) command buttons to the form. Take two of the labels and change one caption "Let" and the other to "Set". Now name then lblLet and lblSet. With the two command buttons change one caption to "Let" and the other to "Set". Also change the name to cmdLet and cmdSet. Now with the third label change the caption to any phrase you want and change the name to lblDisplay. Finally change lblLet and lblSet's Font to something different that stands out. Make sure that they both have the same exact font though. I will use 18pt, bold, underline Comic Sans MS. You should end up with a form that looks similar to the following:

We will now move on to the code. This will involve two events: cmdLet and cmdSet's Click event. These two events will help display why we need to use the Set command. Their code will be simple:
Private Sub cmdLet_Click()
lblDisplay.Font = lblLet.Font
End Sub

Private Sub cmdSet_Click()
Set lblDisplay.Font = lblSet.Font
End Sub

Run the program and start by clicking the Let button. Notice that the font family (face or name) changes but nothing else. Now click the Set button and notice that the Display font is the same as the Set font.

Now that you see the visual difference between the two this will make explaining it much more easier. In Visual Basic 6 objects can have something called a "Default Property". When you store the value of an object variable to value without specifying a .Property it assumes the Default Property. You may be most familiar with this when setting the text of a Text Box by simply doing
txtName = "Adam" 'Notice the absence of .Text

Since VB has this rather unique ability of the Default Property it causes a bit of a problem when working with objects. Notice that the Let button only changes the Font's Name and nothing else. That is because the Default Value for a Font is Name. Since no property was specified it assumed Name. Therefore only Name changed.

When you click the Set button everything changed and the two fonts are the same. This is where the "Set" command comes into play. It is used to reference the object and not the default member of an object. Set changes the reference of an object to reference (in memory) another object (or Nothing). In this example the Font object of lblDisplay now references the Font object of lblSet. That is why they appear to be the same, they are referencing the same object.

In conclusion, how do you know when and when not to use the Set command? First off you must be working with objects. It will never be necessary, in Visual Basic 6, to use the Set command with primitive data types. You would use the Set command when you want to reference the entire object. It is bad programming practice to use Default Properties and makes it much more clearer what you are doing when you explicitly specify what member you are setting (Let).

WEB DESIGN INDIA
42 B Malviya Nagar , New Delhi-110017

Skype: manmeetsi
Email: support.webdesignindia@gmail.com
Tel: 91-011-40502005, 9810067295

 















 


© 2008-2009 dotnet4all.com