I been browsing an old VB6 code and I saw something like this
Set AST = CreateObject("ADODB.Stream")
I have experience using VB6 and VB.NET but I never use this keyword Set
before in my VB6 projects. I researched a lot in the internet what is the use of Set
and what I only know is the usage in Properties
which is only I know in VB.NET
Public Property myProperty As String
Get
Return _myProperty
End Get
Set(value as String)
_myProperty = value
End Set
End Property
and I think Set
is used differently on the code above. What is the difference of the two approaches? I want to know what does the Set
do in VB6
The Set procedure is used to set the value of the property. Visual Basic automatically calls a property's Set procedure when an assignment statement provides a value to be stored in the property. Visual Basic passes a parameter to the Set procedure during property assignments.
Keywords are words with special meaning in a programming language. In Visual Basic . NET, keywords are reserved; that is, they cannot be used as tokens for such purposes as naming variables and subroutines.
The New keyword is also used in type parameter lists to specify that the supplied type must expose an accessible parameterless constructor. For more information about type parameters and constraints, see Type List. To create a constructor procedure for a class, set the name of a Sub procedure to the New keyword.
A keyword is a reserved word with special meanings in the compiler, whose meaning cannot be changed. Therefore, these keywords cannot be used as an identifier in VB.NET programming such as class name, variable, function, module, etc.
Set
is assigning a new reference to the AST
variable, rather than assigning a value to (the object currently referenced by AST
)'s default property.
There's not much VB 6 documentation around on the web, but1 some of the help for VB.Net still references the older ways.
See Default Property Changed for Visual Basic 6 Users:
In Visual Basic 6.0, default properties are supported on objects. On a Label control, for example, Caption is the default property, and the two assignments in the following example are equivalent.
Dim lbl As Label
lbl = "Important"
lbl.Caption = "Important"
While default properties enable a certain amount of shorthand in writing Visual Basic code, they have several drawbacks:
...
- Default properties make the Set statement necessary in the Visual Basic language. The following example shows how Set is needed to indicate that an object reference, rather than a default property, is to be assigned.
Dim lbl1 As Label, lbl2 As Label
lbl1 = "Saving" ' Assign a value to lbl1's Caption property.
lbl2 = lbl1 ' Replace lbl2's Caption property with lbl1's.
Set lbl2 = lbl1 ' Replace lbl2 with an object reference to lbl1.
So, in VB.Net, Let
and Set
became obsolete (in fact, Let
was already pretty much obsolete in VB 6) because the language rules changed. An assignment A = B
, if A
is a reference, is always assigning a new reference to A
.
1MarkJ has supplied links to the older VB6 documentation in the comments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With