Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Ok here is my basic problem; I'm creating a program that can interact with various databases. My idea was to have a base class with stuff needed by all the databases, such as the database type (so i can identify what database it should be), username, password etc etc.

Then I have all the classes which inherit these, such as an oracle, sql server etc classes.

 

Now, my plan is to assign these database connections to a toolbar button; so i have extended the toolbar button class to include a member which is the base class of the databases.

 

My problem is this; I need to go from the base class and cast it to the actual database class.... so i wanted to return the database type from the base class, then on that result cast it. The problem is this gives: "An unhandled exception of type 'System.InvalidCastException' occurred in test.exe".

 

It seems the problem is you can cast from the derived class to the base class, but not the other way round.

 

Any idea how I would do fix this?

Edited by Goksly
Posted

Ok one way I have thought is, as all the classes are database related, they will roughly have all the same functions. So if I make the base class have all virtual functions, and all the derived classes having the same functions but overriding them - that should work fine and no matter what, casting the object assigned to the toolbar button to the base class and then calling the once empty, but now overridden base class functions.

 

That a good way, or some cowboy style?

Posted

That sounds fine to me. With my database setup I have an interface displaying the methods and implement them all differently for different database types. I then declare my variable as IData but assign it to New SQLServerData or New AccessData depending on the db in use.

 

The above is basically equivalent but with some base functionality built in too.

 

:)

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Posted

Mark,

I'm pretty new to this .Net stuff so I'd like to just go over that and make sure ive understood you. You create an interface (blank class with method headings but no code - forcing classes that use it to have those methods).

How do you know what database is in use? And with your method can the the SQLServerData and AccessData have different methods?

 

Thanks :)

  • *Experts*
Posted
Why would you really need to know what kind of a database you use beside of course deciding during instantination? This is the beauty of abstraction that you can have :). You could force the class to return some form of an identification, like a string using the interface if you need it. And yes, you can add different methods to a class that implements an interface, but you just wouldn't be able to access it directly without casting. But then again, if you need many different public methods for different databases (which shouldn't really happen) then this level of abstraction might not help you.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...