matching a condition in a list

  • Thread starter Thread starter Masahiro Ito
  • Start date Start date
M

Masahiro Ito

I am trying to do an if statement that could have many different matches.
In sql I would do it like:

SELECT *
FROM Customer
WHERE Staff IN (10, 15, 23, 14)

In VB, I would do:

If Staff.Count = 10 or Staff.Count = 15 or Staff.Count = 23 or
Staff.Count = 14 Then
Console.writeline(Staff.Count.tostring)
End If

Is there any way to match to a list, without using an arraylist,
collection, icomparer?

I have a boolean, with a list of a dozen boolean variables. Can I do it
with boolean as well?

ie:

if (b1, b2, b3, b4, b4) = False Then
Console.writeline("At least one of those variables was false")
End If


Thanks for ideas!

Masa
 
* Masahiro Ito said:
I am trying to do an if statement that could have many different matches.
In sql I would do it like:

SELECT *
FROM Customer
WHERE Staff IN (10, 15, 23, 14)

In VB, I would do:

If Staff.Count = 10 or Staff.Count = 15 or Staff.Count = 23 or
Staff.Count = 14 Then
Console.writeline(Staff.Count.tostring)
End If

Is there any way to match to a list, without using an arraylist,
collection, icomparer?

I have a boolean, with a list of a dozen boolean variables. Can I do it
with boolean as well?

ie:

if (b1, b2, b3, b4, b4) = False Then
Console.writeline("At least one of those variables was false")
End If

Quick and Dirty:

\\\
MsgBox(CStr(IsInSet(12, 1, 2, 3, 4, 5, 99, 23, 12, 4, 87)))
MsgBox(CStr(IsInSet(12, 1, 2, 3, 4, 5, 99, 23, 13, 4, 87)))
..
..
..
Public Shared Function IsInSet(ByVal Item As Object, ByVal ParamArray SetItems() As Object) As Boolean
Array.Sort(SetItems)
Return (Array.BinarySearch(SetItems, Item) >= 0)
End Function
///
 
Is there any way to match to a list, without using an arraylist,
Array.Sort(SetItems)
Return (Array.BinarySearch(SetItems, Item) >= 0)

I find it a funny solution however.

Cor
 
* "Cor Ligthert said:
I find it a funny solution however.

Is there anything wrong with it (if yes, please let me know). The code
below will make "set" semantics more visible, because it will separate
the value to be searched from the set in which the search should be
performed:

\\\
MsgBox(CStr(IsInSet(12, New Object() {1, 2, 3, 4, 5, 99, 23, 12, 4, 87})))
MsgBox(CStr(IsInSet(12, New Object() {1, 2, 3, 4, 5, 99, 23, 13, 4, 87})))
..
..
..
Public Shared Function IsInSet(ByVal Item As Object, ByVal SetItems() As Object) As Boolean
Array.Sort(SetItems)
Return (Array.BinarySearch(SetItems, Item) >= 0)
End Function
///
 
Hi Whathsisname,
Herfried isn't as smart as he thinks he is.
You know I almost defend him, he is smart, however as well young and does
not take forever time for it.

:-)

I am glad you saw it too

:-)

Cor
 
Hi Herfried,
Is there anything wrong with it (if yes, please let me know).

My message was about that the OP definitly said about not using an from
Ilist derived class.

And also you did in my opinion not give an answer accoording to the SQL
clause WHERE IN.

In my opinion for this is the select case (a statement because it does look
so much at the "switch" was hated by me in the past), however in VB.net I
find it a very nice statement.

However your solution looks very nice and maybe I can use it in future, but
not to replace the select case.

:-)

Cor
 
Herfried,
Yours is the solution I would have gave, however I would not have sorted it
& used BinarySearch.

I would have simply used Array.IndexOf, realizing that its doing a linear
search. For the number of elements that are probably being pass I don't
consider the linear search a real performance problem. Given the overhead of
Sorting the array. Read I consider both acceptable. Of course if I were
calling the function a significant number of times on the same array, then
sorting the array once, and using BinarySearch would be a benefit, at the
expense of more management. Keeping in mind that we should only optimize
when the code has proven by profiling to be a performance problem.

Public Shared Function IsInSet(ByVal Item As Object, ByVal ParamArray
SetItems() As Object) As Boolean
Return (Array.IndexOf(SetItems, Item) >= 0)
End Function

I actually prefer the ParamArray version, as its closer to the SQL syntax.

Jay
 
* "Cor Ligthert said:
My message was about that the OP definitly said about not using an from
Ilist derived class.

The OP wrote that he wanted a solution without 'ArrayList',
'Collection', 'IComparer'. My solution uses an 'IComparer', I know, but
I don't see why there is a problem with using that.
And also you did in my opinion not give an answer accoording to the SQL
clause WHERE IN.

I had a look at the VB sample he provided in his code.
In my opinion for this is the select case (a statement because it does look
so much at the "switch" was hated by me in the past), however in VB.net I
find it a very nice statement.

