我必须为这个类编写一个程序,让我调用一个多播代表,向后写一个字符串,计数字符和单词。我已经解决了所有的问题,但是当我运行它时,我得到: System.Reflection.TargetParameterCountException
我昨天才了解到代表们的情况,所以我可能错过了一些明显的事情。
Private Delegate Sub Words(ByVal input As String)
Sub Main()
Console.Title = "Fun with words"
Console.WriteLine("Type some words below.")
Dim Input As String = Console.ReadLine()
Dim Display As [Delegate]
Dim Backwards As Words
Dim Length As Words
Dim Wordcount As Words
Backwards = New Words(AddressOf BackwardsFunc)
Length = New Words(AddressOf LengthFunc)
Wordcount = New Words(AddressOf WordcountFunc)
Display = MulticastDelegate.Combine(Backwards, Length, Wordcount)
Display.DynamicInvoke()
Console.ReadKey() 'end
End Sub
Sub BackwardsFunc(ByVal Input As String)
' Backwards
Console.Clear()
Console.ForegroundColor = ConsoleColor.Cyan
Console.WriteLine(Input)
Console.ResetColor()
Console.WriteLine("Backwards:")
Dim Array() As Char = Input.ToCharArray
Dim Stack As New Stack()
For Each element As Char In Array
Stack.Push(element)
Next
For Each element As Char In Stack
Console.WriteLine(element)
Next
End Sub
Sub LengthFunc(ByVal Input As String)
Console.WriteLine()
Console.WriteLine("Length: " & Input.Length)
End Sub
Sub WordcountFunc(ByVal Input As String)
Dim Items As String() = Input.Split(New Char() {" "c})
Console.WriteLine()
Console.WriteLine("Words: " & Items.Length)
End Sub
发布于 2014-08-09 20:28:50
您的委托Words
需要一个参数,但在调用委托时不提供参数。
在调用MulticastDelegate时,还提供委托必须使用的参数,如下所示:
Display.DynamicInvoke(Input)
https://stackoverflow.com/questions/25222379
复制相似问题