I would like to create a interface that I can use with different types. Like a table where columns may be of different types eg Integer, String, Date.
The following articles have been of great help.
ListBox, list of different objects implementing the same interface
List of two different Types in C#
The problem I have is that the i still need a common interface that can handle different types of data. How can I get around this?
Could I just have the main Interface store as a string and have all types converted to and from?
interface IMytype
{
string Item { get; set; }
}
class Myint : IMember
{
public int Item {
get; // code here IMytype.Item (get) then convert to int
set; // code here to convert to string then IMytype.Item (set)
}
}
Example
IMytype testint = new MyInt();
testint.Item = 10;
List<IMytype> testlist = new List<IMytype>();
testlist.add(testint);
开发者_JS百科
Am I on the right track?
An interface is a contract that states what behaviour that class will provide. It does should not specify what data the class will hold.
You wish to build up a list of objects that conform to a particular contract - you want them all to behave in a particular way. You are getting confused by the fact that these classes will all have a different internal representation - some represent ints, others may represent floats etc..
You need to step back, and decide what shared behaviour you want these classes to implement. This will form the basis of your interface.
So you want to be able to get a String representation of the data. This is one method for your interface.
You want the class to be able to receive a string and convert that to its internal type? Theres another method.
Do these types need to Wangle a Flux Capacitor?
There's your interface.
interface IMytype
{
string GetStringValue ();
void ReadString ( string value);
bool WangleFluxCapacitor();
}
Think about the shared behaviour first.
精彩评论