I don't think that it has a good performance in all cases.
However your solution looks very nice and maybe I can use it in future, but
not to replace the select case.

?!?
 
Whatshisname said:
Herfried isn't as smart as he thinks he is.

He certainly has more credibility than anyone throwing insults using an alias....
Is there some reason you can't use your own name?

LFS
 
Cor

Herfried may be young but I sense a lack of humility. His answers seem to be drenched in a layer of smugness even when he is wrong.
 
Jay,

* "Jay B. Harlow said:
Yours is the solution I would have gave, however I would not have sorted it
& used BinarySearch.

I would have simply used Array.IndexOf, realizing that its doing a linear
search. For the number of elements that are probably being pass I don't
consider the linear search a real performance problem.

I agree. There will often be only a "hand full" of items, so a linear
search with runtime Theta(n) is better than sorting in O(n log(n))
and doing a binary search (which was in O(log(n)), AFAIR).
Given the overhead of
Sorting the array. Read I consider both acceptable. Of course if I were
calling the function a significant number of times on the same array, then
sorting the array once, and using BinarySearch would be a benefit, at the
expense of more management.

ACK. I didn't have my .NET reference here, so I missed 'Array.IndexOf'.
Keeping in mind that we should only optimize
when the code has proven by profiling to be a performance problem.

Public Shared Function IsInSet(ByVal Item As Object, ByVal ParamArray
SetItems() As Object) As Boolean
Return (Array.IndexOf(SetItems, Item) >= 0)
End Function

I actually prefer the ParamArray version, as its closer to the SQL syntax.

;-)
 
What difference does a name make? Will it make my point more valid? I call them as I see them. I think I struck a nerve ,otherwise, why would you care

BYTW: I'm under doctors orders not to use my real name.

----- Larry Serflaten wrote: ----



He certainly has more credibility than anyone throwing insults using an alias...
Is there some reason you can't use your own name

LF
 
Hi Herfried,

You are only returning the selection criteria when that fullfils the test

In this sentence
SELECT * FROM Customer WHERE Staff IN (10, 15, 23, 14)
Stands the * for a lot of dataitems.

Your solution would match when it was
SELECT Staff FROM Customer WHERE Staff IN (10, 15, 23, 14)

The Select in the situation from the OP is almost the same as the select
case where I pointed the OP on.

However this shows a lack of database knowledge in my opinion.
But you never said that was your strongest part.

Cor
 
Cor Ligthert said:
Have a look at the select case, that does what you want in my opinion.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/
html/vaconUsingSelectCase.asp
I hope this was what you where looking for?
Cor

Hi Cor,

Yes - Select Case can work in some cases. Certainly select works when I
have multiple paths to take, but in these cases I only want a single
condition to be met.

If any of these booleans in this list are True, then do ....

Anyway - I appreciate your help - and except in rare cases like the one
above, select case works well.

Masa
 
Cor Ligthert said:
Hi Herfried,

You are only returning the selection criteria when that fullfils the test

In this sentence
SELECT * FROM Customer WHERE Staff IN (10, 15, 23, 14)
Stands the * for a lot of dataitems.

Your solution would match when it was
SELECT Staff FROM Customer WHERE Staff IN (10, 15, 23, 14)

what's in the SELECT list makes no difference in the evaluation of the WHERE
....
 
* "Cor Ligthert said:
You are only returning the selection criteria when that fullfils the test

Did you have a look at the OP's pseudo code and his explanation?
In this sentence
SELECT * FROM Customer WHERE Staff IN (10, 15, 23, 14)
Stands the * for a lot of dataitems.

I know, but that was not the OP's question.
Your solution would match when it was
SELECT Staff FROM Customer WHERE Staff IN (10, 15, 23, 14)

You can call my function for all items in 'Customer', but the OP's
pseudo code showed that he didn't even want to do that. He wanted to
check if one of n boolean variables are set.
However this shows a lack of database knowledge in my opinion.

I have more theoretical knowledge about database systems and SQL as you
may think. I don't have any knowledge in ADO.NET (database access with
..NET).
But you never said that was your strongest part.

....
 
Whatshisname said:
What difference does a name make?
Will it make my point more valid?

It allows people to develop a character judgement based on your participation.
When you don't supply a name, people can only assume you don't want to be
held responsible for your contributions. If you are not willing to take on that
sort of responsibility, then that is a major strike in any character assessment.

There is no chane you can build as good a reputation with an alias as can
be done by posting under your real name. Everyone knows if you trash the
reputation of that name you can simply come back under a new name, so
you basically build no reputation at all. If you have no reputation for being
credible, then you have no credibility either.

I call them as I see them. I think I struck a nerve ,otherwise, why would you care?

If you, or anyone, has a problem with some post, then post your own response
and let the original poster decide who to listen to. There is no need to throw
insults and possibly start off a round of personal attacks. Your using an alias
is no shield, yet throwing insults from a nameless account is about the only thing
an alias is good for. There are no other good reasons for it.
BYTW: I'm under doctors orders not to use my real name.

Yeah right....

LFS
 
Back
Top