How do I persist data without global variables?

后端 未结 5 1029
傲寒
傲寒 2020-12-18 00:06

I\'m used to scripting languages. PHP, Javascript etc. and I\'ve written a few relatively simple Java and C# apps. This is a question I\'ve repeatedly needed an answer for,

5条回答
  •  眼角桃花
    2020-12-18 00:22

    Function A would be part of a class (call it C). Function A could then store the logon credentials and provide a function (or in C#, a property) to obtain the credentials. When they are required, you can simply use the property to obtain the stored credentials and pass them into Function B (on a different class).

    class C
    {
        public void functionA()
        {
             credentials = obtainCredentials;
        }
    
        private LogonCredentials _logonCredentials = null;
    
        public LogonCredentials logonCredentials
        {
            get { return _logonCredentials; }
        }
    }
    
    class D
    {
        public void functionB(LogonCredentials credentials)
        {
           //do stuff
        }
    }
    
    ///other stuff
    ///other function ...
        ...
        instanceC = C();
        instanceC.functionA();
        ///more stuff here
        instangeD = D();
        instanceD.functionB(instanceC.logonCredentials);
    

提交回复
热议问题