Problems with extention methods converted from C#

ola

Member
Joined
Sep 20, 2010
Messages
7
Programming Experience
5-10
I converted some code from c# for a VB.net project

I have an object declared as this :
VB.NET:
<Serializable()> _
Public Class oUserApplication
 
    Public ApplicationID As Integer = 0
    Public ApplicationRoleID As Integer = 0
    Public RoleID As UserRoles = UserRoles.Unknown
    Public DefaultFlag As Boolean = False
    Public Found As Boolean = False 'updated on UI side not from DB
 
    Public ApplicationDescription As String = ""
    Public RoleDescription As String = ""
 
    Public LastUpdateUser As String = ""
    Public LastUpdateDate As Date = Date.MinValue
 
    'TO DO figure out what to do with descriptions
    Public Shared Function Create(record As IDataRecord) As oUserApplication
 
        Return New oUserApplication With { _
   .ApplicationID = record("ApplicationID"),
        .ApplicationRoleID = record("ApplicationRoleID"),
        .DefaultFlag = record("DefaultFlag"),
        .LastUpdateUser = record("sysUpdateUser"),
        .LastUpdateDate = record("sysUpdateDate")
        }
 
 
 
    End Function
 
 
End Class

here is the extention method i am trying to call
VB.NET:
 <System.Runtime.CompilerServices.Extension()> _
    Public Function GetData(Of T)(reader As IDataReader, BuildObject As Func(Of IDataRecord, T)) As IEnumerable(Of T)
        Try
            While reader.Read()
                Return BuildObject(reader)
            End While
        Finally
            reader.Dispose()
        End Try
        Return Nothing
    End Function
now my problem is this on my call vb expects me to pass an object that implements IDatarecord however c# does not here is my call to the extention method

VB.NET:
Dim result = Myextentions.GetData(Of oUserApplicationoUser)(oRD, oUserApplicationoUser.Create)
I get the error Error 7 Argument not specified for parameter 'record' of 'Public Shared Function Create(record As System.Data.IDataRecord) As oUserApplication'. C:\VisualStudio\SourceControl\Replatform\WebServices\UserWS\UserWS\ProcessUser.vb 1117 77 UserWS any ideas ?
 
Extension methods extends as instance methods of the type they extend:
Dim result = oRD.GetData(AddressOf oUserApplicationoUser.Create)

T is inferred type oUserApplication from Create method return type.

This part won't work:
While reader.Read()
Return BuildObject(reader)
End While
Return statement will make GetData return only a single object, VB 2010 doesn't have a Yield equivalent (next version has it), so you have to add all objects to a temporary collection and return that.
 
that worked ! , I will just have to fix the other isue you found with the solution you suggest

thanks !
 
How do I create a temporary collection of

Extension methods extends as instance methods of the type they extend:
Dim result = oRD.GetData(AddressOf oUserApplicationoUser.Create)

T is inferred type oUserApplication from Create method return type.

This part won't work:

Return statement will make GetData return only a single object, VB 2010 doesn't have a Yield equivalent (next version has it), so you have to add all objects to a temporary collection and return that.

forvige my poor VB skills why does this not work

Public Function GetData(Of T)(reader As IDataReader, BuildObject As Func(Of IDataRecord, T)) As IEnumerable(Of T)
Try
Dim BuildObjectTemp As List(Of Func(Of IDataRecord, T))
While reader.Read()
BuildObjectTemp.Add((BuildObject(reader)))
End While
Finally
reader.Dispose()
End Try
Return Nothing
End Function

Its I keep getting an error I dont want to impliment my own Iterator , can I use the Asynch CTP and use the Yeild function it exposes ?

Iterators in Visual Basic
 
I figured it out

<System.Runtime.CompilerServices.Extension()> _
Public Function GetData(Of T)(reader As IDataReader, BuildObject As Func(Of IDataRecord, T)) As IEnumerable(Of T)
Try

While reader.Read()
Return New List(Of T) From {(BuildObject(reader))}
End While
Finally
reader.Dispose()
End Try
Return Nothing
 
Return type of BuildObject function is T.
Func(Of T, TResult) Generic Delegate:
Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter.
So you create a List(Of T) to hold the T objects, where T is the argument (provided by GetData parameter) and not a parameter that you must substitute. This is also consequently the List you ended up with.

To further expand on the logic, you may recall the function that was assigned to it was oUserApplicationoUser.Create, which by definition returns an object of type oUserApplication, so when BuildObject is called within the generic method that is the function that is actually called. For the generic method oUserApplicationoUser type is irrelevant though, it only needs to work with the T argument, but in some cases it may help to see what the generic definition becomes when closed to a specific type.
 
While reader.Read()
Return New List(Of T) From {(BuildObject(reader))}
End While
That will still only return a single object, since Return statement is the final statement in a method. By using Return at that place BuildObject is called only once.
You just need to create the list, fill it with the loop, and finally return it.
 
Back
Top