HighTechTalks DotNet Forums  

Correct syntax for checking that value is in a set.

VB.net microsoft.public.dotnet.languages.vb


Discuss Correct syntax for checking that value is in a set. in the VB.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Mr. X.
 
Posts: n/a

Default Correct syntax for checking that value is in a set. - 06-25-2010 , 05:26 AM






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

Reply With Quote
  #2  
Old   
Andrew Morton
 
Posts: n/a

Default Re: Correct syntax for checking that value is in a set. - 06-25-2010 , 05:41 AM






Mr. X. wrote:
Quote:
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...

--
Andrew

Reply With Quote
  #3  
Old   
Mr. X.
 
Posts: n/a

Default Re: Correct syntax for checking that value is in a set. - 06-26-2010 , 01:15 PM



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

"Andrew Morton" <akm (AT) in-press (DOT) co.uk.invalid> wrote

Quote:
Mr. X. wrote:
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...

--
Andrew

Reply With Quote
  #4  
Old   
Phill W.
 
Posts: n/a

Default Re: Correct syntax for checking that value is in a set. - 06-28-2010 , 06:33 AM



On 26/06/2010 19:15, Mr. X. wrote:

Quote:
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.

Reply With Quote
  #5  
Old   
Mr. X.
 
Posts: n/a

Default Re: Correct syntax for checking that value is in a set. - 06-28-2010 , 10:28 AM



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

"Phill W." <p-.-a-.-w-a-r-d-@-o-p-e-n-.-a-c-.-u-k> wrote

Quote:
On 26/06/2010 19:15, Mr. X. wrote:

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.

Reply With Quote
  #6  
Old   
Armin Zingler
 
Posts: n/a

Default Re: Correct syntax for checking that value is in a set. - 06-28-2010 , 10:50 AM



Am 28.06.2010 17:28, schrieb Mr. X.:
Quote:
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.


--
Armin

Reply With Quote
  #7  
Old   
Phill W.
 
Posts: n/a

Default Re: Correct syntax for checking that value is in a set. - 06-30-2010 , 07:08 AM



On 28/06/2010 16:28, Mr. X. wrote:
Quote:
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.

Reply With Quote
  #8  
Old   
Tom Shelton
 
Posts: n/a

Default Re: Correct syntax for checking that value is in a set. - 06-30-2010 , 08:22 AM



Phill W. expressed precisely :
Quote:
On 28/06/2010 16:28, Mr. X. wrote:
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).

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


--
Tom Shelton

Reply With Quote
  #9  
Old   
J.B. Moreno
 
Posts: n/a

Default Re: Correct syntax for checking that value is in a set. - 06-30-2010 , 11:33 PM



In article <#0uDluVFLHA.4504 (AT) TK2MSFTNGP02 (DOT) phx.gbl>, Mr. X.
<nospam (AT) nospam_please (DOT) com> wrote:

Quote:
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).

--
J.B. Moreno

Reply With Quote
  #10  
Old   
Mr. X.
 
Posts: n/a

Default Re: Correct syntax for checking that value is in a set. - 07-08-2010 , 12:14 PM



Can you give me some sample on IEnumerable, please.

Thanks

"Tom Shelton" <tom_shelton (AT) comcast (DOT) invalid> wrote

Quote:
Phill W. expressed precisely :
On 28/06/2010 16:28, Mr. X. wrote:
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).


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


--
Tom Shelton


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.