Function doesn't return a value on all code pa

2020-05-07 04:48发布

问题:

I'm somewhat new to programming and I am having trouble just making this work. I'm supposed to run this code using a delegate but I've run into error after error. I'm using Vb.net. Could anyone help me solve the issues in this code?

Public Delegate Function D()
Dim Str As String = Console.ReadLine()
Sub Main()
    Dim D1 As D
    D1 = New D(AddressOf Fn1)
End Sub
Function Fn1()
    System.Console.WriteLine("Please enter the string")

    Dim revstr As String = StrReverse(Str)
    Console.WriteLine("Reverse:")
    Console.WriteLine(revstr)

    Console.WriteLine("Amount of characters in the string:")
    Dim Count As Integer = Str.Length
    Console.WriteLine(Count)

    Console.WriteLine("Amount of words in the string:")
    Dim TempA() As String = Str.Split(" ")
    Console.WriteLine(TempA.Length & " ")
    Console.ReadKey()
End Function

Thanks for any help anyone can give!

回答1:

Your Function Fn1 doesn't return anything.

Functions by definition return something. If you want a method that doesn't return anything you should declare it Sub instead of Function

Your function should also declare the data type is is returning:

Public Function Concat(s1 As String, s2 As String) As String
    return s1 & s2
End Function