在C#中检查来自不同类的变量

Jos*_*osh 2 c#

问候-

我有2节课.一个称为"程序",另一个称为"日志".名为Programs的类public const string m_sEnviron = "";接近顶部,我需要通过名为Logs的类来检查m_sEnviron变量的设置.变量m_sEnviron将从名为Tidal的调度程序中设置,因此如何从其他类检查其值.如果这不是最好的,请告诉我更好的方法.

提前致谢.

问候,

Namespace NightScripts
{
   class Program
   {

      public static string m_sEnviron {get; set;} 

      static void Main(string[] args)
      {

      }

      //Lots of other functions...

   }

   class Logs
   {
      //I try to get access to m_sEnviron but it will not show after I type Program.
   }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 7

好吧,m_sEnviron 不是变量(/ field) - 它是一个const; 它总是如此 "".

如果它是静态属性(或字段),那么Programs.m_sEnviron.如果它是一个实例属性(或字段),那么someInstance.m_sEnviron应该工作,因为它是public- 但我会重命名它.

希望你的意思是它是一个static领域; 哪个可以工作,但你应该至少有点谨慎,如果你开始使用多个线程等,这不一定很好.通常最好避免使用公共字段(更喜欢私有字段和公共属性).

例如:

public static string Environ {get;set;}
Run Code Online (Sandbox Code Playgroud)

将是一个易于访问的公共静态属性Program.Environ.