|
-
May 18th, 2004, 10:24 AM
#1
Thread Starter
Frenzied Member
select case [RESOLVED]
If you need to use a select case statement and you want to determine the value of the first few characters of a string, is there a way to 'include' a range rather than using 'or' statments?
ex:
VB Code:
Select Case true
Case Node.StartsWith("N25") Or _
Node.StartsWith("N30") Or _
Node.StartsWith("N35") Or _
Node.StartsWith("N40") Or _
Node.StartsWith("N45")
Return "192.168.4.237"
Case Node.StartsWith("N55") Or _
Node.StartsWith("N65") Or _
Node.StartsWith("N70") Or _
Node.StartsWith("N50")
Return "192.168.4.235"
Case Node.StartsWith("N95") Or _
Node.StartsWith("N75") Or _
Node.StartsWith("N80") Or _
Node.StartsWith("N85") Or _
Node.StartsWith("N90") Or _
Node.StartsWith("N100") Or _
Node.StartsWith("N105")
Return "192.168.4.239"
End Select
here, I'm checking for a 'prefix' and it returns an ip address. is there a better way to write that?
Last edited by Andy; Aug 13th, 2004 at 12:11 PM.
-
May 18th, 2004, 10:28 AM
#2
Thread Starter
Frenzied Member
alright, I came up with this:
VB Code:
Private Function Server(ByVal Node As String) As String
'This needs to determine what ip address to connect
Dim prefix As String = Node.Substring(0, 3)
Select Case prefix
Case "N25", "N30", "N35", "N40", "N45"
Return "192.168.4.237"
Case "N55", "N65", "N70", "N50"
Return "192.168.4.235"
Case "N95", "N75", "N80", "N85", "N90", "N100", "N105"
Return "192.168.4.239"
End Select
End Function
is that the best way? will that even work?
-
May 18th, 2004, 06:03 PM
#3
Thread Starter
Frenzied Member
-
May 18th, 2004, 06:13 PM
#4
PowerPoster
That should work just fine, I've done it that way many times.
-
May 18th, 2004, 07:18 PM
#5
Fanatic Member
I have also done that (but shy away from the Select Case true). Also, you don't need to instantiate a new variable - prefix - just do (at least you should be able to do):
VB Code:
Select Case Node.Substring(0, 3)
-
May 18th, 2004, 07:30 PM
#6
PowerPoster
Hi,
Just a few unenlightened comments.
If prefix is going to contain 3 characters, you can't search for N100 or N105.
For tidiness, you could leave out the "N" from Prefix as it is always there and you can leave out "192.168.4." from the Return and add it later.
If you do leave out the "N" and set prefix to the following 3 characters, you could amend your Case statements for under 100 to include a wildcard
I have never succeeded in using Select Case to compare more than 2 arguments per case (but I have not tried what you are doing)
Last edited by taxes; May 19th, 2004 at 04:24 AM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
May 18th, 2004, 09:24 PM
#7
Hyperactive Member
you could do somehting like this
VB Code:
Private Function Server(ByVal Node As String) As String
'This needs to determine what ip address to connect
Dim prefix As Integer = cint(Node.Substring(1, 3))
Select Case prefix
Case 25 to 45
Return "192.168.4.237"
Case 50 to 70
Return "192.168.4.235"
Case 75 to 105
Return "192.168.4.239"
End Select
End Function
that should work so long as the fourth charactor in the node.substring is not a letter. (for the two digit numbers)
-
May 19th, 2004, 03:32 AM
#8
Learn how to use "Regular Expressions" (Regex) which is perfect for this kind of processing. Regex Classes are part of the .Net framework. There's loads of stuff in MSDN and check out this book:
"Mastering Regular Expressions" - O'Reilly
Great book.
I don't live here any more.
-
May 19th, 2004, 04:23 AM
#9
PowerPoster
Hi dogfish227,
"that should work so long as the fourth charactor in the node.substring is not a letter. (for the two digit numbers)"
I think it will ONLY work for two digit numbers if the fourth character IS a letter
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
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
|