声明一个案例列表在多个地方使用

发布于 2024-10-04 08:45:15 字数 1482 浏览 5 评论 0原文

我的页面中有以下代码,尽管案例数量要大得多,但工作正常。我需要在其他选择中使用完全相同的案例列表,但我不想在各处重复完全相同的代码。

我有这个:

   Select Case Request.Cookies("LatestRefer").Value
        Case "EZ12"
            freeCallNumber = "0800 111 1111"
        Case "EW56"
            freeCallNumber = "0800 222 2222"
        Case "AT34"
            freeCallNumber = "0800 333 3333"
        Case Else
                freeCallNumber = "0800 444 4444"
    End Select

我理想地想要这样的东西

Select Case Request.Cookies("cookie1").Value
            myGlobalListOfCases()
        End Select

Select Case Request.Cookies("cookie2").Value
                myGlobalListOfCases()
            End Select

Select Case Request.Cookies("cookie3").Value
                    myGlobalListOfCases()
                End Select

有什么想法吗?

编辑:

Private Function getFreeCallNumber(ByVal value As String) As String
        Select Case value
            Case "EZ12"
                Return "0800 111 1111"
            Case Else
                Return "0800 222 2222"
        End Select
    End Function

在 page_load 中:

If Not Request.Cookies("cookie1") is Nothing Then
    freeCallnumber = Me.getFreeCallNumber(Request.Cookies("cookie1").Value)
        Else
  freeCallnumber = Me.getFreeCallNumber(Request.Cookies("cookie2").Value)
        End If

这种方法可以工作,但有一个小问题。我必须加载页面两次才能更改电话号码(或者电话号码按上次加载时应有的方式显示)。希望这是有道理的......这是相当奇怪的行为。

I have the following code in my page which works fine although the number of cases is much much bigger. I need to use exactly the same list of cases in other selects but I don't want to have exactly the same code duplicate all over the place.

I have this:

   Select Case Request.Cookies("LatestRefer").Value
        Case "EZ12"
            freeCallNumber = "0800 111 1111"
        Case "EW56"
            freeCallNumber = "0800 222 2222"
        Case "AT34"
            freeCallNumber = "0800 333 3333"
        Case Else
                freeCallNumber = "0800 444 4444"
    End Select

I ideally want something like this

Select Case Request.Cookies("cookie1").Value
            myGlobalListOfCases()
        End Select

Select Case Request.Cookies("cookie2").Value
                myGlobalListOfCases()
            End Select

Select Case Request.Cookies("cookie3").Value
                    myGlobalListOfCases()
                End Select

Any ideas?

EDIT:

Private Function getFreeCallNumber(ByVal value As String) As String
        Select Case value
            Case "EZ12"
                Return "0800 111 1111"
            Case Else
                Return "0800 222 2222"
        End Select
    End Function

And in the page_load:

If Not Request.Cookies("cookie1") is Nothing Then
    freeCallnumber = Me.getFreeCallNumber(Request.Cookies("cookie1").Value)
        Else
  freeCallnumber = Me.getFreeCallNumber(Request.Cookies("cookie2").Value)
        End If

This kind of works but there's a slight problem. I have to load the page twice for the phone number to change (or the phone number appears as it should have done on the previous load). Hope this makes sense... it's fairly odd behaviour.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老娘不死你永远是小三 2024-10-11 08:45:15

创建一个方法:

private string getFreeCallNumber(string value)
{
    switch (value)
    {
        case "EZ12":
            return "0800 111 1111";
        case "EW56":
            return "0800 222 2222";
        // TODO: Add more switch cases here.
        default:
            return null;
    }
}

调用它时:

string freeCallnumber = this.getFreeCallNumber(Request.Cookies["cookie1"].Value));
if (string.IsNullOrEmpty(freeCallNumber))
{
     // other logic
}

我猜在VB.net中是这样的:

Private Function getFreeCallNumber(value as String) as String
    Select Case value
        Case "EZ12"
            return "0800 111 1111"
        Case "EW56"
            return "0800 222 2222"
        ' TODO: Add more switch cases here.
        Case Else
            return Nothing
    End Select
End Function

Create a method:

private string getFreeCallNumber(string value)
{
    switch (value)
    {
        case "EZ12":
            return "0800 111 1111";
        case "EW56":
            return "0800 222 2222";
        // TODO: Add more switch cases here.
        default:
            return null;
    }
}

When calling it:

string freeCallnumber = this.getFreeCallNumber(Request.Cookies["cookie1"].Value));
if (string.IsNullOrEmpty(freeCallNumber))
{
     // other logic
}

My guess that it's like this in VB.net:

Private Function getFreeCallNumber(value as String) as String
    Select Case value
        Case "EZ12"
            return "0800 111 1111"
        Case "EW56"
            return "0800 222 2222"
        ' TODO: Add more switch cases here.
        Case Else
            return Nothing
    End Select
End Function
缪败 2024-10-11 08:45:15

您可以尝试Dictionary(Of TKey, TValue) 类
注意:是可序列化的:-Q

You can try the Dictionary(Of TKey, TValue) Class
Note: is is serializable :-Q

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文