5

I've been programming in JAVA and C all my years at Uni, but now I'm learning C# and building a small application, and I've found troubles with this:

if (taxonType.Equals(null) ¦¦ taxonID == -1)

I get a red underline for this conditional, and I don't really know why, because according to what I've seen that should be ok, but it is not. Is there something I'm missing?

thank you all in advance, Victor


Thank you all!!! I was getting mad about this. The thing is that I'm Spanish and I'm used to have the pipe key | exactly in the same place where ¦ is in the American configuration... I was seeing this ¦ strange, but I thought it was the same...

Thanks for the fast reply!! Victor

1
  • what exact error are you getting describing the "red underline"?
    – Marek
    Mar 23, 2010 at 10:27

9 Answers 9

11
if (taxonType == null || taxonID == -1)

Modified for correct code as well as answering the question asked

2
  • 2
    As already said in other answers, if taxonType is actually a null reference, your test will throw...
    – Seb
    Mar 23, 2010 at 10:30
  • 1
    Code updated, was originally answering the question asked rather than fixing the remainder of the code. If he sends the application I can finish that too ;)
    – rrrr-o
    Mar 23, 2010 at 10:35
5

What is '¦'? Are you sure it is the same character as '|'?

1
  • 1
    @Filip sometimes a question provides a better answer than a long winded answer, especially if the original question was phrased so unclearly ("I get red underline")
    – Marek
    Mar 23, 2010 at 11:42
4

I don't know the types of taxonType and taxonID but it should work like this:

if (taxonType == null || taxonID == -1) 
2

It looks like your are using some special character, which is not recognized as the default pipe (|) character. Perhaps you should check your keyboard settings?

2

the problem is that if taxonType really is Null then using the .Equals function on it would throw a NullReferenceException. go with == instead of Equals

2

if taxonType is actually null this will throw an exception because you'll be trying to call its Equals method.

if you said if (taxOnType == null) || taxonID == -1) you'd be fine.

1

You should use "||" instead of "¦¦".

1

Your test for null should not use .Equals, since if the object is null, the attempt to access a member will throw a NullReferenceException.

Use:

if (taxonType == null || taxonID == -1) 

It is also not clear if you are using the correct pipe character |, as yours shows up as ¦.

1
  • Yes that's it, I tried the equals because taxonType was a string, but my first attempt was the code you wrote. As I kept getting the red underline I tried different kinds of conditions to see if I was getting it wrong...Thanks:)
    – vikitor
    Mar 23, 2010 at 10:32
1

if (String.IsNullOrEmpty(taxonType) || taxonID < 0)

and make taxon as a STRUCT

2
  • String.IsNullOrEmpty is only for use with strings. we don't know what type taxonType is. Furthermore String.IsNullOrEmpty would return true if taxonType is "". That is not the behavior we want. Use taxonType == null instead. TaxonID<0 is not sufficient. Mar 23, 2010 at 11:10
  • taxonType is a string, as I'm using it to create a search form, I set taxonType to null if there is no taxonType selected and taxonID is -1 if there is no taxonID selected in the form... It is just to divert the control flow. Thank you for the advices anyway, it helps me to rethink my application with tools that I didn't know or thought before :)
    – vikitor
    Mar 23, 2010 at 13:15

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.