CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2011
    Posts
    3

    Class Instantiation

    I have a case where the user has a choice in the database types they use, I've written 2 helper classes to handle the database work. What I am trying to do is created a member object that based on the database type they choose will get instantiated with the correct helper class.

    Kinda like this:

    public class myTestClass
    {
    private object m_objDbHelper;

    public myTestClass()
    {
    if (true == DbType.JET)
    {
    m_objDbHelper = new JetDbHelpers(......);
    }
    else
    {
    if (true == DbType.SQL)
    {
    m_objDbHelper = new SqlDbHelpers(......);
    }
    }
    }
    }

    public class JetBdHelper
    {
    // class has public methods & properties
    }

    public class SqlDbHelper
    {
    // class has public methods and properties
    }

    My goal is to be able to just create a instance of the appropiate class to work with the selected database type and access the public vars. I thought what I have above would work, obviously it doesn't which leads me to asking some folks about it.

    It's been about 7 years since I wrote any code at which point its what I did, so I am certainly rusty on it as well as green on the developments of c# in the past 7 years. any help anyone could offer I'd appreciate, even it its just a "go read about this topic" help. I just need a little pointer in this direction.

  2. #2
    Join Date
    Dec 2008
    Posts
    144

    Re: Class Instantiation

    The first thing that jumps out at me is that I don't see where you're testing an object for the DbType. You need to pass an appropriate object into your conditional logic and then test that to see if it is correct.

    Another issue is how you created your If conditionals. You should be testing more like this:
    Code:
    if(object == DbType.Jet)
    m_objDbHelper = new JetDbHelpers
    else if(object == DbType.SQL)
    m_objDbHelper = new SqlDbHelpers
    else
    //throw an exception... or not and just wrap all this in a try...catch
    I''m not certain if the GetType method will show the DbType so you'll need to look at how to check that.

    HTH

  3. #3
    Join Date
    Jan 2011
    Posts
    3

    Re: Class Instantiation

    The DbType is assignment is a system wide attribute returned from the appSettings class of the project, it gets validated there. The pasted code was just a simplified flow to give an idea of what I was trying to do.

    The main goal was to be able to define 1 set of helpers and not have to test every db call for the dbtype, the app is set to run on 1 type at a time. I was kinda thinking that if you had an object declared but not instantiated, when you new'd the object with the needed type you would get that new typed object.

    Thanks for replying.

  4. #4
    Join Date
    Jan 2011
    Posts
    11

    Re: Class Instantiation

    In the end, you'll have to code for both objects (JET and SQL) anyways. I've built an app, similar to what you're trying to do. I, also, only have 2 types of db(JET and SQL). I made a class called clsDatabase and have the constructor take in which db the new instance will use and store it in a member var. In my execute method, I just used a switch statement to check which db type it is and use the appropriate adapter for it.

    To use it (pseudo code)

    Code:
    static void Main()
    {
    	clsDatabase myDb = new clsDatabase(dbType.Sql);
    	myDatatable = myDb.Execute(sqlString);
    }

  5. #5
    Join Date
    Jan 2011
    Posts
    3

    Re: Class Instantiation

    Thanks Special K, in my readings this after noonon MSDn I've come to relalize using the object data type is more like a place holder to accept any type of object, however once you declare the type you cannot change it, so since I declared it as an "object", doesn't matter what I shove in there, I'm still dealing woth an "object" when it comes to going after properties and methods.

    The implementation I'm steering towards is similiar to yours, since I know the db type early on, this wont be as hard as I guess I was trying to make it.

    Thanks again for replying.

  6. #6
    Join Date
    Jan 2011
    Posts
    11

    Re: Class Instantiation

    Ya, you're right. You could shove anything in an object but when it comes to using the object for what it is, it's only gonna know that it's a "generic" object and you won't have access to your public properties/methods unless you cast it.

    Code:
    static void Main()
    {
    	Object objClass = new myClassA();
    	string ClassName;
    	
    	// Not accessible
    	ClassName = objClass.Name;
    	
    	// Accessible
    	// Output: Class A
    	myClassA curClassA = (myClassA)objClass;
    	ClassName = curClassA.Name;
    }
    
    class myClassA
    {
    	private string m_Name;
    
    	public myClassA()
    	{
    		m_Name = "Class A";
    	}
    	
    	public string Name
    	{
    		get { return m_Name; }
    	}
    }
    I tried to do a generic type so I won't have to code that much but ultimately, you're using 2 dif types of DB so you have to code both to complement both needs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured