Generic Class Of T conversion

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

Folks, I have two classes with IEnumerators which differ by Type and
The Function that is used to access data. I want to make this a
generic class:

Public Class WildcatData(Of [T])
Implements IEnumerator, IEnumerable

private tid as integer = 0
private keysort as integer = 0
private data as [T]

Public Sub New(optional key as integer = 0)
KeySort = key
End Sub

Public Function GetEnumerator() As IEnumerator _
Implements IEnumerable.GetEnumerator
Reset()
Return CType(Me, IEnumerator)
End Function

Public Function MoveNext() As Boolean _
Implements IEnumerator.MoveNext
if TypeOf(data) is TUser then
Return GetNextUser(keysort, data, tid)
end if
if TypeOf(data) is TFileRecord then
Return GetNextFile(keysort, data, tid)
end if
End Function

Public Sub Reset() Implements IEnumerator.Reset
tid = 0
End Sub

Public ReadOnly Property Current() As Object _
Implements IEnumerator.Current
Get
Return data
End Get
End Property

End Class

The compiler is issuing errors:

error BC30311: Value of type 'T' cannot be converted to 'TUser'.

Return GetNextUser(keysort, data, tid)
~~~~
error BC30311: Value of type 'T' cannot be converted to 'TFileRecord'.

Return GetNextFileRec(keysort, data, tid)
~~~~
I tried various CType, DirectCast ways but it all comes down to the
same type conversion error. How do I convert these?

Or is there a more better way to generalize that MoveNext() methods so
it isn't locked to these two types?

Thanks
 
Might be another way, but I got it like so:

Public Function GetNext( _
byval key as integer, _
byref data as object, _
byref tid as integer) as boolean
if TypeOf(data) is TUser then
Return GetNextUser(key, data, tid)
end if
if TypeOf(data) is TFileRecord then
Return GetNextFileRec(key, data, tid)
end if
throw new Exception ("Invalid GetNext Type")
end function

and in MoveNext()

Public Function MoveNext() As Boolean _
Implements IEnumerator.MoveNext
Return GetNext(keysort, data, tid)
End Function
 
Might be another way, but I got it like so:

Public Function GetNext( _
byval key as integer, _
byref data as object, _
byref tid as integer) as boolean
if TypeOf(data) is TUser then
Return GetNextUser(key, data, tid)
end if
if TypeOf(data) is TFileRecord then
Return GetNextFileRec(key, data, tid)
end if
throw new Exception ("Invalid GetNext Type")
end function

and in MoveNext()

Public Function MoveNext() As Boolean _
Implements IEnumerator.MoveNext
Return GetNext(keysort, data, tid)
End Function
 
Back
Top