Results 1 to 9 of 9

Thread: [2005] IN statement?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397

    [2005] IN statement?

    is there an IN statement in .NET ?

    something like

    if X is in("11", 22", "33") then ...

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

    Re: [2005] IN statement?

    You could do this:
    vb.net Code:
    1. Select Case X
    2.     Case "11", "22", "33"
    3.         'Do something.
    4. End Select
    or you could do this:
    vb.net Code:
    1. If (New String() {"11", "22", "33"}).IndexOf(X) <> -1 Then
    2.     'Do something.
    3. End If
    The second option is more flexible.
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397

    Re: [2005] IN statement?

    thanks!

    this is what i went with

    Code:
        Private Function ValidFile(ByVal FileType)
            Dim bvalid As Boolean = True
    
            Select Case FileType
                Case ".html", ".doc", ".xls", ".dot", ".htm", ".txt", ".xml", ".ps", ".rtf", ".docx", ".gif", ".wmz", ".xif", ".tif"
                    bvalid = True
                Case Else
                    bValid = False
            End Select
    
            Return bvalid
    
        End Function

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] IN statement?

    If the boolean only gets set to true/false based on 1 single test (as is your example) then there is really no need to create an additional boolean variable in the routine.

    You may also want to do a .ToLower on the filetype incase for some reason it comes in as ".HTML" or something. This may or may not be a possibility depending on how this code is called.

    Code:
        Private Function ValidFile(ByVal FileType As String) As Boolean
    
            Select Case FileType.ToLower
                Case ".html", ".doc", ".xls", _
                     ".dot", ".htm", ".txt", _
                     ".xml", ".ps", ".rtf", _
                     ".docx", ".gif", ".wmz", _
                     ".xif", ".tif"
    
                    Return True
    
                Case Else
                    Return False
            End Select
    
        End Function

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] IN statement?

    Another variation -
    Code:
            Dim fileTypes() As String = {".html", ".doc", ".xls", ".dot", ".htm", ".txt", ".xml", ".ps", ".rtf", ".docx", ".gif", ".wmz", ".xif", ".tif"}
            If Array.IndexOf(fileTypes, ".html") >= 0 Then
                MessageBox.Show("File type Valid")
            Else
                MessageBox.Show("File type IS NOT Valid")
            End If
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [2005] IN statement?

    Quote Originally Posted by techgnome
    Another variation -
    Code:
            Dim fileTypes() As String = {".html", ".doc", ".xls", ".dot", ".htm", ".txt", ".xml", ".ps", ".rtf", ".docx", ".gif", ".wmz", ".xif", ".tif"}
            If Array.IndexOf(fileTypes, ".html") >= 0 Then
                MessageBox.Show("File type Valid")
            Else
                MessageBox.Show("File type IS NOT Valid")
            End If
    -tg

    TG, you hard coded ".html" in your indexof method. I am sure it was just a slip of the code It should be fileType instead.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] IN statement?

    it was a slip and it wasn't. I jsut threw it together behind a button click and didn't mess with creating a function out of it... but you get the idea.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397

    Re: [2005] IN statement?

    Quote Originally Posted by kleinma
    If the boolean only gets set to true/false based on 1 single test (as is your example) then there is really no need to create an additional boolean variable in the routine.
    thank you!

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

    Re: [2005] IN statement?

    If you were going to declare a Boolean variable though, you wouldn't initialise it to True and then use two different cases to set it to either True or False. If you initialise the variable then you only need one case to set it to the other value. If you have two cases then there's no point initialising. I'm of the opinion that all functions should have a single Return statement at the end, so I'd rewrite your function like this:
    Code:
        Private Function ValidFile(ByVal FileType)
            Dim bvalid As Boolean = False
    
            Select Case FileType
                Case ".html", ".doc", ".xls", ".dot", ".htm", ".txt", ".xml", ".ps", ".rtf", ".docx", ".gif", ".wmz", ".xif", ".tif"
                    bvalid = True
            End Select
    
            Return bvalid
        End Function
    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