chess123mate
Member
In a program I am writing (in VB.NET, 2008), I have 2 classes, "Receipt" and "Group", each with similar properties, both implementing a custom interface entitled "IIDUser".
In my program, I have a number of lists of "Receipt" and "Group", declared like:
Friend receipts as List(Of Receipt)
I have written a few functions that apply equally to managing both lists of receipts and groups, declared like this one:
Friend Function getNextAvailableUserLabel(ByRef theList As IList(Of IIDUser), ByVal startLabel As Short) As Short
Then called like this:
getNextAvailableUserLabel(receipts, s)
I did all this in an attempt for interface-based polymorphism, as I thought writing 2 identical functions (except for that one would be for lists of receipts while the other for lists of groups) would be bad practice (and waste time).
However, I get an "InvalidCastException" during runtime. It is explained, "Unable to cast object of type 'System.Collections.Generic.List`1[Receipt_Sorter_v2.Receipt]' to type 'System.Collections.Generic.IList`1[Receipt_Sorter_v2.IIDUser]'."
If I declare the function to just be a "List(Of IIDUser)", it complains "Value of type 'System.Collections.Generic.List(Of Receipt_Sorter_v2.Receipt)' cannot be converted to 'System.Collections.Generic.List(Of Receipt_Sorter_v2.IIDUser)'." in the error list.
Can anyone tell please me what I'm doing wrong and how to fix this?
Thanks,
chess123mate
In my program, I have a number of lists of "Receipt" and "Group", declared like:
Friend receipts as List(Of Receipt)
I have written a few functions that apply equally to managing both lists of receipts and groups, declared like this one:
Friend Function getNextAvailableUserLabel(ByRef theList As IList(Of IIDUser), ByVal startLabel As Short) As Short
Then called like this:
getNextAvailableUserLabel(receipts, s)
I did all this in an attempt for interface-based polymorphism, as I thought writing 2 identical functions (except for that one would be for lists of receipts while the other for lists of groups) would be bad practice (and waste time).
However, I get an "InvalidCastException" during runtime. It is explained, "Unable to cast object of type 'System.Collections.Generic.List`1[Receipt_Sorter_v2.Receipt]' to type 'System.Collections.Generic.IList`1[Receipt_Sorter_v2.IIDUser]'."
If I declare the function to just be a "List(Of IIDUser)", it complains "Value of type 'System.Collections.Generic.List(Of Receipt_Sorter_v2.Receipt)' cannot be converted to 'System.Collections.Generic.List(Of Receipt_Sorter_v2.IIDUser)'." in the error list.
Can anyone tell please me what I'm doing wrong and how to fix this?
Thanks,
chess123mate