Correct syntax for checking that value is in a set.

M

Mr. X.

Hello.
What is the correct syntax in VB.NET for checking that a value is in set of
value ?
pseudo code :
value in ('a','b','c') ...

Thanks :)
 
A

Andrew Morton

Mr. X. said:
What is the correct syntax in VB.NET for checking that a value is in
set of value ?
pseudo code :
value in ('a','b','c') ...

What sort of set? If it's a List, then

If myList.Contains(someValue) Then...
 
M

Mr. X.

What I meant is :
dim a as integer
....
a = 3

What should I do instead of :
if a = 1 or a = 2 or a = 5 or a = 18 or a = 33
The pseudo code for short solution is :
if a in (1,2,5,18,33) ...
but the above does't work in VB.NET.
What should I write instead of above ?

Thanks :)
 
P

Phill W.

What should I do instead of :
if a = 1 or a = 2 or a = 5 or a = 18 or a = 33
The pseudo code for short solution is :
if a in (1,2,5,18,33) ...
but the above does't work in VB.NET.
What should I write instead of above ?

Dim values As New List(Of Integer)(New Integer() {1,2,5,18,33})
If (values.Contains(a)) Then
. . .

HTH,
Phill W.
 
M

Mr. X.

If this is the only solution, so I shall manage it.
but it seems too complicated writing this on code - it's a basic thing.
Each time I need this I shall write "
if (new list(of integer)(New integer(){1,2,5,18,13}).contains(a) ...

Thanks :)
 
A

Armin Zingler

Am 28.06.2010 17:28, schrieb Mr. X.:
If this is the only solution, so I shall manage it.
but it seems too complicated writing this on code - it's a basic thing.
Each time I need this I shall write "
if (new list(of integer)(New integer(){1,2,5,18,13}).contains(a) ...

- You only have to create the list once. (At design time as well as at run time).
- You can make the same list accessible from different locations.
 
P

Phill W.

If this is the only solution, so I shall manage it.
but it seems too complicated writing this on code - it's a basic thing.

"Basic" to you and I, perhaps, but how would Our Friends in Redmond set
about implementing it as a Framework class?

It would be some sort of "Set" class that you could create based on a
given list of values.
Then it would have to have a Contains method (or Intersection, if this
is a Set).
Then, and this is the "Fun" part, it would have to be a Generic template
so that it could be used with objects of /any/ Type.

It's turning into a mini project all of its own.

YMMV, but I've often written my own methods for this sort of thing,
along the lines of:

Function IsIn( _
ByVal iValue As Integer _
, ByVal ParamArray iList As Integer() _
) As Boolean
For Each iEach As Integer In iList
If iEach = iValue Then
Return True
End If
Next
Return False
End Function

? IsIn( a, 1, 2, 5, 18, 13 )

HTH,
Phill W.
 
T

Tom Shelton

Phill W. expressed precisely :
"Basic" to you and I, perhaps, but how would Our Friends in Redmond set about
implementing it as a Framework class?

It would be some sort of "Set" class that you could create based on a given
list of values.
Then it would have to have a Contains method (or Intersection, if this is a
Set).


IEnumerable(Of T) already has an Intersect, Union, and Except as
Extension methods.
 
J

J.B. Moreno

Mr. X. said:
What I meant is :
dim a as integer
...
a = 3

What should I do instead of :
if a = 1 or a = 2 or a = 5 or a = 18 or a = 33
The pseudo code for short solution is :
if a in (1,2,5,18,33) ...
but the above does't work in VB.NET.
What should I write instead of above ?

The problem is that VB (and C#) doesn't have the concept of a set as a
base type, the FRAMEWORK has collections, but until 2010 VB didn't have
a simple collection initializer (which means that you couldn't leverage
that into what you want).

With VB.2010 you can do this:

array.IndexOf({1,2,5,18,33}, a) > -1

and it works (although an extension method would be much clearer).


(And why, why, is IndexOf a SHARED mehod instead of an instance method?
{1,2,5, 18, 23}.IndexOf(a) > -1 would almost be clear enough that you
wouldn't need an extension method).
 
T

Tom Shelton

Mr. X. laid this down on his screen :
Can you give me some sample on IEnumerable, please.

Thanks :)

Option Strict On
Option Explicit On

Module Module1

Sub Main()
Dim a() As Integer = {1, 2, 3}
Dim b() As Integer = {2, 3, 4}

Console.WriteLine("a = {1,2,3}")
Console.WriteLine("b = {2,3,4}")
Console.WriteLine()

Console.WriteLine("a intersect b")
For Each i As Integer In a.Intersect(b)
Console.WriteLine(i)
Next
Console.WriteLine()

Console.WriteLine("a union b")
For Each i As Integer In a.Union(b)
Console.WriteLine(i)
Next
Console.WriteLine()

Console.WriteLine("a except b")
For Each i As Integer In a.Except(b)
Console.WriteLine(i)
Next
Console.WriteLine()

End Sub

End Module

Output:

a = {1,2,3}
b = {2,3,4}

a intersect b
2
3

a union b
1
2
3
4

a except b
1

Press any key to continue . . .

HTH
 
B

Branco Medeiros

Mr. X said:
What I meant is :
dim a as integer
...
a = 3

What should I do instead of :
if a = 1 or a = 2 or a = 5 or a = 18 or a = 33
The pseudo code for short solution is :
if a in (1,2,5,18,33) ...
but the above does't work in VB.NET.
What should I write instead of above ?
<snip>

Select Case is your friend:

Select Case a
Case 1, 2, 5, 18, 33
DoSomething
Case 3, 4, 6 To 17, 19 To 32
DoSomethingElse
Case Else
DoSomethingEntirelyDifferent
End Select

HTH.

Branco.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top