如何从一个类到另一个类访问变量值? [英] How to acess variable value from one class to another class?
本文介绍了如何从一个类到另一个类访问变量值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从一个类访问另一个类的字符串.我使用了如下的property方法-
I want to access a string from one class to another. I have used the property method as follows -
Myclass.cs
public class MyClass
{
private string _user;
public string user
{ get { return this._user; } set { this._user = value; } }
}
consumption.aspx.cs
我正在通过函数将值分配给用户
I am assigning the value to user in a function
MyClass m = new MyClass();
m.user = "abc"
现在,当我尝试在分配该值后调用的另一个函数中使用该值时
Now when I try to use this value in my another function which is called after this value is assigned
RawDal.cs
MyClass m = new MyClass();
string x = m.user;
我得到了空值...该怎么做?
I get empty value... How to do it?
推荐答案
您没有使用同一实例.试试
You are not using the same instance. Try
public class MyClass
{
private string _user;
public string user
{ get { return this._user; } set { this._user = value; } }
}
public string YourFunction()
{
MyClass m = new MyClass();
m.user = "abc"
return m.user;
}
如果您要返回的只是一个字符串,请尝试类似
If all you want to return is a string try something like
string x = YourFunction();
这篇关于如何从一个类到另一个类访问变量值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文