Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3593
  • Last Modified:

How to merge two separate classes into one (ASP.NET 2.0 / C#)?

I have two completely different classes setup and I would like to merge them into one, so with one object I can call values from either class data. Here's an example:

 public class ClassOne
    {
        private string _test;
        public string Test1
        {
            get { return _test; }
            set { _test = value; }
        }
    }

 public class ClassTwo
    {
        private string _test;
        public string Test2
        {
            get { return _test; }
            set { _test = value; }
        }
    }

To call the classes I do this:
ClassOne abc = new ClassOne();
ClassOne xyz = new ClassOne();
Response.Write(abc.Test1);
Response.Write(xyz.Test2);

What I want to do is create master class (ClassMaster) so I can do this:
ClassMaster mno = new ClassMaster();
Response.Write(mno.Test1);
Response.Write(mno.Test2);
0
bemara57
Asked:
bemara57
1 Solution
 
answer_meCommented:
This can be done by two methods:
1. Creating partial class ( if you want the code for both the existing classes to be in different places)
check this: http://msdn2.microsoft.com/en-us/library/wa80x488(VS.80).aspx
2. Merge the code of the two classes into one single class
0
 
answer_meCommented:
Also you need to have two different variables for _test for two classes
0
 
Aurora27Commented:
If you want to just get/set, why dont u have just one class /one variable or one class /two variables? That shud work fine, if the code u have gone is what its doing.

CT.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Vinod_nkCommented:
Hi,
You can have your Master class as below, which will give the result as the way you want. But I still wonder why would you want to do it, :-)

public class ClassMaster
{
  private ClassOne _classOne;
  private ClassTwo _classTwo;

  public ClassMaster()
  {
    _classOne = new ClassOne();
    _classTwo = new ClassTwo();
  }

  public string Test1
  {
            get { return _classOne.Test1; }
            set { _classOne.Test1 = value; }
  }

  public string Test2
  {
            get { return _classTwo.Test2; }
            set { _classTwo.Test2 = value; }
  }

}

Hope it helps.
Rgds
V
0
 
bemara57Author Commented:
Thanks. I'm glad you're curious because I don't think I'm going about it the right way- still a little new to this. Let me tell you what exactly I'm trying to do. I'm trying to pull settings for a website from web.config AND a database. Then I want to merge those settings into one master config setting class so I can reference any settings from either web.config or the database without creating or keeping track of two different classes instances.

This is the master I'm trying to set up:
public class ConfigSettings
{
        public ConfigSettings()
        {
               ??not sure yet??
        }
       
        private string _dog;
        public string Dog
        {
            get { return _dog; }
            set { _dog = value; }
        }

        private string _cat;
        public string Cat
        {
            get { return _cat; }
            set { _cat = value; }
        }
}

This is for web.config settings:
public class ConfigXML : ConfigurationSection
{
        public ConfigXML(string name)
        {
            (ConfigXML)WebConfigurationManager.GetSection(name);
        }

        [ConfigurationProperty("dog")]
        public string Dog
        {
            get { return (string)base["dog"]; }
            set { base["dog"] = value; }
        }
}

This is for the reset of the settings which are in a database:
public class ConfigDB : DataAccess
{
        public ConfigDB(string name)
        {
            ...run db queries to get cat value
        }

        [ConfigurationProperty("cat")]
        public string Cat
        {
            get { return (string)base["cat"]; }
            set { base["cat"] = value; }
        }
}

In my aspx.cs I do this:
SiteXML = new ConfigXML("Mary");
SiteDB = new ConfigDB("Mary");

So when I want to access dog, I have to do SiteXML.Dog, but if I want cat I have to do SiteDB.Cat.

What I want to be able to do is this from the aspx.cs page:
SiteSettings = new ConfigSettings("Mary");

Then access it like SiteSettings.Dog and SiteSettings.Cat.

I was thinking of maybe using multiple inheritance because ConfigXML and ConfigDB has to inherit ConfigurationSection and DataAccess respectively, then also inherit ConfigSettings to it. Not sure if I'm approaching things the right way. Can you help me get this right, or will your solution above work fine in this scenario?
0
 
Vinod_nkCommented:
ok,
Looks like what you want is a class which can read config values from config file as well as database. The following sample works with simple appsettings entry in config. If you are planning to user custom ConfigSections, then please post a sample data that will be in the config file, and I can have the sample code below changed.
So here I have made things simple and just have 2 class :

public class ConfigSettings
{
     ConfigDB _configDb;

        public ConfigSettings()
        {
               _configDb = new ConfigDB();
        }
       
        public string Dog
        {
            get { return System.Configuration.ConfigurationSettings.AppSettings["dog"]; }
         }

        public string Cat
        {
            get { return ConfigDB.GetValue("cat"); }
        }
}

public class ConfigDB
{
        private ConfigDB()
        {
        }

        public static string GetValue(string keyName)
        {
            /* Query the DB and return the value here */
        }
}

Rgds
V
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now