Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top