views:

106

answers:

1

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

+1  A: 

At first it seems that you have a completely false picture on how OOP works (or hopefully just the wrong words).

You don't pass object between files. You pass them between classes. Normally you would put one class into one file and in that case your description would be right. But nevertheless you can put multiple classes into one file and you can also spawn one class over multiple files (by using the partial keyword).

So to be completely correct, you also don't pass classes. You pass instances of classes. And the problem in your example is that you declare an object of a specific class, but you don't instantiate it:

private void Method1(int one, int two)  
{  
    SomeClass USER;  // This doesn't create an instance!
    USER = new SomeClass(); //This creates an instance.

    //SomeClass USER = new SomeClass(); //Can also be written as one-liner.

    Squid RsP = new Squid();  
    RsP.sqdReadUserConf(USER); // Able to pass 'USER' to class method in different file.
}  

Update

After reading your comment i looked into your second and third example. Maybe it is just a typo again in Listing 2 File 1, but cause you changed it in Listing 3 File 1 i think there must somewhere your problem:

private void SomeClass1 
{  
    [snip]  
    TCOpt_fM.AutoUpdate = optAutoUpdate.Checked;  
    TCOpt_fM.WhiteList = optWhiteList.Checked;  
    TCOpt_fM.BlackList = optBlackList.Checked;  
    [snip]  

    //It is impossible to declare here a variable name that is already used
    //above.
    //But it looks like you just declared here a variable (without instantiation)
    //and trying to use it as parameter for the SaveOptions() function.
    private TCOpt TCOpt_fM;  
    TCOpt_fM.SaveOptions(TCOpt_fM);  
}

Normally this will lead to a couple of error messages.

  • declaring a variable name that is already used in the same scope
  • trying to pass an object that is not initialized
    • (If you like to pass a not initialized object you have to initialize it with null or use the out keyword
Oliver
Wow, I did mangle some things up- my apologies. A years experience in OOP ' and the comment was on the wrong line. And yes I did mean passing objects between classes (not files) and 'passing instances of a class'. Thanks for setting me straight here :). I categorized the code into 'Listings' for easy reference. Left the bad nomenclature in tact. 1. What approach am I using in Listing 3? specifically this:soSwGP.OPTIONS = OPTIONS; 2. Why doesn't Listing 2 work?
XO
Yes typo, thanks again- too funny. Very happy now, both listings are working. Thanks for walking me through this and clarifying my terminology confusion. What's weird is, I was compiling a variation of Listing 2 (variable in proper place) but it was not printing values- oh well. Listing 2 is passing an Object. What's Listing 3 referred to as --> 'Squid soSwGP = new Squid(); soSwGP.OPTIONS = OPTIONS;'? It involves a couple of extra steps by placing properties in File 3, but it works nicely when I use it to pass values to pupulate fields in a form.
XO
I spoke to soon. I now understand what you mean by your 2nd sub-bullet (thanks for being complete in your answers). TCOpt_fM is not initialized yet. null as in 'private 'TCOpt TCOpt_fM = null'? I tried this, but was still unable to assign anything to it in File 3. I will check over my code in the meantime.
XO
Fixed it. The object wasn't null after all. However, your tip will be prove to be valuable going forward. I wasn't passing the object along from the starting class to the final class. Another marathon day. I'll check back to on your reply to question comment 2. And again, thanks!
XO