473,669 Members | 2,458 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.DoesASomeC lassExist(ident ifier))!=null){
someClass.DoSom ething();
}

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 3806
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.c om> wrote in message
news:<68******* *************** ****@posting.go ogle.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.DoesASomeC lassExist(ident ifier))!=null){ someClass.DoSom ething(); } 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.DoesASomeC lassExist(ident ifier))!=null)
someClass.DoSom ething()
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
6481
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 C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
7
2662
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 before the variable is used? I'm trying to get an idea of whether the .NET compilers for VB.NET and C# will move all variable declaration to the beginning of a method or whether they will allocate the memory as it is needed by the method to...
7
7712
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 would be easily translated to C, but it seems variable declaration is only possible in the beginning of a block in C. Any suggestions how to get around this?
6
1920
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 same INI file using the following code: Private Declare Auto Function GetPrivateProfileString Lib "kernel32" _ (ByVal lpAppName As String, _ ByVal lpKeyName As String, _ ByVal lpDefault As String, _ ByVal lpReturnedString As...
6
1248
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 this Return Me.Session(name) TestConstant is a contstant containing "test.tester"
9
6796
by: Denis Petronenko | last post by:
i can write something like this int foo() { return 100; } if( int x=foo() ) { .... }else{
7
1697
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 * const * cp". What does this declaration mean? Thanks,
14
13050
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
15
14903
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 after a long time since people here also helped me out with my lexer. thanking in anticipation.
1
11856
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; Public; Global; etc) will cause an error, either at compile time or when attempting to execute the procedure it's referred to from within. With this unset, any unregognised references will be treated as a previously unknown and unset variable of type...
0
8465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8895
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8809
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8588
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6210
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5682
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4386
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.