Visual Basic (vb.net) 如何检查任务是否无法完成
Visual Basic (vb.net) How to check if task could not be completed
我试图做到这一点,所以当我的按钮被点击时,它会 运行 通过一系列任务或要做的事情,但我想要它,所以当它无法完成任何这些任务时它会(做某事)——比如使用 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 年......事情的格式真的把我搞砸了......
编辑:
您将需要使用 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
我试图做到这一点,所以当我的按钮被点击时,它会 运行 通过一系列任务或要做的事情,但我想要它,所以当它无法完成任何这些任务时它会(做某事)——比如使用 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 年......事情的格式真的把我搞砸了......
编辑:
您将需要使用 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