locked
Problem Calling a VB extention method RRS feed

  • Question

  • User627028808 posted

    I converted some code from c# for a VB.net project

    I have an object declared as this :

    <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 IDataRecordAs 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

     <System.Runtime.CompilerServices.Extension()> _
        Public Function GetData(Of T)(reader As IDataReader, BuildObject As Func(Of IDataRecordT)) 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
    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 ?
    Thursday, February 23, 2012 1:13 PM

Answers

  • User560403387 posted

    You should add 'AddressOf ' before your reference to the Create method in the GetData call. So

    Dim result = Myextentions.GetData(Of oUserApplication)(oRD, AddressOf oUserApplication.Create)

    Or even better:

    Dim result = oRD.GetData(Of oUserApplication)(AddressOf oUserApplication.Create)

    You know; extension method style. That should work.

    Menno

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 23, 2012 2:18 PM

All replies

  • User560403387 posted

    You should add 'AddressOf ' before your reference to the Create method in the GetData call. So

    Dim result = Myextentions.GetData(Of oUserApplication)(oRD, AddressOf oUserApplication.Create)

    Or even better:

    Dim result = oRD.GetData(Of oUserApplication)(AddressOf oUserApplication.Create)

    You know; extension method style. That should work.

    Menno

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 23, 2012 2:18 PM
  • User627028808 posted

    You are Awesme Menno, thanks the AddressOf before the refrence worked for my original call.

    you are right I should use the extention method properly with youe secceond way of calling it however

    I cannot Access Getdata using oRD which is actually a record set

    Not sure why any ideas on why ? My extention method is part of an external library  which is imported into the calling Project

     :

    ublic Module MyExtentions
     
        
     
        <System.Runtime.CompilerServices.Extension()> _
        Public Function GetData(Of T)(reader As IDataReader, BuildObject As Func(Of IDataRecordT)) As IEnumerable(Of T)
            Try
                While reader.Read()
                    Return BuildObject(reader)
                End While
            Finally
                reader.Dispose()
            End Try
            Return Nothing
        End Function
     
       
     
    End Module
    Thursday, February 23, 2012 2:51 PM
  • User627028808 posted

    You should add 'AddressOf ' before your reference to the Create method in the GetData call. So

    Dim result = Myextentions.GetData(Of oUserApplication)(oRD, AddressOf oUserApplication.Create)

    Or even better:

    Dim result = oRD.GetData(Of oUserApplication)(AddressOf oUserApplication.Create)

    You know; extension method style. That should work.

    Menno

     

    DOh sorry I was an idiot , the seccond call worked as well, Just had to recompile lol

     

    thanks !! , this new code will add a ton of efficeincy and speed to my application , using generic collections and no more datasets , we used to fill a dataset and then convert it to our generic collection of objects ugh!

    Thursday, February 23, 2012 2:56 PM