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:
I'm hoping to be able to do something like:Code: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 } }
My Types will be int, bool, string and double.Code:public void foo() { Column<int> myIntColumn = new Column<int>; myIntColumn.SetValue("31"); int myValue = myIntColumn.GetValue(); }
Is there a neat solution to the problem with the GetValue() member?




Reply With Quote