我为我的vb.net课程做了这个任务。怎么了?

时间:2013-07-05 17:53:03

标签: vb.net

好的,所以分配是

1) Write a program using various procedures to perform the operations listed below. Call these procedures using delegates. Make sure to document your program and have the program print descriptive text along with the numbers in b. and c.
    a) Print a text string in reverse word order. 
    b) Print the number of characters in the string.
    c) Print number of words in the string.

我尝试了以下代码,但是我得到了AllGreets.DynamicInvoke的异常。我该怎么做才能解决这个问题?

我不是一个程序员,参加这个课程不是我做过的最好的选择,但现在我必须完成它。因为它已经付清了。

Module Module1

    Sub Main()

        Dim part1 As GreetingDelegate
        Dim part2 As GreetingDelegate
        Dim part3 As GreetingDelegate
        Dim part4 As GreetingDelegate

        part1 = AddressOf greating
        part2 = AddressOf greatingchar
        part3 = AddressOf reverse
        part4 = AddressOf number

        Dim AllGreets As GreetingDelegate = _
                      [Delegate].Combine(part1, part2, part3, part4)

        AllGreets.DynamicInvoke()
    End Sub

    Public Delegate Sub GreetingDelegate(ByVal MsgString As String)

    ' What the greating is
    Public Sub greating()

        Console.WriteLine("The greating is Hi how are you?")
        System.Console.WriteLine("press enter")
        Console.ReadLine()
    End Sub

    ' Number of char in string
    Public Sub greatingchar()

        Dim thing As Long
        thing = Len("Hi how are you?")
        System.Console.WriteLine("There is")
        System.Console.WriteLine(thing)
        System.Console.WriteLine("characters in this greating")
        System.Console.WriteLine("press enter")
        Console.ReadLine()
    End Sub

    Public Sub reverse()

        ' string in reverse
        Dim t As String

        t = StrReverse$("Visual Basic")

        System.Console.WriteLine(t)
        System.Console.WriteLine("press enter")
        System.Console.ReadLine()
    End Sub

    Public Sub number()

        'number of word in string

        Dim count As Long
        Dim text As String

        text = "Hi how are you?"
        count = text.Split(" ").Length
        System.Console.WriteLine(count)
        System.Console.WriteLine("press enter")
        System.Console.ReadLine()
    End Sub
End Module

1 个答案:

答案 0 :(得分:0)

委托定义实施者必须使用的签名。在您的情况下,您定义了一个方法,该方法将字符串作为输入参数并且什么都不返回。为了使用它,您的实现方法必须与该签名一致。例如,如果您希望它的行为类似于GreetingDelegate,那么问候方法(BTW,正确拼写您的方法)将需要如下所示:

Public Sub Greeting(msgString As String)
   ' Do something with msgString
End Sub

或者,如果您不需要输入字符串,您的委托可以声明如下,但是要意识到您定义此委托类型的所有方法也不允许输入字符串参数(因为它们不会同意声明的签名)。

Public Delegate Sub GreetingDelegate()