行为差异 Dim oDialog1 as Dialog1 = New Dialog1 VS Dim oDialog1 as Dialog1 = Dialog1

发布于 2024-09-27 11:55:25 字数 389 浏览 1 评论 0原文

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 use
Dim 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画骨成沙 2024-10-04 11:55:25

Dialog1 是您正在创建的对象的类型

Dim oDialog1 as Dialog1 = Dialog1

就等于说

MyCat is a Cat, and it's a Cat.

不太有道理。

如果您需要知道猫有多少条腿,或者猫是否有毛,那么您可以说 Cat.CountLegs,但不能说 Cat.GetName 或 Cat.Age,因为您不知道自己是哪只猫谈论。

这与您的对话框相同。

Dim oDialog1 as Dialog1 = Dialog1

并不是指任何特定的对话框,只是一般的 Dialog1,这是没有意义的(也不应该在 VB.NET 2008 中编译)。

as 为

Dim oDialog1 as Dialog1 = New Dialog1

您提供了一个全新的 Dialog1,称为 oDialog1。您询问有关 oDialog1 的所有问题都会为您提供有关 Dialog1 对象的通用默认值。

Dialog1 不会有位置,因为它还不存在。但是因为您已经使用 NEW 关键字创建了一个新实例,所以 oDialog1 将成为 Dialog1 类型的第一个对象 - 您可以给它一个位置等。

如果您调用

Dim oDialog2 as Dialog1 = New Dialog1

Then 您将拥有 two< /em> Dialog1 的 - 每个都有一个单独的位置,等等。

如果您为 Dialog1 和 oDialog1 提供更好的名称(例如分别为 UserConfirmationDialog 和confirmExit),这将有助于更有意义。

然后它会变成类似的东西

Dim confirmExit as UserConfirmationDialog = New UserConfirmationDialog.

并且可能

Dim confirmDelete as UserConfirmationDialog = New UserConfirmationDialog.

Dialog1 is the type of the object you are creating.

Dim oDialog1 as Dialog1 = Dialog1

is the same as saying

MyCat is a Cat, and it's a Cat.

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.

Dim oDialog1 as Dialog1 = Dialog1

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

Dim oDialog1 as Dialog1 = New Dialog1

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

Dim oDialog2 as Dialog1 = New Dialog1

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

Dim confirmExit as UserConfirmationDialog = New UserConfirmationDialog.

and possibly

Dim confirmDelete as UserConfirmationDialog = New UserConfirmationDialog.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文