Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

vb.net - VB6 keyword Set what does it mean?

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...