Passing Objects between different files

Posted by user309779 on Stack Overflow See other posts from Stack Overflow or by user309779
Published on 2010-04-06T08:03:57Z Indexed on 2010/04/07 3:03 UTC
Read the original article Hit count: 275

Filed under:
|
|

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.
    }  
}  

In this example, I was not able to use the above approach. Probably because the above example 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 above).

Listing 2
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
    }  
}  

Thanks in advance,
XO

© Stack Overflow or respective owner

Related posts about c#

Related posts about objects