为什么我的函数没有返回任何东西?

时间:2021-01-18 13:09:30

标签: vb.net function

谁能帮助我为什么我总是收到这些警告?

我是新手,不太了解自己的路;这是一个应用程序,当你按下按钮时,它会在我的相机上拍照。

这是我的代码:

Public Class test

    Public test_ID As String = "............"
    Public test_PASSWORD As String = ".........."
    Public test_IP As String = "http://.........."
    Public test_URL As String = test_IP & "osc/"

    Function post(osc_command As Object) As Object
        Dim url = test_URL + osc_command
        Dim request As Net.WebRequest = Net.WebRequest.Create(url)
        request.Credentials = New Net.NetworkCredential(test_ID, test_PASSWORD)
        Dim resp As Net.WebResponse = request.GetResponse()
    End Function

    Function takePicture() As Object
        Dim url = test_URL & "commands/execute"
        Dim payload = New Dictionary(Of Object, Object) From {
                {"name", "camera.takePicture"}
            }
        Dim request As Net.WebRequest = Net.WebRequest.Create(url)
        request.Credentials = New Net.NetworkCredential(test_ID, test_PASSWORD)
        Dim resp As Net.WebResponse = request.GetResponse()
    End Function

    Sub New()
        post("state")
        takePicture()
    End Sub

End Class

这是按钮:

Private Sub cmdTakePicture_Click(sender As Object, e As EventArgs) Handles cmdTakePicture.Click
    Dim testObject As New dder_testConnector.theta
    testObject.takePicture()
End Sub

2 个答案:

答案 0 :(得分:0)

在您的 takePicturepost 函数中,您需要在 Dim resp As Net.WebResponse = request.GetResponse() 行下添加:

' Code to use the WebResponse goes here .... (See Jimi's comment for an example)

' Close the response to free resources.
resp.Close()

Return (whatever you wanted to return from the function)

初学者小技巧:当你想向调用者返回一些数据时使用Function;当您不需要直接向调用者返回任何内容时,请使用 Sub

更多信息:

https://docs.microsoft.com/en-us/dotnet/api/system.net.webresponse?view=net-5.0

答案 1 :(得分:0)

请参考文档,很清楚。

(例如布尔值)VB.NET 函数的最小签名是:

Function MyFunction() As Boolean
    Return True
End Function

您也可以使用较旧的 VB6 表单,但这并不常见:

Function MyFunction() As Boolean
    MyFunction = True
    Exit Function
End Function

现在,请务必查看the documentation