Results 1 to 9 of 9

Thread: select case [RESOLVED]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489

    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:
    1. Select Case true
    2.             Case Node.StartsWith("N25") Or _
    3.                       Node.StartsWith("N30") Or _
    4.                       Node.StartsWith("N35") Or _
    5.                       Node.StartsWith("N40") Or _
    6.                       Node.StartsWith("N45")
    7.                 Return "192.168.4.237"
    8.  
    9.             Case Node.StartsWith("N55") Or _
    10.                       Node.StartsWith("N65") Or _
    11.                       Node.StartsWith("N70") Or _
    12.                       Node.StartsWith("N50")
    13.                 Return "192.168.4.235"
    14.  
    15.             Case Node.StartsWith("N95") Or _
    16.                   Node.StartsWith("N75") Or _
    17.                   Node.StartsWith("N80") Or _
    18.                   Node.StartsWith("N85") Or _
    19.                   Node.StartsWith("N90") Or _
    20.                   Node.StartsWith("N100") Or _
    21.                   Node.StartsWith("N105")
    22.                 Return "192.168.4.239"
    23.  
    24.  
    25.         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.

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    alright, I came up with this:
    VB Code:
    1. Private Function Server(ByVal Node As String) As String
    2.         'This needs to determine what ip address to connect
    3.         Dim prefix As String = Node.Substring(0, 3)
    4.  
    5.         Select Case prefix
    6.  
    7.             Case "N25", "N30", "N35", "N40", "N45"
    8.                 Return "192.168.4.237"
    9.  
    10.             Case "N55", "N65", "N70", "N50"
    11.                 Return "192.168.4.235"
    12.  
    13.             Case "N95", "N75", "N80", "N85", "N90", "N100", "N105"
    14.                 Return "192.168.4.239"
    15.  
    16.  
    17.         End Select
    18.  
    19.  
    20.     End Function
    is that the best way? will that even work?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    any comments on this?

  4. #4
    PowerPoster SuperSparks's Avatar
    Join Date
    May 2003
    Location
    London, England
    Posts
    265
    That should work just fine, I've done it that way many times.
    Nick.

  5. #5
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    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:
    1. Select Case Node.Substring(0, 3)

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  7. #7
    Hyperactive Member dogfish227's Avatar
    Join Date
    Oct 2002
    Location
    GA
    Posts
    409
    you could do somehting like this
    VB Code:
    1. Private Function Server(ByVal Node As String) As String
    2.         'This needs to determine what ip address to connect
    3.         Dim prefix As Integer = cint(Node.Substring(1, 3))
    4.  
    5.         Select Case prefix
    6.  
    7.             Case 25 to 45
    8.                 Return "192.168.4.237"
    9.  
    10.             Case 50 to 70
    11.                 Return "192.168.4.235"
    12.  
    13.             Case 75 to 105
    14.                 Return "192.168.4.239"
    15.  
    16.  
    17.         End Select
    18.  
    19.  
    20.     End Function
    that should work so long as the fourth charactor in the node.substring is not a letter. (for the two digit numbers)

  8. #8
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    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.

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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
  •  



Click Here to Expand Forum to Full Width