Solved

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

Posted on 2007-11-13
6
3,016 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
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 500 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now