Click here to Skip to main content
15,894,097 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi Friends,
I have a class to store some common variables. Class structure given below
C#
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
C#
public class UnitTest1
    {
 public CommonVariables GetCommonVariables = new CommonVariables();

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


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

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

   }
Posted

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:
C#
cv.tKeyTms = // assign to some newly created object
cv.tKeyAccount = // same thing.


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:
C#
class CommonVariables {
    internal string KeyTms { get; set; }
    internal string KeyAccount { get; set; }
}

You can also control access for getters and setters differently, or may properties read-only or write-only:
C#
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...

}


You need to improve your naming. Underscores are not recommended; the class name should be singular, not plural, abbreviations are best avoided. I would advise you to consult Microsoft naming conventions; they are very reasonable and will be supported by FxCop, which I also would recommend, but only to certain extent, using it with care (http://en.wikipedia.org/wiki/FxCop).

—SA
 
Share this answer
 
v5
Comments
Abhinav S 6-Apr-15 22:33pm    
5.
Sergey Alexandrovich Kryukov 7-Apr-15 0:33am    
Thank you, Abhinav.
—SA
jinesh sam 8-Apr-15 9:57am    
Sorry for late reply. Lots of thanks for your detailed explanation
Sergey Alexandrovich Kryukov 8-Apr-15 10:15am    
No problem with timing here, ever.
You are very welcome.
Good luck, call again.
—SA
jinesh sam 8-Apr-15 10:28am    
Can you please help me on this Question
http://www.codeproject.com/Questions/894889/How-to-merge-cells-in-SheetData-How-to-Delete-One?arn=0
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.
 
Share this answer
 
Comments
jinesh sam 6-Apr-15 9:31am    
I need to access only public varaibles
Sergey Alexandrovich Kryukov 6-Apr-15 10:35am    
Members, private members, in your case, properties. Your properties are public. You are trying to break through the open door. Please see my answer.
I cannot understand where this absurdity comes from. You explicitly declared some fields private and asking about access to public. There is no issue. What you do is correct, only could be better.
—SA
Sergey Alexandrovich Kryukov 6-Apr-15 10:37am    
Countered vote of 1. Please see my answer.
—SA
Abhinav S 6-Apr-15 22:33pm    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900