?
Solved

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

Posted on 2007-11-13
6
Medium Priority
?
3,198 Views
Last Modified: 2009-01-20
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
Comment
Question by:bemara57
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 10

Expert Comment

by:answer_me
ID: 20278255
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
 
LVL 10

Expert Comment

by:answer_me
ID: 20278258
Also you need to have two different variables for _test for two classes
0
 
LVL 3

Expert Comment

by:Aurora27
ID: 20278300
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:Vinod_nk
ID: 20278913
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
 

Author Comment

by:bemara57
ID: 20282369
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
 
LVL 8

Accepted Solution

by:
Vinod_nk earned 2000 total points
ID: 20287848
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

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question