Easy question

  • Thread starter Thread starter Jake Forson
  • Start date Start date
J

Jake Forson

Hi there,

Is there a syntactically cleaner way to do this:

if (!(MyObject is Whatever))
{
// ...
}

Thanks.
 
Jake Forson said:
Try

if (MyObject isn't Whatever)

Thanks but there's no such keyword (with an apostrophe in it no less). Sure
you're not thinking of another language?
 
Jake Forson said:
Is there a syntactically cleaner way to do this:
if (!(MyObject is Whatever))
{
// ...
}

If you've got a giant class hierarchy that will use lots of these
kinds of tests, I've seen people provide
bool MyBaseClass.IsWhatever
Whatever MyBaseClass.AsWhatever
for every "whatever" that's common. It's not a OO-purist's approach,
but it does lead to code that's easier to read:

if (MyObject.IsWhatever)
{ MyObject.AsWhatever.whateverMethod();
}
 
Hi Lucian,

I wouldn't go as far as saying that's easier to read. It appears the same
to me.
 
No, I think he knew it was just as goofy as what he replied to.

Actually, I was giving him the benefit of the doubt since teenagers
sometimes respond with naive or insulting comments
 
Jake said:
Hi there,

Is there a syntactically cleaner way to do this:

if (!(MyObject is Whatever))
{
// ...
}

Hi Jake,

What you are doing seems odd, you are saying do something if an object
isn't of some type; but you would do this regardless of what the object
actually is. That seems odd to me, do you have an example of this
situation in context?

For now I would just do...

if(MyObject is Whatever) {}
else
{
//Object is not whatever
}

I'm not sure that's better than what you have though.

Cheers,
Gary
http://www.garyshort.org/
 
I don't know if it's cleaner but you could do:

if(MyObject.GetType() != typeof(Whatever))
{
}
 
Of course I just realized that doesn't take into account an inherited
type, just so you know...
 
Hi Jake,
What you are doing seems odd, you are saying do something if an object
isn't of some type; but you would do this regardless of what the object
actually is. That seems odd to me, do you have an example of this
situation in context?

For now I would just do...

if(MyObject is Whatever) {}
else
{
//Object is not whatever
}

I'm not sure that's better than what you have though.

Cheers,
Gary
http://www.garyshort.org/

Thanks for the feedback. Based on the responses I've seen so far there
apparently is no "cleaner" way to do it than what I originally posted. It's
simply a matter of testing if an object is *not* of a given type but I find
the syntax ugly (having to wrap it in brackets). It's worse however when
testing multiple conditions in the same statement, e.g.,

if (!(MyObject is Whatever) || OtherConditions)
{
// ...
}

While I haven't tried it yet, maybe the brackets can be eliminated depending
on the precedence rules but that also makes it hard to digest when other
conditions are present. Anyway, I knew I was probably grasping at straws and
it's hardly a critical issue. Thanks again (to all)
 
Of course I just realized that doesn't take into account an inherited
type, just so you know...

You can test for inherited types as well (there are functions for this) but
this whole approach is overkill (and bad for performance). Thanks anyway.
 
Thanks but there's no such keyword (with an apostrophe in it no less).
Sure you're not thinking of another language?

I was joking. Sorry to be unclear.

On the serious side, you might consider whether your design can be modified
to avoid this situation. I try to avoid type-checking in favor of more OOP
approaches where possible. In particular, I've never had to follow a branch
if an object -wasn't- of a specified type.

///ark
 
Hey,
Personally I'd try as much as possible to do it this way:
if(MyObject is Whatever){
}

Reason being the other way can lead to some gotchas, because if
MyObject can be ANYTHING including a string, a dataset, a char, a
SqlConnection .... this could trip you up in your logic
 
Mark Wilden said:
I was joking. Sorry to be unclear.

On the serious side, you might consider whether your design can be modified
to avoid this situation. I try to avoid type-checking in favor of more OOP
approaches where possible. In particular, I've never had to follow a branch
if an object -wasn't- of a specified type.

You've never implemented object.Equals then? ;)

That's the situation I use this construct in most often (although
depending on the exact nature of the equality check, calling GetType()
is sometimes more appropriate).

Admittedly in C# I'd use "as" and then a nullity check, but the
equivalent in Java is common...
 

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

Back
Top