At least, I think that's what I think I want to wrap my head around ;-)
Brief problem statement: How can I write a method signature in a base class that returns a different type depending on the base class? In other words, I want all subclasses to have this method and return something, but what they return might be an int, float, etc. depending on the subclass.
More detailed:
I am writing some classes to talk to a SQL database. I'm desperately trying to avoid "one class per table" because there are lots of tables. There's only four possible data types (int, float, datetime, string), so I thought I'd do this:
- Define a base class called DBType:
- String columnName
- setValue() that would be overloaded
- int maxLength (for Strings)
- toSql() function that would return the appropriate value for a Parameter.Add() binding statement. i.e., it'd return an int, float, DateTime, or String depending on the subclass
- functions like insert(), update(), and delete(), or perhaps Save() (upsert) and Remove() (delete) - you get the basic idea
- Write four classes that extend this (DBT_Integer, DBT_Float, etc.). They would have constructors that would take care of additional specific defaults (string can only be so long, validation of date ranges, etc.)
Here's the problem, though...how do I write toSql() in the base class? What I'd like it to do is say "I'm going to return a value, but it could be an int, float, DateTime, or String. You won't really care because you'll call it like this: Parameter.Add("id",something.toSql()).
Or perhaps I've gone into some OOP design rabbithole and need to rethink things...
BTW, using an object database (like db4o or something) is not really an option in this case.



LinkBack URL
About LinkBacks


