Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a Dynamic select case (vb)

I would like to have one of my cases be a dynamic string because in my table these values changed partially except for the beginning code "2061" so how do I make my code do something similar to when I'm looking up in my query "... like '2061%' here is my code

Function GetResponse(val As String) As String
        Select Case val
            Case "2061"
                Return "Opted Out"
            Case "00"
                Return ""
            Case Else
                Return "Other"
        End Select
    End Function
like image 422
Arthur Muggins Avatar asked Nov 03 '22 12:11

Arthur Muggins


1 Answers

You can use the Like operator in VB.NET for this. Here I'm selecting the first case that matches True.

Function GetResponse(val As String) As String
    Select Case True
        Case val Like "2061*"
            Return "Opted Out"
        Case val = "00"
            Return ""
        Case Else
            Return "Other"
    End Select
End Function

In C# you would use an if statement to get the same functionality:

string GetResponse(string value)
{
    if (value.StartsWith("2061"))
        return "Opted Out";
    else if (value == "00")
        return "";
    else
        return "Other";
}

And in F#:

let GetResponse (value:string) =
    match value with
    | x when x.StartsWith("2061") -> "Opted Out"
    | "00" -> ""
    | _ -> "Other"

Note, I recommend using more descriptive function and variable names than this, but I don't know what context this code is in. "GetResponse" is vague, and doesn't indicate what the return values might mean. You might want to rename it as "GetUserOptedOutStatus" or something similar.

like image 172
Philip Avatar answered Nov 15 '22 04:11

Philip