Results 1 to 7 of 7

Thread: Value of type 'System.Func(Of String, String)' cannot be converted to 'System.Convert

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Value of type 'System.Func(Of String, String)' cannot be converted to 'System.Convert

    Value of type 'System.Func(Of String, String)' cannot be converted to 'System.Converter(Of String, String)

    Why? They are both effectively a function pointer (or delegate?) to a function that accept a string and return a string.

    SO there has to be a way to make that thing work.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Value of type 'System.Func(Of String, String)' cannot be converted to 'System.Con

    They're not the same. One has a type of System.Func(Of String, String) the second one is System.Converter(Of String, String).

    Tell us more about what are you trying to do. (With the code, preferrably).

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Value of type 'System.Func(Of String, String)' cannot be converted to 'System.Con

    Looks to me both accept function (x) x

    Error 1 Overload resolution failed because no accessible 'PerformATask' is most specific for these arguments:
    'Public Sub PerformATask(filename As String, func As System.Func(Of String, String))': Not most specific.
    'Public Sub PerformATask(filename As String, func As System.Converter(Of String, String))': Not most specific.

    It's essentially the same thing yet not convertable to one or the other

    vb.net Code:
    1. Public Sub PerformATask(ByVal filename As String, ByVal func As System.Converter(Of String, String))
    2.         Dim colBuffer = fileToStringCol(filename)
    3.  
    4.         colBuffer = colBuffer.ConvertAll(Of String)(func)
    5.     End Sub
    6.     Public Sub PerformATask(ByVal filename As String, ByVal func As System.Func(Of String, String))
    7.         PerformATask(filename, Function(x) func(x))
    8.     End Sub

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Value of type 'System.Func(Of String, String)' cannot be converted to 'System.Con

    It is still not quite clear to me what are you trying to do, but consider the following code:

    vb Code:
    1. Public Class Form1
    2.     Private Delegate Function TestDelegate(ByVal StringParam As String) As String
    3.  
    4.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    5.         Dim d1 As TestDelegate = AddressOf MainImplementation
    6.         Dim d2 As TestDelegate = AddressOf AlternativeImplementation
    7.  
    8.         Dim argument As String = "test"
    9.  
    10.         DoATask(d1, argument)
    11.         DoATask(d2, argument)
    12.     End Sub
    13.  
    14.     Private Function MainImplementation(ByVal param1 As String) As String
    15.         Return param1 & "(main implementation)"
    16.     End Function
    17.  
    18.     Private Function AlternativeImplementation(ByVal param1 As String) As String
    19.         Return param1 & "(alternative implementation)"
    20.     End Function
    21.  
    22.     Private Sub DoATask(ByVal method As TestDelegate, ByVal arg As String)
    23.         MsgBox(method.Invoke(arg))
    24.     End Sub
    25. End Class

    It uses two different implementations (Main and Alternative) and two delegates of the same type (TestDelegate)

    In your code you're using two different delegates (even if they happen to have the same number of arguments and the same returned value).

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Value of type 'System.Func(Of String, String)' cannot be converted to 'System.Con

    You mean, a function delegate that have the same number of arguments and the same returned value are actually 2 different delegates?

    Is delegate an object or a type anyway?

    Let's examine this code:
    vb.net Code:
    1. Dim b = Function(x As String) x ' type of b is <function (string) as string> based on option infer (why the < and > anyway?)
    2.         Dim c As System.Func(Of String, String) = b 'whatever type b is, sort of work with system.func and system.converter
    3.         Dim d As System.Converter(Of String, String) = b 'whatever type b is, sort of work with system.func and system.converter
    4.         Dim e As <function (string) as string> 'can't declare b type normally this is a compile error

    How do we declare e as a delegate to a function that accepts a string and return a string without initializing it?

    I mean we did that in Dim b = Function(x As String) x

    This is another way to see the problem

    function hello(f as WHAT?) as string
    so that
    dim g as system.converter(of string, string)=f works
    and
    dim h as system.converter(of string, string)=f works too
    Last edited by teguh123; Feb 17th, 2011 at 05:40 AM.

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Value of type 'System.Func(Of String, String)' cannot be converted to 'System.Con

    More like a type. In your case Func(Of String, String) is one delegate and Converter(Of String, String) - is another.

    It's the same with interfaces.

    vb Code:
    1. Public Interface A
    2.         Sub DoATask()
    3.     End Interface
    4.  
    5.     Public Interface B
    6.         Sub DoATask()
    7.     End Interface

    If one class implements interface A and another one implements Interface B they won't match

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    Re: Value of type 'System.Func(Of String, String)' cannot be converted to 'System.Con

    Dim b = Function(x As String) x

    What is the type of b?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width