在C#中的不同类检查变量 [英] Checking variable from a different class in C#

查看:142
本文介绍了在C#中的不同类检查变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候 -



我有2个班。一个被称为程序,另一种是所谓的日志。所谓程序类有公共常量字符串m_sEnviron =; 上方附近,我需要检查什么m_sEnviron变量设置为通过我的类调用日志。变量m_sEnviron会从一个叫做潮汐,所以我怎么能检查由不同的类其值设置调度。如果这是不是最好要做到这一点,那么请让我知道什么是更好的方式是。



在此先感谢。



问候,

 命名空间NightScripts 
{
类节目
{

公共静态字符串m_sEnviron {搞定;设置;}

静态无效的主要(字串[] args)
{

}

//很多其他的功能..

}

级原木
{
//我试图让访问m_sEnviron,但它不会显示在我节目类型。
}
}


解决方案

好, m_sEnviron 不是的变量(/场) - 这是一个常量;它的总是



如果它是一个静态属性(或域),那么 Programs.m_sEnviron 。如果它是实例属性(或域),那么 someInstance.m_sEnviron 应该工作,因为它是公共 - 但我。将其重命名为



我的期望的你的意思是这是一个静态字段;其中的可以的工作,但你至少应该谨慎一点,这并不一定打得很好,如果你开始使用多线程等。而公共字段通常最好避免(喜欢私人领域和公共。属性)



例如:

 公共静态字符串ENVIRON {获取;设置;} 

将是一个公共的,静态属性作为交通方便Program.Environ


Greetings-

I have 2 classes. One is called "Programs" and the other is called "Logs". The class called Programs has public const string m_sEnviron = ""; near the top and I need to check what the m_sEnviron variable is set to through my class called Logs. The variable m_sEnviron will get set from a scheduler called Tidal so how can I check its value from a different class. If this is not the best to do this then please let me know what the better ways are.

Thanks in advance.

Regards,

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.
   }
}

解决方案

Well, m_sEnviron isn't a variable (/field) - it is a const; it is always "".

If it was a static property (or field), then Programs.m_sEnviron. If it was an instance property (or field) then someInstance.m_sEnviron should work, since it is public - but I would rename it.

I expect you mean it to be a static field; which can work, but you should at least be a little cautious that this doesn't necessarily play nicely if you start using multiple threads, etc. And public fields are generally best avoided (prefer private fields and public properties).

For example:

public static string Environ {get;set;}

would be a public, static property easily accessible as Program.Environ.

这篇关于在C#中的不同类检查变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