“全局变量"在Visual C#中 [英] "Global variable" in Visual C#

查看:95
本文介绍了“全局变量"在Visual C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了Graph类,并且我想模拟一个分销网络. 图表工作100%.但是,我想在所有应用程序中使用相同的结构/类! 例如: 我有显示模拟的Form1,但是我想插入Nodes(例如),但是我想在Form2中做它! 由于数据始终在同一类中,因此我可以使Graph实例成为全局实例,但C#不采用全局变量.那么,我该如何解决呢?有任何想法吗?谢谢!

I have made the Graph class, and i want to simulate a distribution network. The Graph works 100%. But, i want to use that same struct/class in all my application! For example: I have Form1 that shows the simulation, but i want to insert Nodes (for example) but i want to do it in Form2! Since the data is always in the same class, i could make my Graph instance global but C# does not take global variables. So, how would i solve this? Any ideas? Thank you!

推荐答案

在表单的构造函数中提供对Graph的引用.

Give the forms a reference to the Graph in their constructor.

 Graph g = new Graph();
 Form1 f1 = new Form1(g);
 Form2 f2 = new Form2(g);

然后这两种形式都在同一个图形上工作.

Then both forms are working with the same graph.

这篇关于“全局变量"在Visual C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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