Windows Develop Bookmark and Share   
 index > Windows Forms General > Dimming a class based on a choice
 

Dimming a class based on a choice

Hello all,

Here's my delma, I have an MSSQL database and a Sybase database that can be used with a spacific application, I have the .NET data providers for both and have created 2 classes based on their respective providers, the classes are identical in every respect except the provider from which they are based.

What I am looking to do is based on an option on startup instanciate the proper class. The problem I have is, what do I do with my base class? Here's an example.

Friend BaseVariable as SomeClass

If MSSQL = True then

BaseVariable = New MsSqlClass

Else

BaseValiable = New SyBaseClass

End If

The Friend variable needs to be instanciated as something to be avaliable to the rest of the application, should I instanciate one of my classes and then do like a redim, or is there a generic class that I should use.

Thanks in advance for your help.

Jim

c95se5m  Wednesday, February 01, 2006 8:52 PM

Interface is a way to described methods for a class without really implementing them.

I'll explain with an example:

First, we shall define the interface IDataProvider:

public interface IDataProvider

{

public void SetData(DataTable dataToSet);

public DataTable GetData();

}

As you can see, the definition is like class definition, only replacing the class with interface.

In the interface, I described 2 methods: SetData and GetData, the interface contains only the signature of the methods and not the implementation.

Now, I'm defining a class that implements this interface that uses MySql database:

public class MySqlProvider : IDataProvider

{

#region IDataProvider Members

public void SetData(System.Data.DataTable dataToSet)

{

// Fills the data base with the data table sent

}

public System.Data.DataTable GetData()

{

// Returns the data table from the data base

}

#endregion

}

And the other implemention:

public class SyBaseProvider : IDataProvider

{

#region IDataProvider Members

public void SetData(System.Data.DataTable dataToSet)

{

// Fills the data base with the data table sent

}

public System.Data.DataTable GetData()

{

// Returns the data table from the data base

}

#endregion

}

Offcourse, you can write what ever you want in this classes: methods memebers etc, but the GetData and SetData methods must be defined.

Now, to use this interface,

We declare the interface:

IDataProvider provider;

We use the implementaion we need based on a term:

if(MSSQL == true)

provider = new MySqlProvider();

else

provider = new SyBaseProvider();

And we can call the methods:

provider.SetData(...);

provider.GetData();

when we need them, the work will be done acording to the implementaion that was defined.

I requmened you to read about interface, it can help you a lot, also, check out the abstract class, that is like inerface but gives the option to write some code on the base class (that is common to all imlementions).

Eli Gazit  Wednesday, February 01, 2006 10:18 PM

What about using interface?

Eli Gazit  Wednesday, February 01, 2006 8:57 PM
Would you have an example of doing it that way? I am kinda new to interfaces and am not sure what need to me done...
c95se5m  Wednesday, February 01, 2006 9:46 PM

Interface is a way to described methods for a class without really implementing them.

I'll explain with an example:

First, we shall define the interface IDataProvider:

public interface IDataProvider

{

public void SetData(DataTable dataToSet);

public DataTable GetData();

}

As you can see, the definition is like class definition, only replacing the class with interface.

In the interface, I described 2 methods: SetData and GetData, the interface contains only the signature of the methods and not the implementation.

Now, I'm defining a class that implements this interface that uses MySql database:

public class MySqlProvider : IDataProvider

{

#region IDataProvider Members

public void SetData(System.Data.DataTable dataToSet)

{

// Fills the data base with the data table sent

}

public System.Data.DataTable GetData()

{

// Returns the data table from the data base

}

#endregion

}

And the other implemention:

public class SyBaseProvider : IDataProvider

{

#region IDataProvider Members

public void SetData(System.Data.DataTable dataToSet)

{

// Fills the data base with the data table sent

}

public System.Data.DataTable GetData()

{

// Returns the data table from the data base

}

#endregion

}

Offcourse, you can write what ever you want in this classes: methods memebers etc, but the GetData and SetData methods must be defined.

Now, to use this interface,

We declare the interface:

IDataProvider provider;

We use the implementaion we need based on a term:

if(MSSQL == true)

provider = new MySqlProvider();

else

provider = new SyBaseProvider();

And we can call the methods:

provider.SetData(...);

provider.GetData();

when we need them, the work will be done acording to the implementaion that was defined.

I requmened you to read about interface, it can help you a lot, also, check out the abstract class, that is like inerface but gives the option to write some code on the base class (that is common to all imlementions).

Eli Gazit  Wednesday, February 01, 2006 10:18 PM

I would like to thank you 100 times over for this information, you hit the nail on the head of what I have to do, and the example was fantastic.

Thank you again.

Jim

c95se5m  Wednesday, February 01, 2006 11:09 PM

Glad I could help.

Again, I think that the abstract way is even better for you (I guess you have some methods that are exactly the same on both implementions).

Eli Gazit  Wednesday, February 01, 2006 11:27 PM

You can use google to search for other answers

Custom Search

More Threads

• Where does title text come from, for grouped buttons on taskbar?
• How do I Highlight the contents of a numericupdown after calling Focus()?
• Outlook Express Problems (WAN)
• Read Data from a Text file being retrieved from a Textbox control
• DataGridView with row specific combo box contents
• Visual studio type tooltip
• WYSIWYG HTML Editor
• user access control
• How do I serialize SortedList
• Hook shortcut key over the system