|
-
Feb 17th, 2011, 04:23 AM
#1
Thread Starter
Fanatic Member
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.
-
Feb 17th, 2011, 04:26 AM
#2
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).
-
Feb 17th, 2011, 04:28 AM
#3
Thread Starter
Fanatic Member
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:
Public Sub PerformATask(ByVal filename As String, ByVal func As System.Converter(Of String, String)) Dim colBuffer = fileToStringCol(filename) colBuffer = colBuffer.ConvertAll(Of String)(func) End Sub Public Sub PerformATask(ByVal filename As String, ByVal func As System.Func(Of String, String)) PerformATask(filename, Function(x) func(x)) End Sub
-
Feb 17th, 2011, 05:17 AM
#4
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:
Public Class Form1 Private Delegate Function TestDelegate(ByVal StringParam As String) As String Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim d1 As TestDelegate = AddressOf MainImplementation Dim d2 As TestDelegate = AddressOf AlternativeImplementation Dim argument As String = "test" DoATask(d1, argument) DoATask(d2, argument) End Sub Private Function MainImplementation(ByVal param1 As String) As String Return param1 & "(main implementation)" End Function Private Function AlternativeImplementation(ByVal param1 As String) As String Return param1 & "(alternative implementation)" End Function Private Sub DoATask(ByVal method As TestDelegate, ByVal arg As String) MsgBox(method.Invoke(arg)) End Sub 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).
-
Feb 17th, 2011, 05:27 AM
#5
Thread Starter
Fanatic Member
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:
Dim b = Function(x As String) x ' type of b is <function (string) as string> based on option infer (why the < and > anyway?) Dim c As System.Func(Of String, String) = b 'whatever type b is, sort of work with system.func and system.converter Dim d As System.Converter(Of String, String) = b 'whatever type b is, sort of work with system.func and system.converter 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.
-
Feb 17th, 2011, 05:29 AM
#6
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:
Public Interface A Sub DoATask() End Interface Public Interface B Sub DoATask() End Interface
If one class implements interface A and another one implements Interface B they won't match
-
Feb 19th, 2011, 03:26 PM
#7
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|