|
-
Jun 19th, 2008, 10:09 AM
#1
Thread Starter
Hyperactive Member
[2005] IN statement?
is there an IN statement in .NET ?
something like
if X is in("11", 22", "33") then ...
-
Jun 19th, 2008, 10:13 AM
#2
Re: [2005] IN statement?
You could do this:
vb.net Code:
Select Case X
Case "11", "22", "33"
'Do something.
End Select
or you could do this:
vb.net Code:
If (New String() {"11", "22", "33"}).IndexOf(X) <> -1 Then
'Do something.
End If
The second option is more flexible.
-
Jun 19th, 2008, 10:56 AM
#3
Thread Starter
Hyperactive Member
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
-
Jun 19th, 2008, 11:24 AM
#4
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
-
Jun 19th, 2008, 11:24 AM
#5
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
-
Jun 19th, 2008, 11:27 AM
#6
Re: [2005] IN statement?
 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.
-
Jun 19th, 2008, 12:06 PM
#7
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
-
Jun 19th, 2008, 12:54 PM
#8
Thread Starter
Hyperactive Member
Re: [2005] IN statement?
 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!
-
Jun 19th, 2008, 06:30 PM
#9
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
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
|