在vb.net中断开if后的sub语句

时间:2014-06-15 19:39:01

标签: vb.net if-statement

我正在尝试编写仅在变量' file'时打开saveFileDialog的代码。 为空,如果' file'生成错误消息 为空。当' file'以下代码将弹出一个错误消息框。是空的,但无论如何都会继续打开saveFileDialog。

Public Shared Sub DownloadFile(cloudId As CloudIdentity, directoryPath As String, file      As String)

Try
Dim cloudFilesProvider = New CloudFilesProvider(cloudId)

If file = "" Then
cloudFilesProvider.GetObjectSaveToFile("EstherTest", directoryPath, file)

End If
Catch

If file = "" Then
MessageBox.Show("Please select file to view")
End If
End Try
End Sub

你可以指导我。

1 个答案:

答案 0 :(得分:0)

我认为你的第一个if语句中有错误的相等运算符。

用humand的话说,你告诉编译器:

  

如果file等于“”,则执行...(If file = "" Then

您要说的是:如果file不等于“”(If file <> "" Then)。

作为第二个注释:除非发生异常,否则永远不会调用第二个if语句,因为永远不会到达catch块。您应该将第二个条款放在elseelse if clause中。 为简单起见,我删除了try catch块(因为我对vb.net语法并不擅长)。

总而言之,您的代码应如下所示:

Public Shared Sub DownloadFile(cloudId As CloudIdentity, directoryPath As String, file      As String)
    Dim cloudFilesProvider = New CloudFilesProvider(cloudId)
    ' if file is not emtpy, proceed
    If file <> "" Then
        cloudFilesProvider.GetObjectSaveToFile("EstherTest", directoryPath, file)    
    ' else if file is empty, show message box
    else if file = "" Then
        MessageBox.Show("Please select file to view")
    End If
End Sub
相关问题