
Originally Posted by
jmcilhinney
Action is a delegate that represents a Sub, i.e. a method that doesn't return a value, while Func is a delegate that represents a function, i.e. a method that does return a value. They are both overloaded and accept from zero to 16 parameters.
An array requires that you specify its type, which then restricts each element to be that type. In my example, the array was type Action, so each element had to be an Action delegate that referred to a method with no parameters that doesn't return a value. If all your functions had the same return type then you could declare the array with the appropriate Func type, but that's not an option in this case. As is always the case when declaring something as a particular type, if it needs to refer to objects of multiple types then you need to declare it as a common type. The most specific type that is common to all delegates is Delegate. I don't know what you mean by "structure of strings" but here's an example of how you might approach this:
vb.net Code:
Private checkBoxes As CheckBox()
Private methods As [Delegate]() = {New Func(Of String, String)(AddressOf GetSomething),
New Func(Of String, String())(AddressOf GetSomethingElse)}
Private Function GetSomething(str As String) As String
'...
End Function
Private Function GetSomethingElse(str As String) As String()
'...
End Function
vb.net Code:
For Each method As [Delegate] In methods
Dim result = method.DynamicInvoke(TextBox1.Text)
If TypeOf result Is String Then
MessageBox.Show(CStr(result))
ElseIf TypeOf result Is String() Then
For Each value As String In DirectCast(result, String())
MessageBox.Show(value)
Next
End If
Next
Thank you. I actually modified the code but got it to work. It was a matter of understanding how it dealt with the data type. When I used the [Delegate] data type it actually didn't quite work. However by specifying the function type as the data type it worked just fine. I had to code four separate arrays. 1 for the first type of function return type and 1 for its associated checkboxes. Then a second for the second data return type and its associated checkboxes. Essentially to better explain the dilemma, I built a structure say:
vb.net Code:
Public Structure MyStructure
strFirstPartOfString = "x"
strSecondPartOfString = "y"
strThirdPartOfString = "z"
end structure
Then I created a function to use that structure. I also created functions that returned an array filled with variable of that structure:
vb.net Code:
Public Function MyFunction(ByVal As String) as MyStructure()
'Fill array with variable of type "MyStructure"
Return myStructureArray
End Function
So to code the actual "action list" array, using delegate didn't work. However this did:
vb.net Code:
Private arrayMyArrayOfFunctions As Func(Of String, MyStructure) = {Func(Of String, MyStructure)(AddressOf MyFunction), ....}
and also
vb.net Code:
Private arrayMyArrayOfFunctions2 As Func(Of String, MyStructure()) = {Func(Of String, MyStructure())(Address of MyFunction), ...}
Now all I have to do is get the manipulations to work. Thanks so much for your continued help in this matter. Hopefully delving into data types and structures in the spring will help me better understand how to deal with "homemade" data types a little better
I really appreciate the feedback, I'm learning a lot. Is there a good resource for learning more complex coding like this? Have any good book suggestions?
Note: It may appear as if my two function arrays are referencing the same function that returns a type of array, but I actually have two different functions, one returns a variable of type MyStructure, one returns a variable of type MyStructure(). Sorry if my examples are a little misleading in that regard or confusing.