Typically, if I want to pass an object to an instance of something I would do it like so...
Listing 1
File 1:
public class SomeClass
{
// Some Properties
public SomeClass()
{
public int ID
{
get { return mID; }
set { mID = value; }
}
public string Name
{
set { mName = value; }
get { return mName; }
}
}
}
public class SomeOtherClass
{
// Method 1
private void Method1(int one, int two)
{
SomeClass USER; // Create an instance
Squid RsP = new Squid();
RsP.sqdReadUserConf(USER); // Able to pass 'USER' to class method in different file.
}
}
Similar approach to listing 1 but I was unable to get it to work. What am I doing wrong here?
Listing 2 (this does not work, why?)
File 1:
private void SomeClass1
{
[snip]
TCOpt_fM.AutoUpdate = optAutoUpdate.Checked;
TCOpt_fM.WhiteList = optWhiteList.Checked;
TCOpt_fM.BlackList = optBlackList.Checked;
[snip]
private TCOpt TCOpt_fM;
TCOpt_fM.SaveOptions(TCOpt_fM);
}
File 2:
public class TCOpt:
{
public TCOpt OPTIONS;
[snip]
private bool mAutoUpdate = true;
private bool mWhiteList = true;
private bool mBlackList = true;
[snip]
public bool AutoUpdate
{
get { return mAutoUpdate; }
set { mAutoUpdate = value; }
}
public bool WhiteList
{
get { return mWhiteList; }
set { mWhiteList = value; }
}
public bool BlackList
{
get { return mBlackList; }
set { mBlackList = value; }
}
[snip]
public bool SaveOptions(TCOpt OPTIONS)
{
[snip]
Some things being written out to a file here
[snip]
Squid soSwGP = new Squid();
soSgP.sqdWriteGlobalConf(OPTIONS);
}
}
File 3:
public class SomeClass2
{
public bool sqdWriteGlobalConf(TCOpt OPTIONS)
{
Console.WriteLine(OPTIONS.WhiteSites); // Nothing prints here
Console.WriteLine(OPTIONS.BlackSites); // Or here
}
}
In this example, I was not able to use approach in Listing 1. Probably because Listing 1 passes an object between classes. Whereas, below, things are defined in a single class. I had to use some extra steps (trial & error) to get things to work. I am not sure what I did here or what its called. Is it good programming practice? Or is there is an easier way to do this (like in Listing 1).
Listing 3 (this works)
File 1:
private void SomeClass1
{
private TCOpt TCOpt_fM;
[snip]
TCOpt_fM.AutoUpdate = optAutoUpdate.Checked;
TCOpt_fM.WhiteList = optWhiteList.Checked;
TCOpt_fM.BlackList = optBlackList.Checked;
[snip]
TCOpt_fM.SaveOptions(TCOpt_fM);
}
File 2:
public class TCOpt:
{
public TCOpt OPTIONS;
[snip]
private bool mAutoUpdate = true;
private bool mWhiteList = true;
private bool mBlackList = true;
public bool AutoUpdate
{
get { return mAutoUpdate; }
set { mAutoUpdate = value; }
}
public bool WhiteList
{
get { return mWhiteList; }
set { mWhiteList = value; }
}
public bool BlackList
{
get { return mBlackList; }
set { mBlackList = value; }
}
[snip]
public bool SaveOptions(TCOpt OPTIONS)
{
[snip]
Some things being written out to a file here
[snip]
Squid soSwGP = new Squid();
soSwGP.OPTIONS = OPTIONS;
}
}
File 3:
[snip]
private TCOptions TCOpt_TCS;
public TCOpt OPTIONS
{
get { return TCOpt_TCS; }
set
{
TCOpt_TCS = value;
sqdWriteGlobalConf();
}
[snip]
public class SomeClass2
{
public bool sqdWriteGlobalConf()
{
Console.WriteLine(OPTIONS.WhiteSites);
Console.WriteLine(OPTIONS.BlackSites);
}
}
Thanks in advance,
XO