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 =
cv.tKeyAccount =
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; }
}
You can also control access for getters and setters differently, or may properties read-only or write-only:
class CommonVariables {
internal string KeyTms { get; private set; }
internal string KeyAccount
{ get { return keyAccount * 2; } }
}
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