如何访问两个类中的公共变量 [英] how to access a public variables in two classes

查看:82
本文介绍了如何访问两个类中的公共变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我有一个类来存储一些常见的变量。下面给出的类结构

Hi Friends,
I have a class to store some common variables. Class structure given below

public class CommonVariables
   {
       private string _tKeyTms;
       private string _tKeyAccount;
       public CommonVariables()// Default Constructor
       {
       }
       public CommonVariables(string Key_Tms, string Key_Account)//user Defined
       {
           this._tKeyTms = Key_Tms;
           this._tKeyAccount = Key_Account;
        }
       public string tKeyTms
       {
           get { return _tKeyTms; }
           set { _tKeyTms = value; }
       }
       public string tKeyAccount
       {
           get { return _tKeyAccount; }
           set { _tKeyAccount = value; }
       }
    }





在主类中我创建了这个类的对象。在其中一个方法中,我调用了类的重载构造函数。如下所示



In the main class i am creating an object of this class. In one of the method i am calling an overload constructor of the class. Like given below

public class UnitTest1
    {
 public CommonVariables GetCommonVariables = new CommonVariables();

 public void TestMethod1()
        {
           GetCommonVariables =new CommonVariables(tKeyTms, tKeyAccount);
         }
     }





我的要求是从另一个类访问变量。我怎样才能做到这一点。当我为 CommonVariables 创建一个新对象时,所有先前的值都丢失并产生空值





My requirement is to access the variables from another class. How can i do this. When i create a new object for CommonVariables all the previous values lost and resulting null values

public class FeeCalculation
   {
      public static CommonVariables cv = new CommonVariables();
          public static double CalculateSomething()
         {
           string tt=cv.tKeyTms;// Null
           string tq=cv.Key_Account;// Null
          }

   }

推荐答案

即使您的代码几乎都是正确的,但问题的表述看起来完全荒谬。您显式声明了两个类实例字段(这是这些成员的正确名称)为private,并询问如何访问两个公共变量。将它们公开并完全按照您的尝试进行访问;你的语法是正确的。



同时,允许访问任何字段通常被认为是糟糕的编码风格。通常,所有其他类型的成员都是私有的,内部的,公共的,受保护的或内部受保护的(您需要知道所有访问修饰符及其使用),但字段只是私有的。要将它们暴露给课外使用,可以将它们更改为属性,也可以将它们用作属性的后备字段。



在您的代码中,实际上使用了第二种方法,将字段与变量配对。不,你没有失去任何东西。默认情况下,所有引用类型成员都由null初始化。你通过 tKeyTms 和<得到正确的 _tKeyTms _tKeyAccount 对象code> tKeyAccount ;如果字段未初始化,则它们应为null。您可以在任何地方(在声明或构造函数中)初始化它们,然后您的属性将返回那些不为null的私有对象。此外,您可以通过属性初始化它们,因为您的属性是可读写的:

Even though your code seems to be nearly all correct, the formulation of the question looks like total absurd. You explicitly declared two class instance fields (this is a correct name of such members) as private and asking "how to access two public variables". Declare them public and access exactly as you tried; your syntax is correct.

At the same time, access to any field, being allowed, is often considered as poor coding style. Usually, all other kinds of members are done private, internal, public, protected or internal protected (you need to know all access modifiers and their use), but fields are only private. To expose them to out-of-class use, they can be changed into properties, or they can be used as backing fields for properties.

In your code, you actually used the second approach, paired the fields with variables. No, you did not lose anything. By default, all reference-type members are initialized by null. You get correct _tKeyTms and _tKeyAccount objects via tKeyTms and tKeyAccount; if the fields not initialized, they should be null. You can either initialize them anywhere (at the point of declaration or in constructor) and then your properties will return those private objects, which won't be null. Also, you can initialize them through the properties, as your properties are read-write:
cv.tKeyTms = // assign to some newly created object
cv.tKeyAccount = // same thing.





最后,因为你的财产吸气者和制定者不生产任何副作用,使用自动实现的属性更合理: https://msdn.microsoft.com/en-us/library/ bb384054.aspx



例如:



Finally, as your property getters and setters don't produce any side effect, it's more reasonable to use auto-implemented properties: https://msdn.microsoft.com/en-us/library/bb384054.aspx.

For example:

class CommonVariables {
    internal string KeyTms { get; set; }
    internal string KeyAccount { get; set; }
}



您还可以不同方式控制getter和setter的访问,或者可以是只读或只写属性:


You can also control access for getters and setters differently, or may properties read-only or write-only:

class CommonVariables {
    
    // private setter
    // (effectively, out-of-class read-only):
    internal string KeyTms { get; private set; }
 
    // read-only with backing field:                                              
    internal string KeyAccount
       { get { return keyAccount * 2; } } 

    // sorry, something wrong with code formatting...

}





您需要改进命名。不建议使用下划线;类名应该是单数,而不是复数,最好避免使用缩写。我建议你咨询微软的命名约定;它们非常合理,并且会得到FxCop的支持,我也建议这样做,但只是在某种程度上,小心使用它( http://en.wikipedia.org/wiki/FxCop )。



-SA


私有变量的整个概念是只有正确的实例才能访问这些值。

您将无法访问其他实例的私有变量。
The whole concept of private variables is that only the correct instance can access these values.
You will not be able to access private variables of another instance.


这篇关于如何访问两个类中的公共变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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