Solved

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

Posted on 2007-11-13
6
3,045 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

14 Experts available now in Live!

Get 1:1 Help Now