使用旧对象实例/以前的对象实例 [英] using old object instances/previous object instances

查看:24
本文介绍了使用旧对象实例/以前的对象实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Visual Basic 2008 Express 中,我想引用一个表单.但是,当我输入 Dim mainmenu as New MainMenu 时,这会创建一个新实例.当我想通过使用表单 2 中的按钮更改表单主菜单中的标签时,我必须执行以下操作:

In Visual Basic 2008 Express, I would like to reference a form. However, when I enter Dim mainmenu as New MainMenu, this creates a new instance. When I want to change a label in form main menu by using a button in form 2, I have to do the following:

    Dim mainmenu As New MainMenu
    Dim pitchint As Integer
    pitchint = Val(Pitch_txt.Text) 'simple way will filter out trailing non-numerics input
    If pitchint > 720 Then
        pitchint -= 720
    ElseIf pitchint > 360 Then
        pitchint -= 360
    End If

    Pitch_txt.Text = pitchint '<--put this line here will solve your "070" issue
    mainmenu.Pitchlbl.Text = pitchint
    If Pitch_txt.Text.Length <> 0 Then
        If Pitchlbl.Text <> Pitch_txt.Text Then
            Pitchlbl.Text = Pitch_txt.Text
        End If
    End If

    Dim yawint As Integer
    yawint = Val(Yaw_txt.Text) 'simple way will filter out trailing non-numerics input
    If yawint > 90 Then
        yawint -= 90
    End If
    If yawint < -90 Then
        yawint += 90
    End If

    Yaw_txt.Text = yawint '<--put this line here will solve your "070" issue

    If Yaw_txt.Text.Length <> 0 Then
        If Yawlbl.Text <> Yaw_txt.Text Then
            Yawlbl.Text = Yaw_txt.Text
        End If
    End If

这将创建窗体主菜单的新实例.然后,当我插入行 mainmenu.Yawlbl.Text = yawintmainmenu.Pitchlbl.Text = pitchint 时,没有任何反应.我没有收到任何错误.请帮忙.提前致谢.

This will create a new instance of the form main menu. Then, when I insert the lines mainmenu.Yawlbl.Text = yawint and mainmenu.Pitchlbl.Text = pitchint, nothing happens. I do not get an error nothing. Please help. Thanks in advance.

推荐答案

我的解决方案被错误描述,并且对于回答这个问题的各种可能方法存在一些混淆,因此我编辑了我的原始帖子以进行比较和对比本页详细讨论的三种主要方法.

My solution has been mischaracterized and there has been some confusion about the various possible approaches to answer this question, so I've edited my original post to compare and contrast the three major approaches discussed at length on this page.

方案一:使用VB.NET默认表单实例

把这一行放在Dim mainmenu As New MainMenu之后:

mainmenu.Show()

您可能有两个 MainMenu 窗体.这是因为 VB 允许您仅通过使用其类名来引用表单的静态实例.所以你可以说,即 MainMenu.Property = value 它将在 VB 创建的静态实例上运行.

You will probably have two MainMenu forms. This is because VB allows you to reference a static instance of a form simply by using its class name. So you can just be saying i.e. MainMenu.Property = value and it will operate on the static instance created by VB.

尝试删除 Dim mainmenu As New MainMenu 行.这可能就是您需要做的所有事情(只要您 Show() 表单),因为您将引用与类名相同.

Try removing the line Dim mainmenu As New MainMenu. This might be all you need to do (as long as you Show() the form) since you called your reference the same as the class name.

方案二:遵循单例设计模式(简化版,无线程安全)

单例设计模式确保一个类只能有一个实例.将此代码放入 MainMenu 肯定会导致屏幕上出现一些错误.

The singleton design pattern makes sure there can only be one instance of a class. Putting this code into your MainMenu will surely cause some errors to appear on your screen.

Public Class MainMenu

    ' static (shared) instance of this class
    Private Shared _instance As MainMenu

    ' function which returns the static instance
    ' with lazy initialization (constructor is called once GetInstance is 
    Public Shared Function GetInstance() As MainMenu
        If _instance Is Nothing Then
            _instance = New MainMenu()
        End If
        Return _instance
    End Function

    ' private constructor to restrict instantiation of this class (only allowed in GetInstance)
    Private Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    ' all the rest of your original MainMenu code here

End Class

修复错误的方法是使用一个变量来保存对 MainMenu 实例的引用.只需将 mainmenu 替换为类似 myMainMenu 的变量,然后在使用表单之前,输入:

The way you fix the errors is by using a variable to hold a reference to the instance of MainMenu. Simply, replace mainmenu with a variable like myMainMenu and before you use the form, put this:

Dim myMainMenu As MainMenu = MainMenu.GetInstance()
myMainMenu.Show()

解决方案 3:创建您自己的实例

解决方案 1解决方案 3 之间存在差异.在解决方案 1 中,如果您只使用默认实例,您将只有一个表单实例.此解决方案允许您拥有任意数量的实例!你可能不需要这个,但是这里...

There are differences between solution 1 and solution 3 . In solution 1, you will only have one instance of the form if you use only default instances. This solution allows you to have any number of instances! You probably don't need this, but here goes...

