我已经在使用下面的代码。以下代码是okey。如何在vb.net中声明变量?
Private Sub Code1()
Try
If 1 = 1 Then 'This is not original condition in my project.
Label1.Text = "Apple"
End If
Dim x As Integer = 5
Dim y As Integer = 0
x = x \ y
Catch ex As Exception
If Label1.Text = "Apple" Then
MsgBox("Error")
End If
End Try
End Sub
我不想把Label1控件放在我的项目上。
因此,我决定用下面的代码替换上面的代码。
但下面的代码给出了这样的错误:http://prnt.sc/ace1bt
Private Sub Code2()
Try
If 1 = 1 Then 'This is not original condition in my project.
Dim k As String
k = "Apple"
End If
Dim x As Integer = 5
Dim y As Integer = 0
x = x \ y
Catch ex As Exception
Dim k As String
If k = "Apple" Then
MsgBox("Error")
End If
End Try
End Sub
所以,我需要一个代码,而无需使用像Label1的任何形式的控制。
请注意,变量必须位于Try后面。
我打开每一种代码。谢谢。
警告的哪部分你不明白? – SLaks
第一个块的工作原因是'Label'在Try和If块之外声明。第二次与第一次发布这个问题**的原因相同。如果/然后创建块范围。块范围中声明的内容保留在其中。 Catch中的'k'是一个新的变量,永远不会等于“apple”可能重复的[引用变量和对象在窗体中的其他位置](http://stackoverflow.com/q/33248704/1070452)。 – Plutonix