Solved

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

Posted on 2007-11-13
6
3,065 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
2 questions 10 25
EF5: Question about Metadata Artifact Processing 4 15
Getting error in connectionstring with Excel. 30 30
ASP.NET MVC identity 6 26
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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