需要将数据存储在C#类中 [英] need to store a data in a C# class

查看:307
本文介绍了需要将数据存储在C#类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将数据(变量)存储到一个单独的C#公共类中,该类可以从所有表单进行全局访问?

我尝试存储到字符串中,但是在打开另一种形式后该字符串的值丢失了.

请帮帮我.

任何提示或想法将不胜感激:)

解决方案

使用单例模式: http://en.wikipedia.org/wiki/Singleton_pattern [ ^ ].

请在本文中找到作为C#类的singleton的良好实现: http://csharpindepth.com/Articles/General/Singleton .aspx [ ^ ].

—SA


CG是正确的,static关键字是最佳解决方案,易于使用,或者您可以尝试使用property属性传递数据:)

我建​​议购买一本基本的编程书籍并阅读.问题的答案是,变量必须是静态的,只有一个实例可以从任何地方访问.但是,如果您不知道,那么您在尝试编写实际上可以做任何事情的代码之前应该解决的知识上就有很大的差距.


how can i store a data(variable) into a separate C# public class, that can be globally access from all forms?

i tried storing into a string but the value of that string gets lost after opening of another form.

Please help me out.

any hints or ideas would be appreciated :)

解决方案

Use singleton pattern: http://en.wikipedia.org/wiki/Singleton_pattern[^].

Please find a good implementation of singleton as a C# class in this article: http://csharpindepth.com/Articles/General/Singleton.aspx[^].

—SA


CG is right, static keyword is the best solution and easy to use, or you can try use property to pass the data :)


I suggest buying a basic programming book and reading it. The answer to your question is, your variable needs to be static, to have just one instance you can access from anywhere. But, if you don''t know that, you have huge gaps in your knowledge that you should address before trying to write any code that actually does anything.


这篇关于需要将数据存储在C#类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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