在 VB.NET 中跨表单共享变量的最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5451340/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:23:01  来源:igfitidea点击:

Best practice for sharing variables across forms in VB.NET

vb.net

提问by Johan

I need to share variables across two forms in VB.NET. One of them is the main form and the other is a child form.

我需要在 VB.NET 中跨两种形式共享变量。其中一个是主窗体,另一个是子窗体。

I have been searching, and I have found a few methods. I want to use the best method to do this. I have listed a few options below. Please comment on which one would be the best option:

我一直在寻找,我找到了一些方法。我想用最好的方法来做到这一点。我在下面列出了几个选项。请评论哪一个是最好的选择:

  1. Create a static/shared variable in one of the forms and access it in the other forms via:

    Form1 frm = new Form1(); // Creating object of parent to access shared variable
    frm.a = "abc"; // Passing value
    
  2. Send an instance of the main form to the child form when creating the child form. The variables can then be accessed via a property function.

  3. Create global variables in a module. This seems like the easiest option, but I doubt it is the best option.

  4. I also read something about delegates and events, but I don't know how to implement this.

  1. 在其中一个表单中创建一个静态/共享变量,并通过以下方式在其他表单中访问它:

    Form1 frm = new Form1(); // Creating object of parent to access shared variable
    frm.a = "abc"; // Passing value
    
  2. 创建子窗体时,将主窗体的实例发送到子窗体。然后可以通过属性函数访问变量。

  3. 在模块中创建全局变量。这似乎是最简单的选择,但我怀疑它是否是最佳选择。

  4. 我还阅读了一些关于委托和事件的内容,但我不知道如何实现这一点。

If there is another method I haven't mentioned, please share it with me.

如果还有我没有提到的其他方法,请与我分享。

回答by Fred

There is no one answer to the question. All the methods you listed should 'work.' Which you should use depends why you want to share the variable. For example:

这个问题没有一个答案。您列出的所有方法都应该“有效”。您应该使用哪个取决于您为什么要共享变量。例如:

  1. Say you have a form with a list of records, and the user double-clicks a record, so you want to open a new form to edit the record, and you want to pass the record ID. In this case I would add a constructor method to the second form: Sub New(RecordID as String) 'Add code to load the record here End Sub

  2. Say some of the forms in your application may want to know the database path or something else global like that. For that, I would make the appropriate variable on the parent form into a Public variable (called a Field) and access it as MainForm.FieldName. (Disclaimer: Purists will say you shouldn't rely on the somewhat messy fact that VB.NET automatically instantiates an instance of the form class and lets you refer to it by the form name, and that you should instead get a pointer to the actual instance of the form and store it in your child form and access the parent form like that. Actually, this is like number '2' in your post. But it's not actually necessary if you don't mind programmatical incorrectness.)

  3. Say there is something global in your app, like the time the app was started, so you can tell the user "You've been using the app for 5 hours, go get a life!" These things could be stored in a module. (Or in the application class but that's quite hidden)

  1. 假设您有一个包含记录列表的表单,并且用户双击了一个记录,因此您想打开一个新表单来编辑该记录,并且您想传递记录 ID。在这种情况下,我将向第二种形式添加一个构造函数方法: Sub New(RecordID as String) 'Add code to load the record here End Sub

  2. 假设您的应用程序中的某些表单可能想知道数据库路径或其他类似的全局信息。为此,我会将父窗体上的适当变量设置为公共变量(称为字段)并作为 MainForm.FieldName 访问它。(免责声明:纯粹主义者会说你不应该依赖这样一个有点混乱的事实,即 VB.NET 自动实例化表单类的一个实例并让你通过表单名称来引用它,而你应该得到一个指向实际的指针表单的实例并将其存储在您的子表单中并像这样访问父表单。实际上,这就像您帖子中的数字“2”。但如果您不介意编程不正确,则实际上没有必要。)

  3. 假设你的应用程序中有一些全局性的东西,比如应用程序的启动时间,那么你可以告诉用户“你已经使用了 5 个小时的应用程序,去生活吧!” 这些东西可以存储在一个模块中。(或者在应用程序类中,但这是非常隐藏的)

回答by user1432290

Store your global variables in a Module1.vb file, they must be publicly declared to be accessible from all forms:

将全局变量存储在 Module1.vb 文件中,它们必须公开声明为可从所有形式访问:

Public X as String
Public Y as Integer

Then just use them as you would any other variable on any page:

然后就像使用任何页面上的任何其他变量一样使用它们:

X = "Hello"
Y = 10

Textbox1.Text = X
Textbox2.Text = Y

It's not the safest practice so it shouldn't be used for all your variables. But it is very neat and simple.

这不是最安全的做法,因此不应将其用于所有变量。但它非常整洁和简单。

回答by josiahg

Create two forms. Add 3 radio buttons and 1 button to form1. Add a label to form2. In the code for form1 type

创建两个表单。将 3 个单选按钮和 1 个按钮添加到 form1。向 form2 添加标签。在 form1 类型的代码中

    Public rdb As Integer = 1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     Form2.Show()
If RadioButton1.Checked Then
            rdb = 1
        ElseIf RadioButton2.Checked Then
            rdb = 2
        ElseIf RadioButton3.Checked Then
            rdb = 3
        End If
End Sub

Then in form2's code

然后在form2的代码中

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label1.Text = Form1.rdb
    End Sub

回答by HappyToHelp

Not sure if this answers the question but one thing I found useful was to refer to the variables in form1 while programming in form2 as Form1.variablename and when in Form1 and referring to variables in Form2 to use in Form1 as Form2.variablename Basically, refer to variables in other forms by putting the name of the form they are in followed by a . and then the variable name

不确定这是否回答了问题,但我发现有用的一件事是在 form2 中编程时将 form1 中的变量引用为 Form1.variablename 以及在 Form1 中时引用 Form2 中的变量以在 Form1 中使用为 Form2.variablename 基本上,请参阅通过将它们所在的形式的名称后跟一个 . 然后是变量名

回答by Andrew Hymanman

The child form can have public get and set functions for a private variable, and the parent form can set it when it changes and its end, or get it before it uses it to see if it has changed in the child form.

子窗体可以具有私有变量的公共 get 和 set 函数,父窗体可以在它更改和结束时对其进行设置,或者在使用它之前获取它以查看它在子窗体中是否已更改。

回答by PdotWang

You can add public properties to either form. They can access those properties from each other. (That is not called shared though, and is not static in most cases.)

您可以向任一表单添加公共属性。他们可以相互访问这些属性。(虽然这不称为共享,并且在大多数情况下不是静态的。)