Click here to Skip to main content
15,071,399 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

Project contains three classes one class for Access SQlScript and second class contains SQl server Query and third class contains Oracle Query class, I have to create the object of one of the class mentioned above according to the datasource selected by user it may be SQL Server or Access or Oracle. I don't want to take the abstract class or interfaces and override the method in the above mentioned class, is any other way to do this .

Can I use reflection or anything else.

Plz reply me
Thanks in advance

Sudhir Srivastava
Posted
Updated 12-Jul-11 22:04pm
v5
Comments
   
Gibberish. There are no non-runtime method of creation of class. It has nothing to do with abstract, override and other things you do not understand. You create instance using a class constructor, period. More detailed answer won't help (I don't even want to post a solution). Only one solution would work for you: take elementary manual on C# and .NET and read it all. Slowly. You have no idea how to program anything at all.
--SA
PrafullaVedante 13-Jul-11 4:03am
   
SAKryukov Do you have your stomach disturbed today or what ??
Why are you so arrogant ?? If you dont want to answer just seat on your chair and listen songs or watch tv..... there are many things in the world which you can do rather than spitting in Codeproject....

Get Well soon....
Sudhir Kumar Srivastava lko 13-Jul-11 3:15am
   
SAKryukov,i think u r not getting my question so plz read it carefully, i think u dont have idea about abstract class.
OriginalGriff 13-Jul-11 3:22am
   
It's possible that we don't understand what you are trying to achieve by doing this. Certainly, your question does not make it clear.
Try thinking about it, and then edit your question to try to explain to us (as if we were ten years old) what you want to actually do, without the buzzwords. It may be a language translation problem and that could help.
Use the "Improve question" widget to edit your question and provide better information.
Sudhir Kumar Srivastava lko 13-Jul-11 3:44am
   
is it clear this time?

The standard way of doing this would be to use something called the Provider pattern. Basically, your classes would be data access providers and you would use the inbuilt provider support in .NET to actually run the appropriate methods. Here's[^] a fair demonstration of how you can use it (and it's not limited to web apps - you can use it in desktop apps as well).
   
You can use reflection to create objects at runtime - however, reflection is performance intensive.
If you can void reflection and do late binding using interfaces, it might be faster.
   
v2
Comments
Sudhir Kumar Srivastava lko 13-Jul-11 3:37am
   
Acctually right now i am doing this way but i don;t want to use interfaces or abstract class.
You may have a look at Creational Design Patterns[^].
   
Following example might help you, I assume you would like to create object of a type based on some creation criteria at Runtime using REflection

C#
namespace RuntimeObjectCreation
{
    using System;
    using System.Collections.Generic;
    class Program
    {
        public static IDictionary<string, string> Selector = new Dictionary<string, string>()
        {
            {"A","RuntimeObjectCreation.ClassA"},
            {"B","RuntimeObjectCreation.ClassB"},
            {"C","RuntimeObjectCreation.ClassC"}
        };

        static void Main(string[] args)
        {
            SelectionClass sc = new SelectionClass();
            var @object = sc.GetSelectedObject(Selector["A"]);

        }
    }

    public class SelectionClass
    {

        public object GetSelectedObject(string typeName)
        {
            return Activator.CreateInstance(null, typeName);
        }
    }

    public class ClassA
    {
        public string AName { get; set; }
    }

    public class ClassB
    {
        public string BName { get; set; }
    }

    public class ClassC
    {
        public string CName { get; set; }
    }
}


Hope it helps :)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900