second opinion

G

Guest

hey all,
i could have comma-separated values in a string
let's say: "4005","3","Updated" or really it can be individual strings it
doesn't matter.

what's the quickest or best way to get into this form:

"[field1]='4005' AND ([field2]='3' OR [field2]='Updated')"

thanks,
rodchar
 
P

Peter Rilling

Questions:

1) Are you always going to having only three values, or can you have more?
2) Will the desired format always be the same (that is with an AND and OR)
or will this be determined at runtime?
3) Will the field names always be "field1", "field2", ..., "fieldn"?
 
M

Mark Rae

i could have comma-separated values in a string
let's say: "4005","3","Updated" or really it can be individual strings it
doesn't matter.

what's the quickest or best way to get into this form:

"[field1]='4005' AND ([field2]='3' OR [field2]='Updated')"

1) Are there always three elements in the string - could there be fewer or
more?

2) Are [field1], [field2] and [field3] actual column names in a data table,
or are they themselves variables?

3) Is it always [field1] AND ([field2] OR [field2])?

If so, I'd suggest using the Split() function to parse the string into an
array, then maybe using StringBuilder to create your string output.
 
G

Guest

1) Are you always going to having only three values, or can you have more?
it will vary.
2) Will the desired format always be the same (that is with an AND and OR)
or will this be determined at runtime?
i'm just trying to build this as part of my where clause of my sqlCmd.Text.
3) Will the field names always be "field1", "field2", ..., "fieldn"?
no.

rodchar.

Peter Rilling said:
Questions:

1) Are you always going to having only three values, or can you have more?
2) Will the desired format always be the same (that is with an AND and OR)
or will this be determined at runtime?
3) Will the field names always be "field1", "field2", ..., "fieldn"?

rodchar said:
hey all,
i could have comma-separated values in a string
let's say: "4005","3","Updated" or really it can be individual strings it
doesn't matter.

what's the quickest or best way to get into this form:

"[field1]='4005' AND ([field2]='3' OR [field2]='Updated')"

thanks,
rodchar
 
K

Kevin Spencer

You actually have more than one issue here, one of which you don't seem to
be aware of. Let me deal with the one you know about first:

It is important to define your terms when asking a question! Take time to
explain fully what you're talking about, in order to get the best answer in
the least amount of questions. "[field1]" could refer to a form field, a
field in a class, or almost anything else you want to name "[field1]".

Also, as we are unaware of the context in which you're doing this, we can
only answer the question as generally as you have asked it. I'll try to
cover all the bases, but you'll get more answers by making our work easier!
;-)

You're apparently talking about a conditional statement, such as

If ([field1]='4005' AND ([field2]='3' OR [field2]='Updated') Then...

Now, you're comparing 3 "things" with 3 literal strings (I hope). These 3
"things" are apparently tied to only ONE EACH of the literal strings you're
comparing them to. These 3 "things" are apparently not in an array, or a
collection. If these statements are all correct, there is a design
consideration you should take into account. Linking un-linked "things" to
linked "things" is not good design. It implicitly links the un-linked
"things" together, by virtue of the fact that they are all individually
linked to a "thing" in a group of linked "things." This makes your code
design less extensible, and confusing to anyone that works on that code in
the future (you, for example, say, 6 months from now).

A good rule of thumb is, do not relate things that are unrelated.
Conversely, relate things that ARE related. If, for example, you have a
Collection of some type, and you want to validate the members of that
Collection to a group of literal strings, it might be wise to put the
strings into a StringCollection, which has the same dimensions as the
Collection it is linked to. A Collection is mutable; that is, its size can
change. So, linking a Collection to an array is a bad idea; linking a
Collection to a Collection is a good idea; linking an array to an array is a
good idea.

Also, if you link a Collection to a Collection, all methods which operate on
the one should also operate on the other, in order to keep them
synchronized, having the same number of members, for example. The same would
go with arrays.

Now, as to the issue which you are unaware of:

Assuming again, that you are evaluating a condition of some sort (it's so
hard to tell from VB, as "=" and "=" can mean two different things, but one
can glean from the context, just as the compiler does!), let's say that your
fragment is part of an If statment, for the sake of this discussion:

If ([field1]='4005' AND ([field2]='3' OR [field2]='Updated') Then...

Note that the condition as stated is ambiguous. Do you mean

If BOTH [field1] is equal to '4005' AND [field2] is equal to '3' OR ONLY
[field2] is equal to 'Updated" regardless of the values of [field1] and
[field2] Then...

OR do you mean

If BOTH [field1] is equal to '4005' AND [field2] is equal to '3' or is equal
to 'Updated', but not any other value than '3' or 'Updated' then...

In the first case, BOTH [field1] MUST BE equal to '4005' AND [field2] MUST
BE equal to '3'. However, if [field2] is equal to 'Updated' it doesn't
matter what [field1] is equal to. If [field1] is equal to anything other
than '4005' the statement evaluates to false, unless, and only if field2 is
equal to 'Updated'. If [field1] is equal to '4004' and [field2] is equal to
'3' the statement evaluates to false.

In the second case, [field1] MUST BE equal to '4005'. However, if [field2]
is equal to '3' OR field2 is equal to 'Updated' is doesn't matter what
[field1] is equal to. If [field1] is equal to anything other than '4005' the
statement evaluates to false, unless [field2] is equal to EITHER '3' OR
'Updated'. If [field1] is equal to '4004' and [field2] is equal to '3' the
statement evaluates to true.

The program will use the default interpretation of this statement, but do
you know what the default is? I could look it up, but I prefer the easy way
(which is also easier on anyone working with this code in the future - you,
for example, say, 6 months from now):

If (([field1]='4005' AND ([field2]='3') OR [field2]='Updated') Then...

OR

If ([field1]='4005' AND (([field2]='3' OR [field2]='Updated')) Then...

The parentheses create logical groupings of conditions. Any time you use OR
in statement having multiple conditions, be aware of this pitfall.

And PLEASE, take time in the future to more completely explain what you're
talking about. I don't have all day for this stuff! ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
I'd rather be a hammer than a nail.

rodchar said:
hey all,
i could have comma-separated values in a string
let's say: "4005","3","Updated" or really it can be individual strings it
doesn't matter.

what's the quickest or best way to get into this form:

"[field1]='4005' AND ([field2]='3' OR [field2]='Updated')"

thanks,
rodchar
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top