473,511 Members | 15,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Declaration in IF statement

I would assume that the following would declare the variable someClass
and it's scope would be confined to the if statement.

if((SomeClass someClass =
this.DoesASomeClassExist(identifier))!=null){
someClass.DoSomething();
}

Is there a better more compact way to write this that I cannot think
of?
Am I getting inappropriate compiler errors(in other words should this
compile)?

Thanks,
Harlan
Nov 15 '05 #1
2 3800
You can't declare a variable within and if statement.

And honestly, even if you could I'd advise against it. There's a fine line
between writing compact/readable code and IMHO, declaring variables in an if
statement crosses it.

For example:

string s = other = "1234";

As opposed to

s = "1234";

other = s;

Some people may miss the fact you're assigning "1234" to two different
variables in the above scenario. This just makes it harder for someone else
to read and understand your code.

"wandering1" <hd****@cesco.com> wrote in message
news:<68**************************@posting.google. com>...
I would assume that the following would declare the variable someClass and it's scope would be confined to the if statement. if((SomeClass someClass = this.DoesASomeClassExist(identifier))!=null){ someClass.DoSomething(); } Is there a better more compact way to write this that I cannot think of? Am I getting inappropriate compiler errors(in other words should this compile)? Thanks, Harlan

Nov 15 '05 #2
You cannot do that in C#. You need to declare that variable outside of the if statement

Tu-Thac

----- wandering1 wrote: ----

I would assume that the following would declare the variable someClas
and it's scope would be confined to the if statement

if((SomeClass someClass
this.DoesASomeClassExist(identifier))!=null)
someClass.DoSomething()
Is there a better more compact way to write this that I cannot thin
of
Am I getting inappropriate compiler errors(in other words should thi
compile)

Thanks
Harla

Nov 15 '05 #3

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

Similar topics

83
6423
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
7
2645
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
7
7696
by: seamoon | last post by:
Hi, I'm doing a simple compiler with C as a target language. My language uses the possibility to declare variables anywhere in a block with scope to the end of the block. As I remembered it this...
6
1912
by: jcrouse | last post by:
I have a project with a startup (Sub Main) module and two forms. Based on the logic in the Sub Main it calls one of the two forms. The two forms are almost identical. They both make calls into the...
6
1242
by: rJ | last post by:
hi, what does the following statement mean and what does it do? Dim obTest As Record.Class1 = CType(Class2.GetSessionInfo(class3.TestConstant), Record.Class1) GetSessionInfo() simply does...
9
6785
by: Denis Petronenko | last post by:
i can write something like this int foo() { return 100; } if( int x=foo() ) { .... }else{
7
1684
by: Jess | last post by:
Hello, In the appendix of Accelerated C++, there is a declaration statement const char * const * const * cp; The author says the specifiers are "const char" and the declarator is "* const *...
14
13026
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
15
14889
by: vaib | last post by:
hi to all.i'd like to know the actual difference between variable declaration and definition.it would be very helpful if anyone out there wud help me out with this thing.i'm writing here after here...
1
11849
NeoPa
by: NeoPa | last post by:
Problem Description : In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private;...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.