|
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
|
|
|
|
|
I never even considered that might be possible! Another tool for ye olde toolbox.
Spoke before I tried it; I only got it to work as System.Array.IndexOf ( a , v )
Luc Pattyn wrote: Regex approach
What about Linq?
-- modified at 19:31 Friday 30th November, 2007
|
|
|
|
|
lol nice
"I need build Skynet. Plz send code"
|
|
|
|
|
Thought of this one tonight.
public enum StringsOfSomeKind { SHAPE , FID , OID } ;
StringsOfSomeKind x = System.Enum.Parse ( typeof(StringsOfSomeKind) , currentFd.Name , true ) ;
if ( x != null ) ...
|
|
|
|
|
I created this operator Overoad and i get this error:
"One of the parameters of a binary operator must be the containing type"
here is the code:
public static string[] operator +(string[] a, string[] b)
{
string[] Combine;
int newStringSize = a.Length + b.Length;
int i = 0;
Combine = new string[newStringSize];
foreach (string val in a)
{
Combine[i++] = val;
}
foreach (string val in b)
{
Combine[i++] = val;
}
return Combine;
}
i don't understand the problem. it is complaining on "operator +" on the function declaration
Thanks,
Marios
|
|
|
|
|
Your operator overload needs to be contained in the class of the same type.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
ooh i see,
how do i add this to the string class? and isn't string[] not a class in the first place?
Thanks,
Marios
|
|
|
|