我总共有 102 个与名为 ArrivedFlag
的字段名关联的值。
然后我在标记页面上有一个 id 为 txtFlag
的 TextBox
控件。
在我的代码隐藏中,我有一个 if 语句:
If txtFlag <> "value1" and txtFlag <> "another value" Then
lblMessage.Text ="You don't have the ability to view details"
end if
这很好用。
但是,考虑到有 102 个值,我觉得执行 102 次 IF 语句效率有点低。
有谁知道更好的方法来处理这个问题吗?
Dim allowedFlags = New List(Of String)()
With { _
"value1", _
"another value"
}
End With
If Not allowedFlags.Contains(txtFlag) Then
lblMessage.Text = "You don't have the ability to view details"
End If
最佳答案
将值添加到列表中,然后使用 .Contains
// hopefully you can get the 102 strings from an xml document or db
var listOfFlags = new List<string> { "value1", "another value", .... };
if (!listOfFlags.Contains(txtFlag))
{
lblMessage.Text = "you dont' have ..."
}
关于asp.net - 有没有更有效的方法来管理 IF 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8161551/