asp.net - 有没有更有效的方法来管理 IF 语句?

标签 asp.net vb.net

我总共有 102 个与名为 ArrivedFlag 的字段名关联的值。

然后我在标记页面上有一个 id 为 txtFlagTextBox 控件。

在我的代码隐藏中,我有一个 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/

相关文章:

.net - 将列表的枚举数传递给函数

vb.net - 为什么 Visual Studio 不调试我的 VB.NET 应用程序?

c# - 尝试使用 API Controller 返回 View 时出现问题

c# - 如何按名称选择 Sqlparameter 列表元素?

c# - ASP.NET 无法在同一类型变量中转换 session 变量

c# - 跟踪点有什么用途?

javascript - 如何在每个打印页面上打印 GridView 的标题

vb.net - 可以安全地假设,在 VB.net 中,方法意味着子或函数吗?

vb.net - VB中可以继承带参数的sub new(构造函数)吗?

asp.net - IIS 7.x中URL中的“+”符号问题