Why is following syntax not supported in C#?

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

IIRC in C++ we can declare variables in if statements. Why can't we do that
in C#?
Lets see that example:

object obj = GetSomeObject();

if ((Customer cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((Supplier supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}

I now that that specific example could be solved better but this is just an
example to give you the idea what I mean.

The problem is that at the moment I have to write it like that:

Customer cust;
Supplier supp;

if (cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}

this unnecessarily expands the visibility of both variables to the outer
scope.
What do you think about that idea?
 
Try (Customer)obj to cast your obj to Customer type. The "As" keyword is
VB. Something like this (untested, but it might point you in the general
direction):

if ((Customer cust = (Customer)obj)!=null)
{
cust.DoStuff();
}
else if ((Supplier supp = (Supplier)obj)!=null)
{
supp.DoSomeOtherStuff();
}
 
Hi Cody,

You can use the 'is' keyword

object obj = GetSomeObject();

if(obj is Customer)
{
((Customer)obj).DoStuff();
}
else if(obj is Supplier)
{
((Supplier)obj).DoSomeOtherStuff();
}

It wouldn't be exactly as your C++ code though.
 
Fist, casting throws an exception which is not the intent of my code, second
the as keyword *is* supported in C#. It doesn't throw but instead returns
null.
 
cody said:
IIRC in C++ we can declare variables in if statements. Why can't we do
that
in C#?
Lets see that example:

object obj = GetSomeObject();

if ((Customer cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((Supplier supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}

I now that that specific example could be solved better but this is just
an
example to give you the idea what I mean.

The problem is that at the moment I have to write it like that:

Customer cust;
Supplier supp;

if (cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}

this unnecessarily expands the visibility of both variables to the outer
scope.
What do you think about that idea?

Don't know the exact reason, but no-one stops you from reducing the
visibility of the variables like this:
.....// outer scope, cust and supp invisible
{
Customer cust;
Supplier supp;

if (cust = obj as Customer)!=null)
{
cust.DoStuff();
}
else if ((supp = obj as Supplier )!=null)
{
supp.DoSomeOtherStuff();
}
}
....

Willy.
 
You can use the 'is' keyword
object obj = GetSomeObject();

if(obj is Customer)
{
((Customer)obj).DoStuff();
}
else if(obj is Supplier)
{
((Supplier)obj).DoSomeOtherStuff();
}

That has the disadvantage that the typetest has to be performed twice, once
for the "is" and once for that actual cast. But if I think about it, the
jitter would certainly able to optimize this away, the question is, does it
actually do it? :)

((Customer)obj).DoStuff() is very ugly and you cannot perform more that one
operation on the object. So you have to write it like that which is longer:

if(obj is Customer)
{
Customer cust = obj as Customer;
cust.DoStuff();
}
 
Why not implement an interface or abstract base class with a DoStuff()
method, and inherit both Customer and Supplier from it, then it doesn't
matter whether "obj" is one or the other, you just check it for null and call
the method:

interface IMyEntity
{
public DoStuff();
};

public class Customer : IMyEntity
{
public DoStuff() { /* do customer stuff */ }
};

public class Supplier : IMyEntity
{
public DoStuff() { /* do supplier stuff */ }
};

....

IMyEntity e = (IMyEntity)GetSomeObject();

if (e != null) e.DoStuff();
 
Hi,

I too prefer this method, IMO it's the clearer one

you could also go by :

{
Customer cust = obj as Customer ;
Supplier supp= obj as Supplier;

if (cust !=null)
{
cust.DoStuff();
}
else if ( supp !=null)
{
supp.DoSomeOtherStuff();
}
}

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
 
Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

I too prefer this method, IMO it's the clearer one

you could also go by :

{
Customer cust = obj as Customer ;
Supplier supp= obj as Supplier;

if (cust !=null)
{
cust.DoStuff();
}
else if ( supp !=null)
{
supp.DoSomeOtherStuff();
}
}

Cheers,

Agreed, simplicity and readability might have been the reason the language
designers didn't opt for the "if " construct.

Willy.
 
Back
Top