VB6 keyword Set what does it mean?

前端 未结 1 1149
长发绾君心
长发绾君心 2020-11-28 15:35

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.

相关标签:
1条回答
  • 2020-11-28 15:43

    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.

    0 讨论(0)
提交回复
热议问题