在另一个类中重用变量 [英] Reuse variable in another class

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

问题描述



我的程序中有两节课.在我的first.class中,我在变量中定义了一些值,请参见下面的代码:

var x= System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
var y= Path.GetFileName(x);

MessageBox.show(x);




如何在我的second.class中使用这些值?

在此先感谢

解决方案

如果这些值是应用程序中大多数类将使用的值,则这些值必须是基类和所有类的一部分必须从中继承.

如果不是,则将其设置为public static属性,其他类可以使用它们. >
并使用first.xfirst.y


调用它们.在second.class中创建first.class的实例,并在second.class中使用first.class的属性


Hi,

I''ve two classes in my program. In my first.class I defined some value in variables, see code below:

var x= System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
var y= Path.GetFileName(x);

MessageBox.show(x);




How can I use those values in in my second.class ??


Thanks in advance

解决方案

If those values are something that most of the classes in your application will use, then these must be a part of a base class and all the classes must inherit from it.

If not, then make it public static property and other classes can use them.


lets assume your first class name is first, all you have to do is to declare those variables as public
and call them using first.x and first.y


Create an Instance of first.class in second.class and use the properties of first.class in second.class


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

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