Reference variable from a different method, class, file

后端 未结 4 1648
余生分开走
余生分开走 2021-01-29 10:54

I need to reference the value of a variable in a different method, class, and file than the one I am currently in. I am brand new to C# and still trying to get these concepts.

4条回答
  •  后悔当初
    2021-01-29 11:36

    In your case, you can set that variable as return parameter of your method:

    namespace Planning
    {
        public class Service
        {
            public bool AddRoute()
            { 
                bool variable = true;
                return variable;
            }
        }
    }
    

    Call from different class:

    namespace Planning
    {
        public class AnotherClass
        {
            public void DoSomething()
            {
                Service service = new Service();
                bool otherVariable = service.AddRoute();
            }
        }
    }
    

    Now value of your variable from AddRoute method is inside otherVariable in another class.

提交回复
热议问题