我必须为这个类创建一个程序,让我调用Multicast Delegate来向后写一个字符串,计算字符数,然后对单词进行计数。我已经解决了所有问题,但是当我运行它时,我得到: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
答案 0 :(得分:0)
您的代理Words
需要一个参数,但在您调用代理时不提供参数。
当您调用MulticastDelegate时,还提供委托必须使用的参数,如下所示:
Display.DynamicInvoke(Input)