|
hi there still new to c# so forgive this simple question
but is there an operator i can use to perform OR/AND operations on strings
ie
if (currentFd.Name.ToUpper() != "SHAPE" || "FID" || "OID")
{
// do whatever
}
id prefer not to turn this into a case statement and embedded if statements seem pretty ridiculous as well, but i searched around and had no luck finding a solution. anybody out there help?? thanks in advance for helping a newbie
|
|
|
|
|
use the .Equals() method
if(currentFd.Name.ToUpper().Equals("SHAPE") || currentFd.Name.ToUpper().Equals("FID") || currentFd.Name.ToUpper().Equals("OID"))
{
}
...only use cascading if statements if you are really pissed off at your grader
-- modified at 14:39 Thursday 29th November, 2007
"I need build Skynet. Plz send code"
|
|
|
|
|
Alaric_ wrote: == and != can't be applied to strings in that way
What? It's not a problem with the == operator.
|
|
|
|
|
...let me rephrase....(in my opinion) == shouldn't be applied to strings in that way because strings are not actually value types....if you were checking it against null, then "str==null" would be acceptable because you would be comparing whether the value of the reference was equal to null or not. I did not intend to say that "==" and "!=" were causing his problems
...if you want to test whether str is equal to "some string value",
str = "some literal string value";
if(str == "some literal string value"){}
if(str.Equals("some literal string value")){}
the value of the reference to str IS NOT EQUAL to "some literal string value"
but the value stored by str IS EQUAL to "some literal string value"
"I need build Skynet. Plz send code"
|
|
|
|
|
understood..
thanks for clarification
|
|
|
|
|
Well I disagree, in my opinion:
if(str == "some literal string value"){}
if(str.Equals("some literal string value")){}
|
|
|
|
|
I second that.
The most readable syntax is the right one for me (provided it does what is intended).
And it also avoids the potential null problem.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
I thought that == did the exact same thing as .Equals
And i don't mean the result is the same. I mean the actual comparison is exactly the same.
My current favourite word is: PIE!
I have changed my name to my regular internet alias. But don't let the 'Genius' part fool you, you don't know what 'SK' stands for.
-The Undefeated
|
|
|
|
|
Hi,
this is how I see it, after some experiments:
str=="abc" generates MSIL containing a call to the static method
String.op_Equality(string,string)
str.Equals("abc") generates MSIL containing a virtual call to the
instance method String.Equals(string)
when str is a valid (non-null) string reference, both methods are supposed to do
exactly the same.
But when str happens to be null, the first will succeed, whereas the second will
throw a NullReferenceException.
Given all this, I never plan on using the latter.
BTW: there also is the static method String.Compare with many overloads, yielding
more than just an equal/unequal result.
The C# compiler treats strings in special ways, to make life easier. To name a few:
- string literals (with double quotes)
- string comparison (the shorthand way)
- switching on a string.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
thanks for the prompt reply... but I have no problem using statements in such a way in c# in fact they are virtually the same. Still waiting for an answer regarding the or statement. thanks
"For reference types where == has not been overloaded, it compares whether two references refer to the same object - which is exactly what the implementation of .Equals does in System.Object."
|
|
|
|
|
String.Compare(string1, string2, false) == 0 is probably the more preferred method as it will not throw a null reference exception.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
thanks for the tip, but I'm Still wondering about the multiple string comparison
|
|
|
|
|
Hi, the first reply you got contained the answer.
you can't do if (a == b || c || d) ... where a,b,c,d all have the same type (other
than bool), but you should do if (a==b || a==c || a==d) ...
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
yeah thanks i just figured that out,
my thanks to everyone who helped
|
|
|
|
|
actually....operating on a string that was actually null would probably need to throw some kind of Custom Exception, unless the string being operated on is some sort of an optional field. You would probably want to break your validation out into separate steps
private void whatever(string parameter)
{
try
{
if(parameter == null || parameter.Equals(""))
{
throw new CustomBusinessException("Parameter is a required field");
}
else
{
if(parameter.Equals(someComparisonString))
{
}
else
{
throw new CustomBusinessException("Parameter is invalid");
}
}
}
catch(CustomBusinessException cbex)
{
throw cbex;
MessageBox.Show(cbex.Message);
}
}
"I need build Skynet. Plz send code"
|
|
|
|
|
String.IsNullOrEmpty it is cleaner.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
You could use a static Dictionary.
private static System.Collection.Generic.Dictionary<string,object> dic ;
static classname()
{
dic = new System.Collection.Generic.Dictionary<string,object>
(
System.StringComparer.CurrentCultureIgnoreCase
) ;
dic.Add ( "SHAPE" , null ) ;
dic.Add ( "FID" , null ) ;
dic.Add ( "OID" , null ) ;
}
...
if ( dic.ContainsKey ( currentFd.Name ) ) ...
|
|
|
|
|
hey thanks for the help. seems a bit overkill compared to what i was looking for but i think this might be the best way to go. thanks again i appreciate it
|
|
|
|
|
again...if you are simply wanting to compare a string against multiple values, you can use the format:
string str = "xyz";
if(str.ToUpper().Equals("XYZ") || str.ToUpper().Equals("XXX"))
{
}
"I need build Skynet. Plz send code"
|
|
|
|
|
thank you - this is exactly what i was trying to figure out... thanks for your persistence
|
|
|
|
|
Use of the Dictionary:
Removes the need for the ToUpper
Improves readability
Makes maintenance easier
|
|
|
|
|
What's wrong with a switch?
switch (currentFd.Name.ToUpper()) {
case "SHAPE":
case "FID":
case "OID":
break;
}
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Guffa wrote: What's wrong with a switch?
Nothing wrong here, I even like this one.
It is more readable than the ORed if statement.
I hope exactly the same MSIL gets generated though.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|
|
I forgot about this solution:
if ( "#SHAPE#FID#OID#".IndexOf ( "#" + currentFd.Name.ToUpper() + "#" ) != -1 ) ...
(Yes, it'll give a false positive if the currentFd.Name is similar to "SHAPE#FID".)
|
|
|
|
|
or with another poor man's dictionary:
private static string[] keywords=new string[] { "SHAPE", "FID", "OID" };
...
if (Array.IndexOf(keywords, currentFd.Name.ToUpper()) >= 0) ...
BTW: I am still expecting some one to come up with a Regex approach...
-- modified at 19:38 Friday 30th November, 2007:
if (keywords.IndexOf(currentFd.Name.ToUpper()) >= 0) ...
was incorrect as pointed out by PIEBALD.
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips:
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use PRE tags to preserve formatting when showing multi-line code snippets
|
|
|
|