First off,
don't expose your backing fields!
Either use an automatic property:
public String StrSuperPatID { get; set; }
or declare the backing field as
private
class dbConnect
{
private String strSuperPatID;
public String StrSuperPatID
{
get { return strSuperPatID; }
set { strSuperPatID = value; }
}
To use the getter and setter of the class instance is easy:
public partial class frmFulBldCuntDet : Form
{
private dbConnect dbConn = new dbConnect();
private void MyMethod()
{
dbConnect.StrSuperPatID = "Hello World!";
...
Console.WriteLine(dbConnect.StrSuperPatID);
}
}
"It didn't help me to solve the problem. However I have placed the modified question as the solution 02 and hope it will describe my problem"
You are aware that C# doesn't have global variables? That everything is contained within a class? So if you want to access StrSuperPatID from a different class, you either need to access them via a class instance as shown above or make them
static
within the dbConnect class:
class dbConnect
{
private static String strSuperPatID;
public static String StrSuperPatID
{
get { return strSuperPatID; }
set { strSuperPatID = value; }
}
}
And when I said "
don't expose your backing fields!" why did you continue to do so? It defeats the purpose of having a property in the first place if the outside world can just modify the backing store directly!
"You have mentioned that "don't expose your backing fields!" does make it any sense that this is about encapsulation? Or other thing? can you explain it to me?"
Yes, it is about encapsulation!
When you declare a property, you are actually declaring two methods: the getter and the setter.
string myString;
public string MyString
{
get { return myString; }
set { myString = value; }
}
Is the equivalent of:
string myString;
public string GetMyString() { return myString; }
public void SetMyString(string value) { myString = value; }
The compiler adds some syntactic sugar to make them easier to use:
MyString = "hello";
Console.WriteLine(MyString);
Instead of having to write:
SetMyString("hello");
Console.WriteLine(GetMyString());
And properties look like "normal" variables, but they add the capability to validate data, put it into controls for display, or use something totally different instead - all without the user knowing about it.
When you expose your backing field (and that is just the name given to the class level variable you actually store the property value in:
myString
in the example I gave, and to
expose
it means to make it available outside the class by making it
public
for example) you let the user circumvent the mechanism altogether. If your property setter validates a string so that is it always a correct and valid UserName for example, then exposing the backing field lets the outside world bypass that validation and change the value to an invalid UserName without your class knowing about it. So you program falls over because your other code relies on the validation to ensure it doesn't need to check the value every time it uses it.
So if you declare both
strSuperPatID
and
StrSuperPatID
as
public
then there is no point in having the property in the first place because the world outside your class doesn't have to use it.
Make your backing fields
private
and the outside world can't touch 'em except via the property where you control what happens! :laugh:
"Ok pal then what should I do? Please state the best practice! Thank You!"
Either you need the common variables to be static (and then it has a single instance, accessed via the class name rather than a variable) or you want the Parent (form1) do do the work. Personally, I would go with the parent in most cases, but in this specific case I would make the CommonVariable class static and use an automatic property (since you do nothing else with it than store the value).
public static class CommonVariables
{
public static string StrSuperPatID {get; set;}
}
You then access it via the class name.
Form1:
private void button1_Click(object sender, EventArgs e)
{
CommonVariables.StrSuperPatID = strYear + strMonth + strID;
frmHBAIC hbi01 = new frmHBAIC();
hbi01.Show();
}
and Form2:
private void button1_Click_1(object sender, EventArgs e)
{
label1.Text = CommonVariables.StrSuperPatID;
}
Without trying to declare a variable of the class CommonVariables in either form.