How to acess variable value from one class to another class?

后端 未结 2 1516
梦如初夏
梦如初夏 2021-02-06 12:59

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;
           


        
2条回答
  •  孤城傲影
    2021-02-06 13:13

    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();
    

提交回复
热议问题