行为差异 Dim oDialog1 as Dialog1 = New Dialog1 VS Dim oDialog1 as Dialog1 = Dialog1
VB.Net 2005
我现在有一个已关闭的 Dialog1。要从模块内的 Dialog1 获取信息,我需要使用
Dim oDialog1 as Dialog1 = **New** Dialog1.
VB.Net 2008
我有一个仍然打开的 Dialog1。要从模块内的 Dialog1 获取信息,我需要使用将 oDialog1 变暗为 Dialog1 = Dialog1
。
VB.Net 2005 不使用 Dim oDialog1 as Dialog1 = Dialog1
进行编译,并坚持使用NEW
发生了什么以及为什么我需要不同的初始化语法?
VB.Net 2005
I have a now closed Dialog1. To get information from the Dialog1 from within a module I need to use
Dim oDialog1 as Dialog1 = **New** Dialog1.
VB.Net 2008
I have a still open Dialog1. To get information from the Dialog1 from within a module I need to useDim oDialog1 as Dialog1 = Dialog1
.
VB.Net 2005 does not compile using Dim oDialog1 as Dialog1 = Dialog1
and insists on NEW
What is going on and why do I need the different initialisation syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Dialog1 是您正在创建的对象的类型。
就等于说
不太有道理。
如果您需要知道猫有多少条腿,或者猫是否有毛,那么您可以说 Cat.CountLegs,但不能说 Cat.GetName 或 Cat.Age,因为您不知道自己是哪只猫谈论。
这与您的对话框相同。
并不是指任何特定的对话框,只是一般的 Dialog1,这是没有意义的(也不应该在 VB.NET 2008 中编译)。
as 为
您提供了一个全新的 Dialog1,称为 oDialog1。您询问有关 oDialog1 的所有问题都会为您提供有关 Dialog1 对象的通用默认值。
Dialog1 不会有位置,因为它还不存在。但是因为您已经使用 NEW 关键字创建了一个新实例,所以 oDialog1 将成为 Dialog1 类型的第一个对象 - 您可以给它一个位置等。
如果您调用
Then 您将拥有 two< /em> Dialog1 的 - 每个都有一个单独的位置,等等。
如果您为 Dialog1 和 oDialog1 提供更好的名称(例如分别为 UserConfirmationDialog 和confirmExit),这将有助于更有意义。
然后它会变成类似的东西
并且可能
Dialog1 is the type of the object you are creating.
is the same as saying
Doesn't quite make sense.
If you need to know about how many legs cats have, or if cats are furry, then you can say Cat.CountLegs, but you can't say Cat.GetName or Cat.Age because you don't know which cat you're talking about.
It's the same with your Dialog.
is not referring to any particular Dialog, just Dialog1's in general, which doesn't make sense (and also shouldn't compile in VB.NET 2008).
Where as
is giving you a brand new Dialog1, called oDialog1. Everything you ask about oDialog1 will give you generic, default about Dialog1 object.
Dialog1 will not have a position, because it doesn't exist yet. But because you've created a new instance of one by using the NEW keyword, oDialog1 will be your first object of the Dialog1 type - you can give it a position, etc.
If you call
Then you'll have two Dialog1's - each with a separate position, etc.
It will help make a bit more sense if you give Dialog1 and oDialog1 better names, such as UserConfirmationDialog and confirmExit, respectively.
It will then become something like
and possibly