Can I somehow implement a method in a list item?
For example, if this is the class I base my list off of:
class TestListItem
{
public string Name;
public string Address;
public TestListItem(string name,string address)
{
Name = name;
Address = address
}
public int TestMethod(int i)
{
return i;
}
}
Then I'd like to be able to do the following:
public List<T> junk = new List<T>();
junk.AddNew(new TestListItem("blah","blah"));
junk[0].TestMethod(5); //Not allowed

3 answers
What you first need to do is to define an interface which has one member, ToByteArray().
GroupListItem and all the other classes that will be used with DeviceDataTable<T> will then need to implement this interface (no problem as they all have ToByteArray() methods anyway).
Finally, you need to impose a constraint on DeviceDataTable<T> whereby T implements the interface so that the compiler will know that it's OK to call ToByteArray() on an instance of T referenced within the generic class:
answered 2 years ago by:
17279
499
Thanks! That appears to be exactly what I am after... but (of course) I have found out that interfaces can't contain a field. I tried to add the field 'byte UID;' to the interface and that results in an error.
499
I've learned that I can't expose a field from an interface. Instead I must encapsulate the data as a property. Works well now.
You need to specify TestListItem as the type argument of the generic List. The following program works fine:
answered 2 years ago by:
17279
499
I can't format code in a comment so I'll post an answer...
Sorry... I cut some things out to simplify the post and I realize now that I shouldn't have. Here is what I have:
Above is the exact example. Notice that I have made DeviceDataTable<T> so that I could add methods to the generic list to make it suitable for my use. GroupListItem is just one class which will be used with my DeviceDataTable. However, all classes that will be used with DeviceDataTable<T> will support a method called ToByteArray(). Given that the DeviceDataTable<T> can be based on GroupListItem or any other class that I make... how can I implement a call to the GroupListItem ToByteArray method from within a particular instance of DeviceDataTable<>.
answered 2 years ago by:
499