is there an IN statement in .NET ?
something like
if X is in("11", 22", "33") then ...
Printable View
is there an IN statement in .NET ?
something like
if X is in("11", 22", "33") then ...
You could do this:or you could do this:vb.net Code:
Select Case X Case "11", "22", "33" 'Do something. End SelectThe second option is more flexible.vb.net Code:
If (New String() {"11", "22", "33"}).IndexOf(X) <> -1 Then 'Do something. End If
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
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
Another variation -
-tgCode: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
Quote:
Originally Posted by techgnome
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.
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
thank you!Quote:
Originally Posted by kleinma
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