Main Page | Report this Page
.NET DotNet Forum Index  »  VB.NET Forum (Visual Basic .NET)  »  test for variable not in a list of values...
Page 1 of 1    

test for variable not in a list of values...

Author Message
Keith G Hicks...
Posted: Wed Nov 04, 2009 4:10 pm
Guest
How can I do this in vb.net?

Dim SomeVar as String = "red"

IF SomeVar NOT IN ("yellow", "green", "blue") THEN...
 
Keith G Hicks...
Posted: Wed Nov 04, 2009 4:41 pm
Guest
All I could find from my research is that with all the myriad of string
operators there are in vb.net this is not as simple as just a function. I'm
really surprised. I figured on something like this:

If CompareMany(SomeVar, "yellow", "green", "blue", "orange", "pink")

where the 1st param is the value that may or may not exist in the list that
follows and the list can contain any number of values. I used such a
function once in Delphi I think. Can't remember for sure which language.

Anyway, I ended up usign a case statement. Works fine but not as elegant as
I'd like.


"Keith G Hicks" <krh at (no spam) comcast.net> wrote in message
news:%23dfR7MZXKHA.4360 at (no spam) TK2MSFTNGP04.phx.gbl...
Quote:
How can I do this in vb.net?

Dim SomeVar as String = "red"

IF SomeVar NOT IN ("yellow", "green", "blue") THEN...
 
Armin Zingler...
Posted: Wed Nov 04, 2009 4:49 pm
Guest
Keith G Hicks schrieb:
Quote:
How can I do this in vb.net?

Dim SomeVar as String = "red"

IF SomeVar NOT IN ("yellow", "green", "blue") THEN...


dim myItems as string()={"yellow", "green", "blue"}

if not myitems.contains("red") then

end if

I'd create the array once only.


--
Armin
 
Captain Jack...
Posted: Wed Nov 04, 2009 5:05 pm
Guest
"Keith G Hicks" <krh at (no spam) comcast.net> wrote in message
news:eUNPVeZXKHA.5368 at (no spam) TK2MSFTNGP02.phx.gbl...
Quote:
All I could find from my research is that with all the myriad of string
operators there are in vb.net this is not as simple as just a function.
I'm really surprised. I figured on something like this:

If CompareMany(SomeVar, "yellow", "green", "blue", "orange", "pink")

where the 1st param is the value that may or may not exist in the list
that follows and the list can contain any number of values. I used such a
function once in Delphi I think. Can't remember for sure which language.


If you do want to do it as a function, this would be one way:

' Return True if the first argument isn't equal to any of the others
Public Function CompareMany(ByVal ValueToCompare As String, ByVal ParamArray
OtherValues() As String) As Boolean
For Each Element As String In OtherValues
If ValueToCompare = Element Then
Return False
End If
Next
Return True
End Function

Which can have as many arguments after the first one that you want, when you
call it.

--
Jack
 
Keith G Hicks...
Posted: Wed Nov 04, 2009 5:19 pm
Guest
Yeah, I like that Armin. Thanks.

I presume you meant this instead (parens on myItems instead of on string):

dim myItems() as string = {"yellow", "green", "blue"}


"Armin Zingler" <az.nospam at (no spam) freenet.de> wrote in message
news:uVmm1iZXKHA.4360 at (no spam) TK2MSFTNGP04.phx.gbl...
Quote:
Keith G Hicks schrieb:
How can I do this in vb.net?

Dim SomeVar as String = "red"

IF SomeVar NOT IN ("yellow", "green", "blue") THEN...


dim myItems as string()={"yellow", "green", "blue"}

if not myitems.contains("red") then

end if

I'd create the array once only.


--
Armin
 
Armin Zingler...
Posted: Wed Nov 04, 2009 5:33 pm
Guest
Keith G Hicks schrieb:
Quote:
Yeah, I like that Armin. Thanks.

I presume you meant this instead (parens on myItems instead of on string):

dim myItems() as string = {"yellow", "green", "blue"}

Yes, this also works. I prefer my version because the _whole_ type
("string-array") is following the "As" keyword.


--
Armin
 
Sergey Poberezovskiy...
Posted: Wed Nov 04, 2009 10:26 pm
Guest
Keith,

You can easily write a function, similar to the following:

Public Function IsIn(Of T)(value As T, ParamArray values As T()) As Boolean
Return Array.IndexOf(values, value) <> -1
End Function

This way you can compare other types, not just string with a very compact
syntax


"Keith G Hicks" wrote:

Quote:
All I could find from my research is that with all the myriad of string
operators there are in vb.net this is not as simple as just a function. I'm
really surprised. I figured on something like this:

If CompareMany(SomeVar, "yellow", "green", "blue", "orange", "pink")

where the 1st param is the value that may or may not exist in the list that
follows and the list can contain any number of values. I used such a
function once in Delphi I think. Can't remember for sure which language.

Anyway, I ended up usign a case statement. Works fine but not as elegant as
I'd like.


"Keith G Hicks" <krh at (no spam) comcast.net> wrote in message
news:%23dfR7MZXKHA.4360 at (no spam) TK2MSFTNGP04.phx.gbl...
How can I do this in vb.net?

Dim SomeVar as String = "red"

IF SomeVar NOT IN ("yellow", "green", "blue") THEN...



.
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Mon Nov 23, 2009 4:28 pm