vb.net VB6关键字Set是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18051563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB6 keyword Set what does it mean?
提问by Netorica
I been browsing an old VB6 code and I saw something like this
我一直在浏览一个旧的 VB6 代码,我看到了这样的东西
Set AST = CreateObject("ADODB.Stream")
I have experience using VB6 and VB.NET but I never use this keyword Setbefore in my VB6 projects. I researched a lot in the internet what is the use of Setand what I only know is the usage in Propertieswhich is only I know in VB.NET
我有使用 VB6 和 VB.NET 的经验,但我以前从未Set在我的 VB6 项目中使用过这个关键字。我在网上研究了很多,什么是用的Set,我只Properties知道在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 Setis used differently on the code above. What is the difference of the two approaches? I want to know what does the Setdo in VB6
我认为Set在上面的代码中使用不同。这两种方法有什么区别?我想知道Set在 VB6 中做什么
回答by Damien_The_Unbeliever
Setis assigning a new reference to the ASTvariable, rather than assigning a value to (the object currently referenced by AST)'s default property.
Set正在为AST变量分配一个新的引用,而不是为(当前被 引用的对象AST)的默认属性分配一个值。
There's not much VB 6 documentation around on the web, but1some of the help for VB.Net still references the older ways.
网络上没有多少 VB 6 文档,但是1一些 VB.Net 的帮助仍然引用了旧的方法。
See Default Property Changed for Visual Basic 6 Users:
请参阅为 Visual Basic 6 用户更改的默认属性:
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.
在 Visual Basic 6.0 中,对象支持默认属性。例如,在 Label 控件上,Caption 是默认属性,下面示例中的两个赋值是等效的。
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.
虽然默认属性在编写 Visual Basic 代码时启用了一定数量的速记,但它们有几个缺点:
...
- 默认属性使 Set 语句在 Visual Basic 语言中是必需的。下面的示例显示了如何需要 Set 来指示要分配的对象引用,而不是默认属性。
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, Letand Setbecame obsolete (in fact, Letwas already pretty much obsolete in VB 6) because the language rules changed. An assignment A = B, if Ais a reference, is always assigning a new reference to A.
因此,在 VB.Net 中,由于语言规则发生了变化Let而Set变得过时(实际上,Let在 VB 6 中已经过时了)。一个赋值A = B,如果A是一个引用,总是给 分配一个新的引用A。
1MarkJ has supplied links to the older VB6 documentation in the comments.
1MarkJ 在评论中提供了旧 VB6 文档的链接。

