Thread: Trying to wrap my brain around generics...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    12

    Trying to wrap my brain around generics...

    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.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    I am thinking you want something like this?
    Code:
        public class GenericClass<T>
        {
            public virtual T ReturnValue()
            {
                return default(T);
            }
        }
    
        public class IntClass : GenericClass<int>
        {
            public override int ReturnValue()
            {
                return 10;
            }
        }
    Woop?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    why not just try LINQ to SQL or the Entity Framework? it does all this for you.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Why not just return object and call AddWithValue instead?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Left Brain v Right Brain
    By stevesmithx in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-30-2008, 09:13 AM
  2. Word Wrap
    By sethjackson in forum Windows Programming
    Replies: 4
    Last Post: 09-21-2005, 04:35 PM
  3. Can't wrap my brain around this:
    By swayp in forum C++ Programming
    Replies: 3
    Last Post: 01-29-2005, 02:57 PM
  4. brain vs. computer (brain wins hands down)
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 09-17-2003, 08:41 PM
  5. Re: Girlfriend Post
    By PsychoBrat in forum A Brief History of Cprogramming.com
    Replies: 57
    Last Post: 05-13-2002, 06:11 AM