在另一个类中使用一个类的对象? [英] Use of one class object in another class?

查看:159
本文介绍了在另一个类中使用一个类的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出在C#中。在该应用程序我有一个类 DataCapture.cs 应用程序。在同一个应用程序,我有另一个类为 Listner.cs 。在这里,在Listner.cs类我想使用 DataCapture.cs 的对象,而无需创建 DataCapture.cs 的新对象。就好像我创建的 DataCapture.cs 新对象,我不能访问数据 DataCapture.cs ,因为它创造类和所有数据的新实例得到,因为我事先用 DataCapture.cs 。请帮助me.Thanks收集丢失。


< DIV CLASS =h2_lin>解决方案

您vould使用的 Singleton模式,以确保只有一个对象类code> 存在。然后,您可以得到对象



DataCapture.Instance。 ......,其中.Inastance为公共静态


i am making applications in c#.In that application i have one class as DataCapture.cs. In same application i have another class as Listner.cs . Here in Listner.cs class i want to use object of DataCapture.cs without creating new object of DataCapture.cs. As if i am creating new object of DataCapture.cs,i cant access the the data DataCapture.cs as it creates the new instance of class and all data gets lost as i am using collection in DataCapture.cs.Please help me.Thanks in advance.

解决方案

You vould use the the singleton pattern to ensure that only one object of the class exists. You could then get the object as,

DataCapture.Instance. ...... where .Inastance is public static.

这篇关于在另一个类中使用一个类的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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