【问题标题】:Visual Basic (vb.net) How to check if task could not be completedVisual Basic (vb.net) 如何检查任务是否无法完成
【发布时间】:2021-04-13 11:08:48
【问题描述】:

我试图做到这一点,所以当我的按钮被点击时,它将运行一系列任务或要做的事情,但我想要它,所以如果它无法完成这些任务中的任何事情,它会(做某事) - Like Use MsgBox("此操作无法完成...")

这是我的按钮代码:

Private Sub SelectBtn_Click(sender As Object, e As EventArgs) Handles SelectBtn.Click
    Dim res = client.Get("Logins/" + usernamebox.Text)
    Dim std As New Student()
    std = res.ResultAs(Of Student)
    If std.Password = passwordbox.Text Then
        MsgBox("Welcome Back! " + usernamebox.Text + "!")
    Else
        MsgBox("Username OR Password May Be Wrong", MsgBoxStyle.Exclamation, "Login Error")
    End If

End Sub
    

好的,这是我的按钮代码,它从 Firebase 获取数据.. 但我想要它,所以当它失败时它不会破坏程序.. 而是显示一条消息......我想要这个的原因是出于某种原因,当一个值不“存在”时程序会中断它会中断,我只想显示一条消息,如果它试图中断并继续再次单击它等等。对不起,我输入的方式我已经习惯了Visual Studio 2019...事物的格式真的让我一团糟...

编辑:

【问题讨论】:

    标签: database vb.net firebase visual-studio fire-sharp


    【解决方案1】:

    您需要使用 Try/Catch 块:https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/try-catch-finally-statement

    在你的情况下,它可能看起来像这样:

    Try
        Dim res = client.Get("Logins/" + usernamebox.Text)
        Dim std As New Student()
        std = res.ResultAs(Of Student)
    
        ' student is null, login failed
        If (std Is Nothing) Then
            MessageBox("Username OR Password May Be Wrong", MsgBoxStyle.Exclamation, "Login Error")
        End If
    
        If (std.Password = passwordbox.Text) Then
            MessageBox("Welcome Back! " + usernamebox.Text + "!")
        Else
            ' student is not null, but the password doesn't match
            MessageBox("Username OR Password May Be Wrong", MsgBoxStyle.Exclamation, "Login Error")
        End If
    Catch ex As Exception
        MessageBox.Show("This operation could not be completed.")
        Console.WriteLine(ex.Message) ' see what cause the exception in the output dialog
    End Try
    

    【讨论】:

    • 好吧,我使用了我从你那里得到的代码.. 它仍然破坏了程序,它甚至不显示消息框或什么都没有,当我输入数据库中现有数据的 ID 时,它会拉取它但是如果它不存在,它就会中断。我尝试使用 if 语句来测试它是否为空,我会在我的主要问题上附上截图
    • @EthanMarr - 这表明std(顺便说一句,变量名选择不当)为空。查看我的编辑。
    猜你喜欢
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    相关资源
    最近更新 更多