|
-
Mar 21st, 2011, 03:20 PM
#1
Thread Starter
Addicted Member
[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:
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
TblContactsBindingSource.Filter = String.Format("ESRB = '{0}'", e.Node.Text)
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.
-
Mar 21st, 2011, 03:49 PM
#2
Fanatic Member
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.
-
Mar 21st, 2011, 03:50 PM
#3
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
-
Mar 21st, 2011, 08:40 PM
#4
Thread Starter
Addicted Member
Re: Better way to do this 'elseif' statement
-
Mar 22nd, 2011, 01:27 AM
#5
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:
Dim nodeText = e.Node.Text
If nodeText = "Early Childhood (EC)" OrElse
nodeText = "Everyone (E)" OrElse
nodeText = "Everyone 10+ (E10+)" OrElse
nodeText = "Teen (T)" OrElse
nodeText = "Mature (M)" OrElse
nodeText = "Adults Only (AO)" OrElse
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:
Select Case e.Node.Text
Case "Early Childhood (EC)",
"Everyone (E)",
"Everyone 10+ (E10+)",
"Teen (T)",
"Mature (M)",
"Adults Only (AO)",
"Rating Pending"
'...
End Select
or this:
vb.net Code:
If {"Early Childhood (EC)",
"Everyone (E)",
"Everyone 10+ (E10+)",
"Teen (T)",
"Mature (M)",
"Adults Only (AO)",
"Rating Pending"}.Contains(e.Node.Text) Then
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|