您将再次创建一个名为 myMainMenu 的 MainMenu 新实例,但这次您直接调用构造函数.

You will make a new instance of MainMenu called myMainMenu again but this time you call the constructor directly.

Dim myMainMenu As New MainMenu()
myMainMenu.Show()

无论您在何处调用名为 mainmenu 的表单,都将其替换为 myMainMenu.我是否提到我们将其称为 myMainMenu 而不是 mainmenu 因为我们不想使用与类名相同的名称?(VB 不区分大小写所以 mainmenu = MainMenu,但这很容易混淆,因为默认实例.编译器使用上下文来确定我们是否在谈论类本身或类的默认实例...)仅当您在解决方案 1 中引用默认静态实例时,使用类名才有效.

Wherever you call the form by the name mainmenu, replace that with myMainMenu. Did I mention that we call it myMainMenu instead of mainmenu because we don't want to use the same name as the class name? (VB is case-insensitive so mainmenu = MainMenu, but this is easily confusing because of the default instance. The compiler uses context to determine if we are talking about the class itself or the class' default instance...) Using the classname only works when you reference the default static instance as in solution 1.

这个解决方案的吸引人之处在于您可以同时拥有多个 MainMenu 实例.所以你可以把它放在后面:

The attractive thing about this solution is that you can have multiple instances of MainMenu alive at the same time. So you can put this after:

Dim myMainMenu2 As New MainMenu()
myMainMenu2.Show()

瞧,您打开了两个 MainMenu.但你可能不需要两个!

And voila, you have two MainMenu open. But you probably didn't need two!

总结

为什么有这么多方法?

好吧,添加第一种方法是为了吸引 VB6 程序员使用 VB.NET,因为在 VB6 中就是这样做的!准确地说,在VB6中可以这样做,但是一些半脑的程序员仍然选择了方法3.但是默认实例方法是如此普遍,因为它对于随意的人来说太容易了程序员使用 - 尤其是那些在 VBA 中使用它的外行!

Well, the first method was added to attract VB6 programmers to VB.NET because that was how it was done in VB6! To be accurate, it can be done this way in VB6, but some programmers with half a brain still chose to follow method 3. But the default instance method was so widespread because it was so easy for the casual programmer to use - especially all those laymen using it inside VBA!

第二种方法在某些情况下是首选,但在其他情况下不是.它是单例设计模式的简单实现.查看 Douglas Barbie 的回答中的链接,以获得对它的体面解释和一些 Java 示例.它列出了你需要使用它的所有时间的一个很好的总结——一个应该只有一次实例的记录器、一个配置加载器、一个工厂或其他实例生成器——这些可能超出了你的范围,但也许只是想想Microsoft Office 中的打印对话框.一次只需要打开一个打印窗口.这是一个很好的例子.你的代码需要这个吗?遵循该模式允许这种行为,但正如我在示例中所说的那样,还有一些额外的配置.如果应用程序足够简单,它可能不需要它.

The second method is preferred in some instances, but not in others. It is a simple implementation of the Singleton design pattern. Check out the link in Douglas Barbie's answer for a decent explanation of it and some examples in Java. It lists a good summary of all the times you'd need to use it - a logger which should only have once instance, a configuration loader, a factory or other instance generator - these are probably out of your scope, but maybe simply think about a Print dialog in Microsoft Office. There only needs to be one Print window open at a time. This is a good example of it. Does your code require this? Following the pattern allows for this behavior, but there is some additional configuration as I put in the example. If the App is simple enough, it probably doesn't need it.

第三个是面向对象编程的典型示例(一个很好的起点是理解 OOP;仅这些知识就可以为您提供将来解决此类问题的工具).它使用您创建的名为 MainMenu 的类,并可能使用该类的多个实例,称为 objects.这样做的一个优点是您可以拥有同一个类的两个对象实例,但这些对象具有不同值的属性.想想 Car 类的两个实例,一个有 Car.Make = "Ford",另一个 Car.Make = "Chevy".两款车,但它们的财产价值不同.这与前两个的不同之处仅在于您可以拥有多个实例.如果我们想要获得技术,您可以合并解决方案 1 和 3 并使用默认实例并创建您自己的实例,但几乎所有讨论它的人都警告不要这样做(谷歌VB.NET 表单默认实例"了解更多那件事).

The third is a quintessential example of object oriented programming (a good place to start is understanding OOP; this knowledge alone should give you the tools to solve problems like this in the future). It uses a class called MainMenu, which you created, and uses potentially more than one instance of the class, called objects. An advantage of this is that you can have two object instances of the same class, but the objects have properties with different values. Think two instances of class Car, one has Car.Make = "Ford", the other Car.Make = "Chevy". Both cars, but they differ by property values. This differs from the first two only by the fact that you can have multiple instances. If we want to get technical, you can merge solution 1 and 3 and use the default instance and make your own instances, but this is warned against by just about everyone who discusses it (Google "VB.NET form default instance" for more on that matter).

最后,如果您是小规模编码,那么任何对您来说都可靠的方法.

In the end, if you are coding something on a small scale, it's whatever works reliably for you.

这篇关于使用旧对象实例/以前的对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