在 VB.NET 中跨表单共享变量的最佳实践 [英] Best practice for sharing variables across forms in VB.NET

查看:26
本文介绍了在 VB.NET 中跨表单共享变量的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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. 在其中一个表单中创建一个静态/共享变量并通过以下方式在其他表单中访问它:

  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

  • 在创建子表单时将主表单的实例发送到子表单.然后可以通过属性函数访问这些变量.

  • 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.

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

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

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

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

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

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

    推荐答案

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

    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. 假设您有一个包含记录列表的表单,并且用户双击了一个记录,因此您想要打开一个新表单来编辑该记录,并且您想要传递记录 ID.在这种情况下,我将向第二种形式添加一个构造函数方法:Sub New(RecordID as String)'在此处添加代码以加载记录结束子

    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

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

    说你的应用中有一些全局的东西,比如应用启动的时间,这样你就可以告诉用户你已经使用了 5 个小时的应用,去生活吧!"这些东西可以存储在一个模块中.(或者在应用程序类中,但它非常隐藏)

    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)

    这篇关于在 VB.NET 中跨表单共享变量的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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