Results 1 to 5 of 5

Thread: [RESOLVED] Better way to do this 'elseif' statement

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2010
    Posts
    197

    Resolved [RESOLVED] Better way to do this 'elseif' statement

    Hi all, i have a treeviewlist and use it to apply filters for a database, on the 'afterselect' event. Which all works great, how ever some possible filters could be quite lengthy, heres an example;
    vb Code:
    1. If e.Node.Text = "Early Childhood (EC)" Or e.Node.Text = "Everyone (E)" Or e.Node.Text = "Everyone 10+ (E10+)" Or e.Node.Text = "Teen (T)" Or e.Node.Text = "Mature (M)" Or e.Node.Text = "Adults Only (AO)" Or e.Node.Text = "Rating Pending" Then
    2.             TblContactsBindingSource.Filter = String.Format("ESRB = '{0}'", e.Node.Text)
    3.             lbg1.SetSelected(0, True)

    One elseif statement has 30 'Or e.Node.Text = "somestring" '

    So is there a better/neater way to do this?

    Thanks.

  2. #2
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Better way to do this 'elseif' statement

    Yep, use an array and loop through the values:
    Code:
    Dim checkvalues(29) As String
    checkvalues(0) = "orcheck"
    'etc
    Dim result As Boolean = False
    For Each value As String In checkvalues
         If e.Node.Text = value Then
              result = True
              Exit For
         End If
    Next
    You could also make a String variable with all allowed text values and use the "String.Contains" function to check if e.Text is allowed. I like the array way better though.

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Better way to do this 'elseif' statement

    You could hard code these values into your application, like I did below, but a better way would be to maintain a lookup table on your database.

    Code:
    Public ReadOnly Property ESRBFilter() As HashSet(Of String)
        Get
            Dim hs As New HashSet(Of String)
            hs.Add("Early Childhood (EC)")
            hs.Add("Everyone (E)")
            hs.Add("Everyone 10+ (E10+)")
            hs.Add("Teen (T)")
            hs.Add("Mature (M)")
            hs.Add("Adults Only (AO)")
            hs.Add("Rating Pending")
            Return hs
        End Get
    End Property
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Me.ESRBFilter.Contains(e.Node.Text) Then
            TblContactsBindingSource.Filter = String.Format("ESRB = '{0}'", e.Node.Text)
            lbg1.SetSelected(0, True)
        End If
    End Sub
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2010
    Posts
    197

    Re: Better way to do this 'elseif' statement

    Thanks guys!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Better way to do this 'elseif' statement

    First, if you were going to use an If with multiple conditions like that, you would use OrElse rather than Or. OrElse short-circuits while Or doesn't, meaning that OrElse will stop comparing as soon as it finds a match while Or will compare everything regardless.

    Second, you wouldn't use e.Node.Text over and over. You would get that value once only and assign it to a local variable, then use that over and over. So, your original code should look like this:
    vb.net Code:
    1. Dim nodeText = e.Node.Text
    2.  
    3. If nodeText = "Early Childhood (EC)" OrElse
    4.    nodeText = "Everyone (E)" OrElse
    5.    nodeText = "Everyone 10+ (E10+)" OrElse
    6.    nodeText = "Teen (T)" OrElse
    7.    nodeText = "Mature (M)" OrElse
    8.    nodeText = "Adults Only (AO)" OrElse
    9.    nodeText = "Rating Pending" Then
    There's really not much you can do to make that genuinely simpler. I wouldn't use a loop and a HashSet is probably overkill for such a short list of hard-coded values. In both cases, the code will end up longer rather than shorter anyway. You might try this:
    vb.net Code:
    1. Select Case e.Node.Text
    2.     Case "Early Childhood (EC)",
    3.          "Everyone (E)",
    4.          "Everyone 10+ (E10+)",
    5.          "Teen (T)",
    6.          "Mature (M)",
    7.          "Adults Only (AO)",
    8.          "Rating Pending"
    9.         '...
    10. End Select
    or this:
    vb.net Code:
    1. If {"Early Childhood (EC)",
    2.     "Everyone (E)",
    3.     "Everyone 10+ (E10+)",
    4.     "Teen (T)",
    5.     "Mature (M)",
    6.     "Adults Only (AO)",
    7.     "Rating Pending"}.Contains(e.Node.Text) Then
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width