2013-07-05 2 views
0

OK 할당이 나는 다음과 같은 코드를 시도했지만 나는 AllGreets.DynamicInvoke과 예외가내 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. 

그래서. 이 문제를 해결하려면 어떻게해야합니까?

나는 프로그래머가별로 없으며이 수업을 듣는 것이 내가 선택한 최고의 선택이 아니었지만 지금은 끝내야한다. 그것은 이미 지불 되었기 때문입니다.

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 
+2

이 메서드는 대리자 선언과 일치해야합니다. greate()는 문자열 인수를 취하지 않습니다. 그리고 DynamicInvoke() 메서드에 문자열을 전달하는 것을 잊지 마십시오. –

답변

0

대리인은 구현자가 사용해야하는 서명을 정의합니다. 귀하의 경우에는 문자열을 입력 매개 변수로 사용하고 아무 것도 반환하지 않는 메서드를 정의했습니다. 이를 사용하려면 구현 방법이 해당 서명과 일치해야합니다. 당신이이 GreetingDelegate처럼 행동하려는 경우 예를 들어, 다음과 같이 인사말 방법이 될 필요가있을 것이다, (BTW, 제대로 방법을 철자) :

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

을 다른 방법으로, 입력 문자열이 필요하지 않은 경우, 위임자는 다음과 같이 선언 될 수 있지만이 델리게이트 형식을 정의하는 모든 메서드는 입력 된 문자열 매개 변수도 허용하지 않습니다 (선언 된 서명과 일치하지 않기 때문).

Public Delegate Sub GreetingDelegate() 
관련 문제