ASP.NET: to many if stamtemnt for certain condition

mush 181 Reputation points
2022-12-04T10:17:54.107+00:00

I'm trying to write a condition where it checks the levels of approval as follows:

If lbllvl1.Text = "HOS" And lbllvl2.Text = "HOD" And lbllvl3.Text = "ADAA" And lbllvl4.Text = "HR" Then  
           StatFlag = 47 'HOS  
           Label1.Text = "hello world"  
        ElseIf lbllvl1.Text = "0" And lbllvl2.Text = "HOD" And lbllvl3.Text = "ADAA" And lbllvl4.Text = "HR" Then  
            StatFlag = 46  'HOD  
           Label1.Text = "not hello world"  
        ElseIf lbllvl1.Text = "0" And lbllvl2.Text = "0" And lbllvl3.Text = "ADAA" And lbllvl4.Text = "HR" Then  
           StatFlag = 41 'ADAA  
            Label1.Text = "hello world"  
        ElseIf lbllvl1.Text = "0" And lbllvl2.Text = "0" And lbllvl3.Text = "0" And lbllvl4.Text = "HR" Then  
           StatFlag = 46 'HR  
           Label1.Text = "hello world"  
        End If  

the problem is I have way too many cases. since i have many approvers (Heads of Dept., Head of Center, Head of admin affairs, Head of hr, head of finance ect)

how to include all of them without using so many else ifs.266828-capture.png

this is the table

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,570 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 25,556 Reputation points Microsoft Vendor
    2022-12-05T09:08:12.04+00:00

    Hi @mush ,
    First your if stamtemnt can be abbreviated as:

    If lbllvl1.Text = "HOS"  Then  
                StatFlag = 47   
                Label1.Text = "hello world"  
             ElseIf  lbllvl2.Text = "HOD" Then  
                 StatFlag = 46    
                Label1.Text = "not hello world"  
             ElseIf  lbllvl3.Text = "ADAA"  Then  
                StatFlag = 41   
                 Label1.Text = "hello world"  
             ElseIf lbllvl4.Text = "HR" Then  
                StatFlag = 46   
                Label1.Text = "hello world"  
             End If  
    

    Secondly, if you write an if statement based on your database, it will be troublesome.
    My suggestion is that each level is fixed. For example, if the first layer is HOS, it will always be HOS, and if it does not exist, it will be 0;
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.