Click to See Complete Forum and Search --> : Generic(s) question


rliq
June 8th, 2009, 08:42 PM
Another class has a collection of ColumnBase items in it. I want my Columns to contain different Types (literally) of data. Analogous to an SQL database. The data for the column always originates from a string (one time only). But once the data is in the column, I want it to appear to be the correct Type. So far I have:

public abstract class ColumnBase
{
protected string _value;

public string SetValue(String value)
{
_value = value;
}
}

public class Column<T> : ColumnBase
{
public T GetValue()
{
return (T)_value; // won't allow conversion to T from string
}
}

I'm hoping to be able to do something like:

public void foo()
{
Column<int> myIntColumn = new Column<int>;

myIntColumn.SetValue("31");

int myValue = myIntColumn.GetValue();
}

My Types will be int, bool, string and double.
Is there a neat solution to the problem with the GetValue() member?

dglienna
June 8th, 2009, 09:41 PM
Download the samples herehttp://code.msdn.microsoft.com/vbsamples/ and look for the Generics Language Sample

JonnyPoet
June 9th, 2009, 01:12 PM
May I ask you whats behind the idea to have a generic class that converts from string to anywhere? Looks me to be a bit a sort of trying to handle a design fault you may have by trying to create unusual handlings. Converting from a string to any numeric valuetype cannot be done that way, because it would create problems. What would happen if the input is "Hallo world" How would this string get converted in your case?
So my question is: Whats the basic problem you want to handle that way?

rliq
June 9th, 2009, 06:28 PM
Thank you for the input...

I create the original data (via another application), so I know the data will always be in the correct format.

I was thinking though (wrongly) that the code would compile, just that at runtime I would get an exception, in the case of (Int32)"Hallo World", and be able to catch it.

It's a Silverlight Application. The original data is in XML on my web site and this is part of a DLL that will allow me to do things like:


Database myDB = new Database("DatabaseName");
mymB.Synchronise();


I have it all working fine, it's just that when I access the data from one of the columns in one of the rows in one of the tables in my database I have to cast it to the correct type. I wanted the cast to be in the DLL, so that somebody using it (me) does not have to worry.

I notice that Microsoft Datasets get around this by generating lots of hard-coded stuff based on the data type (which is read from the database in the case of SQL). That makes me think that it cannot be done easily.

BigEd781
June 9th, 2009, 07:30 PM
usually systems like this simply return the type "object" and let the caller do the casting. The whole point of generics (from the generic class's perspective) is that it does not need to know anything about the type at all.

rliq
June 9th, 2009, 09:14 PM
As the caller must obviously know the data type, I think I will just provide member functions in my Column class eg:

public class Column
{
String _theValue;

public Int32 Int32Value
{
get { return Convert.ToInt32(_theValue); }
}

public Boolean BooleanValue
{
get { return Convert.ToBoolean(_theValue); }
}
// etc
}


These functions will throw a runtime exception if the 'wrong' one is called and I'm only supporting a few data types...

BigEd781
June 10th, 2009, 02:30 AM
Why not just something like this? The caller can cast the return value and it makes for a lot less code.


class Column
{

public object GetValue { ... }

}

rliq
June 10th, 2009, 06:13 PM
Thanks Ed. I think that is slightly better. And in my book, slightly better is still better... :)
Rob.