|
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
|
|
|
|
|
Well it derives from System.Array doesn't it?
By the way, I implement that method as:
public static string[]
Concat
(
string[] first
,
string[] second
)
{
string[] result = null ;
if ( ( first != null ) && ( second != null ) &&
( first.Rank == 1 ) && ( second.Rank == 1 ) )
{
result = new string [ first.Length + second.Length ] ;
first.CopyTo ( result , 0 ) ;
second.CopyTo ( result , first.Length ) ;
}
else
{
throw new System.Exception ( "Can only concatenate one-dimensional arrays" ) ;
}
return ( result ) ;
}
|
|
|
|
|
AFAIK you can't do what you want, since Array class isn't yours and probably sealed.
If you insist on using the + operator I am afraid the only thing you can do is come up
with an entirely new class (myStringCollection) and implement all the properties, methods
and operators you need. Such class obviously could contain an array, an ArrayList or a
List<string> to hold the actual data.
If the + operator is not mandatory, have a look at the StringCollection class, it knows
how to accept and add string arrays.
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
|
|
|
|
|
No one mentioned Extension Methods, mightn't they help here?
|
|
|
|
|
PIEBALDconsult wrote: No one mentioned Extension Methods, mightn't they help here?
I haven't studied the subject and am not planning to any time soon. There still is much
to digest in 2.0
Do you expect them to be available on a sealed class (I'm still assuming Array is sealed).
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
|
|
|
|
|
Luc Pattyn wrote: on a sealed class
That seems to be the point of them; they behave as if they are part of the class even though they aren't.
I don't expect to use them either, but this may be a case where they are useful. The problem being that the arrays have to be compatible.
|
|
|
|