Select Case Problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

This is gonna sound real daft, but how do I test a Select Case statement for
variants of a theme?

Here's a snippet of my code...

Select Case sUsr
Case "Guest", "TsInternetUser", "krbtgt", "quality7"
' don't show
Case Left(sUsr, 13) = "SystemMailbox"
' don't show
Case Else
ListBox1.Items.Add(sUsr)
End Select

What i'm trying to do is find any sUsr variable that begins with
'SystemMailbox' and exclude it. If I use the code shown above it helpfully
informs me that the input string is not in the correct format. Is there any
way of using wildcards, or making the above sample correct?

Cheers,

Mike
 
Hi Mike

Select Case True
Case sUsr = "Guest", sUsr = "TsInternetUser", sUsr = "krbtgt", sUsr =
"quality7"
' don't show
Case sUsr.StartsWith("SystemMailbox")
' don't show
Case Else
ListBox1.Items.Add(sUsr)
End Select

HTH

Charles
 
Charles,

It does indeed work, thank you. I have read elsewhere that this form of
Select Case statement can be troublesome and others have discouraged it's
use. Do you have experience of this type of syntax and have you had any
problems with it in the past?

Cheers,

<M>ike
 
Mike

I personally don't like it, although I use it in just such scenarios as you
illustrated. It is a kludge to get round (what seems to me) a deficiency in
the Select Case construct. The necessity to repeat the 'sUsr = ' makes it a
bit ugly,and it also has the (minor) disadvantage that intellisense does not
work as desired on the Case statements.

As far as problems are concerned, I haven't really had any but, as I say, I
try to avoid using it if I can.

Charles
 
Charles,

I was thinking about a same kind of sample as you made and thought, that is
not the way to go.

However it is nicely done

:-)

Cor
 
Looking at your code, the thing that is bothersome is that you are actually
doing three things in the single select statement.

1. Performing filtering logic on the test expression (sUsr)

2. Deciding whether to add the user to the list box.

3. Adding the item to the list box


There is nothing wrong with this and it is really a matter of style, but
when someone reads your single select statement they have to come to a
realization of really what your intentions are. If clean code, readability
and maintainability is a concern, one suggestion is you might want to split
these up to seperate methods, each method doing exactly one thing and the
method name describing exactly what it is trying to do. Again its only a
matter of style and my opinion.

'Prefilter logic
Private Function FilterUsers(ByVal User As String) As String
If User.StartsWith("SystemMailbox") Then
Return "SystemMailbox"
Else
Return User
End If
End Function

'Decision whether to add
Private Function ShouldAddToListBox(ByVal User As String) As Boolean
Dim Result As Boolean = False
Select Case User
Case "Guest", "TsInternetUser", "krbtgt", "quality7",
"SystemMailbox"
Result = False
Case Else
Result = True
End Select
Return Result
End Function


'Actual add to list box
Private Sub AddToListBox(ByVal User As String)
If ShouldAddToListBox(FilterUsers(User)) Then
LB.Items.Add(User)
End If
End Sub
 
I guess I'm from the old school but it seems that an IF then else statement
would work just fine here;

if left(sUsr) = "SystemMailBox" orelse sUsr="Guest" orelse...... then
'do nothing
else
ListBox1.Items.Add(sUsr)

end if
 
What i'm trying to do is find any sUsr variable that begins with
'SystemMailbox' and exclude it. If I use the code shown above it helpfully
informs me that the input string is not in the correct format. Is there any
way of using wildcards, or making the above sample correct?
I've been trying to work on my 'regular expressions' skills, and have a long
way to go,
Wouldn't this type of search be the kind of thing that 'regular expressions'
can help with ?
 
Hello Mike

You cannot do this: Case Left(sUsr, 13) = "SystemMailbox"
That will throw an exception.

In this case I would simply do conditional statements like

If sUsr = "Guest" AndAlso sUsr = "TsInternetUser" AndAlso "krbtgt" Then
'do nothing
ElseIf Left(sUsr, 13) = "SystemMailbox" Then
'do nothing
Else
ListBox1.Items.Add(sUsr)
End If

Something like that...
 
Back
Top