473,320 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Why is following syntax not supported in C#?

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?
Nov 17 '05 #1
13 1021
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();
}

"cody" <de********@gmx.de> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
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?

Nov 17 '05 #2
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.


On Thu, 26 May 2005 15:09:14 +0200, cody <de********@gmx.de> wrote:
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?


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #3
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.
"Michael C#" <ho***@boutdat.com> schrieb im Newsbeitrag
news:#D**************@TK2MSFTNGP12.phx.gbl...
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();
}

"cody" <de********@gmx.de> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
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?


Nov 17 '05 #4

"cody" <de********@gmx.de> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
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.
Nov 17 '05 #5
> 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();
}
Nov 17 '05 #6
KH
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();

"cody" wrote:
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?

Nov 17 '05 #7
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
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:uA**************@TK2MSFTNGP09.phx.gbl...

"cody" <de********@gmx.de> wrote in message
news:ef**************@TK2MSFTNGP10.phx.gbl...
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.

Nov 17 '05 #8

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2****************@TK2MSFTNGP09.phx.gbl...
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


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

Willy.
Nov 17 '05 #9
> 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();
}
}


That would mean that all expressions are evaluated always, not just if
needed.
How would you implement something like that:

if ((Customer cust = GetCust())!=null)
{
}
else if ((Supplier supp = GetSupp())!=null)
{
}
Nov 17 '05 #10
Hi,
Don;t lost your time thinking about such a little overhead, unless you have
it inside a loop there is no point worring about that.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"cody" <de********@gmx.de> wrote in message
news:u6*************@TK2MSFTNGP15.phx.gbl...
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();
}
}


That would mean that all expressions are evaluated always, not just if
needed.
How would you implement something like that:

if ((Customer cust = GetCust())!=null)
{
}
else if ((Supplier supp = GetSupp())!=null)
{
}

Nov 17 '05 #11
Wrong. "as" is not VB. It's C#.

It casts the given reference to the given type, returning null if the
cast fails. A direct cast throws an exception, which can be costly.
Both "as" and casting are appropriate in different circumstances.

However, "as" is definitely a C# keyword.

Nov 17 '05 #12
> How would you implement something like that:
if ((Customer cust = GetCust())!=null)
{
}
else if ((Supplier supp = GetSupp())!=null)
{
}


As a former and long-time C/C++ programmer, I always considered the
ability to perform variable assignments, and later variable
declarations inside conditions to be one of the nastier and uglier
aspects of the language... a holdover from a peculiar grad student
pride at being able to stuff as much as possible into one line of code.
I found the construct difficult to read and therefore hurtful to
maintenance.

Of course, it's all a matter of taste. I realize that the old C standby
of assigning and testing within the condition of a while loop was used
extensively, and I was the odd man out who refused to do it.
Nonetheless, I agree with the C# language designers that clever little
tricks like this one, while they do have some advantages such as
reducing variable scope, have the disadvantage of making the code
crypitc. In the end, I think, they weighed the advantages of this
construct against readability and chose readability. I know I would
have done the same. :)

Nov 17 '05 #13
Oops. Sorry bout that.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Wrong. "as" is not VB. It's C#.

It casts the given reference to the given type, returning null if the
cast fails. A direct cast throws an exception, which can be costly.
Both "as" and casting are appropriate in different circumstances.

However, "as" is definitely a C# keyword.

Nov 17 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
75
by: David MacQuigg | last post by:
Seems like we need a simple way to extend Python syntax that doesn't break existing syntax or clash with any other syntax in Python, is easy to type, easy to read, and is clearly distinct from the...
7
by: Jan van Veldhuizen | last post by:
The UPDATE table FROM syntax is not supported by Oracle. I am looking for a syntax that is understood by both Oracle and SqlServer. Example: Table1: id name city ...
6
by: George Styles | last post by:
Hi, I am trying to work out what a block of C++ does, but I am having trouble understanding the syntax. I know Delphi well, so am happy with objects etc, its just this syntax I dont understand. ...
14
by: aaron kempf | last post by:
I find that ADP does not support any Stored Procedures that use the 'CREATE PROC spHAPPY' syntax. CREATE PROC syntax is listed in books online. This syntax should be supported Here is a...
5
by: Rob | last post by:
To follow up on the "copy constructor clarification thread"... The assignment operator syntax shown previously: MyClass% operator=(const MyClass%); seems to have problems if you have member...
2
by: Marty | last post by:
Hi, if I want to create a templace class (like we can do in C++), what is the syntax to tell to the class's constructor the specific type I want to use? Thank you and have a nice day! Marty
0
by: jacqueharper | last post by:
I am having a problem with an Excel ListObject in my C# .NET application. I am trying to map an XML schema to a ListObject, and continue to get the error "The XPath is not valid because either the...
2
by: rockstar_ | last post by:
Hello all- I'm developing a Content Management software for my own site, and possibly package and deploy to other sites (for friends, family, etc.) The content management software is combined...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.