当我想在Select Case
中定义值时,我收到错误:
'Value' is not declared. It may be inaccessible due to its protection level.
当Dim Value As Object
在外时选择案例 - 没有错误。我的目标是在特殊数字上得到不同的值。例如:
Select Case Integer
Case 1
Dim Value As New UserControl1
Case 2
Dim Value As New UserControl2
Case Else
Dim Value As New UserControl3
End Select
答案 0 :(得分:1)
尝试此操作,假设所有3个用户控件类型都派生自基础UserControl
对象:
Dim Value as UserControl
Select Case Integer
Case 1
Value = New UserControl1
Case 2
Value = New UserControl2
Case Else
Value = New UserControl3
End Select
答案 1 :(得分:1)
在作用域内声明变量并在作用域之外访问它是不可行的,例如select case语句。但是,通过分离声明和初始化可以轻松解决问题。这使您可以在选择案例之外使用变量,因为变量位于更高的范围内。该变量声明为System.Windows.Controls.UserControl,因为这是最具体的常见类型。
Dim Value As UserControl
Select Case Integer
Case 1
Value = New UserControl1
Case 2
Value = New UserControl2
Case Else
Value = New UserControl3
End Select