Question Can the T of generics be an interface?

nicorvp

Member
Joined
May 3, 2009
Messages
24
Location
Italy
Programming Experience
5-10
Hi, on my program I have an interface iGraphable that contains two properties: Abscissa and Ordinate. Then I have an xxxx class (actually more than one) implementing iGraphable and a ListOfxxxx class implementing BindingListView(Of xxxx).
To draw graphs I have a Graph class with a property called Data whose type is BindingListView(of iGraphable).
Why have I a cast exception when I pass a BindingListView(Of xxxx) to the Data property.
Hope I was clear
 
You'd need a to pass a BindgingListView(Of IGraphable) that contains lots of XXX classes (that would be boxed up as igraphables when you pull them out) to the property:

VB.NET:
Dim x as New BindingListView(Of IGraphable)
x.Add(myXXXX)
x.Add(myYYYY)

some.Property = x

MsgBox(someProperty(0).Abscissa)

ps; where did you get the BindingListView(Of xxxx) generic for the type of your property?

pps; you've asked if T in Generic(of T) can be an interface, but then your question implies youre not doing this - youre actually making your own concrete List class that implements an interface and are expecting to pass it to a generic that is storing the interface? To the framework, these will be 2 different things
 
Last edited:
Thanks cjard, now everything is working passing to the Data property a variable whose type is BindingListView(Of iGraphable).

I found BindingListView(Of T) somewhere over Internet a lot of time ago, but I cant remember where. I have used it a lot

About your pps: - sorry, but I'm not so smart with the framework (nor with english!) - I was doing like that:
graph1 class:implements iGraphable + ListOfGraph1 class: inherits BindingListView(of Graph1) class
graph2 class:implements iGraphable + ListOfGraph2 class: inherits BindingListView(of Graph2) class
.....
then I was trying to pass to the Data property (BindingListView(of iGraphable) type) an istance of ListOfGraph1 (or 2, or ...) .
Wasn't I passing a class implementing an interface to a variable (T) whose type is the interface?
Why ListOfGraphx and Data are different things?
 
Last edited:
... and how to serialize BindingListView(Of IGraphable)?
I use it in a class where:
VB.NET:
    Public Property ListOfLoads() As BindingListView(Of iGraphable)
        Get
            Return _listOfLoads
        End Get
        Set(ByVal value As BindingListView(Of iGraphable))
            _listOfLoads = value
        End Set
    End Property

Without the attribute XmlIgnoreAttribute on ListOfLoads I get an InvalidOperationException from XmlSerializer.
I would expect to have serialized at least the interface properties. But how?
 
Back
Top