Jump to another case in switch case condition with current value in ASP.Net using C# and VB.Net

mohal92
 
on May 27, 2021 11:05 PM
Sample_290016.zip
1794 Views

Suppose there are 3 cases, if descr value matches with case x it going inside case x.

But after case z I want to use case x value which is pass through descr

How i can access that case x value at case z.

public string validate(string descr)
{
    switch (descr)
    {
        case x:
            break;
        case y:
            break;
        case z:
            break;
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on May 28, 2021 12:02 AM
on May 28, 2021 12:08 AM

Hi mohal92,

Use goto Statement.

It can jump to anywhere in the same action scope. You can use goto to customize your logic flow, even use it to simulate recursion, iteration, and selection.

Refer below sample code.

Code

C#

protected void OnValidate(object sender, EventArgs e)
{
    validate("1");
}

public string validate(string descr)
{
    string detail = "";
    switch (descr)
    {
        case "1":
            detail = descr;
            goto case "3";
        case "2":
            break;
        case "3":
            return detail;
    }

    return descr;
}

VB.Net

Protected Sub OnValidate(ByVal sender As Object, ByVal e As EventArgs)
    validate("1")
End Sub

Public Function validate(ByVal descr As String) As String
    Dim detail As String = ""
    Select Case descr
        Case "1"
Case1:
            detail = descr
            GoTo Case3
        Case "2"
Case2:
        Case "3"
Case3:
            Return detail
    End Select

    Return descr
End Function