473,324 Members | 2,567 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,324 software developers and data experts.

Instance vs Local Initialization

Consider

{
bool var;
if(var)
Console.WriteLine("var is true");
}

results in the compilation error

Use of unassigned local variable 'var'

If you make var an instance variable you don't get a compile error as
the variable is effectively 'initialized' to all zeros. Why can't local
variables behave in this manner?

It irritates me that I have to type

bool var=false;

Regards
--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
4 1235
vooose <no****@microsoft.com> wrote:
Consider

{
bool var;
if(var)
Console.WriteLine("var is true");
}

results in the compilation error

Use of unassigned local variable 'var'

If you make var an instance variable you don't get a compile error as
the variable is effectively 'initialized' to all zeros. Why can't local
variables behave in this manner?
They could - but it's better that they don't. In a way, the *downside*
is that you *can't* do this with instance variables.

The fact that you can't accidentally use a local variable before it's
been initialised is great IMO. I've seen plenty of bugs in C code which
does just that.
It irritates me that I have to type

bool var=false;


If you want to just not execute some code, don't include it in the
first place, or write:

if (false)

It's relatively rare that I find I have to initialise a local variable
with a value which isn't actually useful.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2
Thanks for your reply John. How about this case...

{
bool found=false;
foreach(string temp in arr)
{
if(temp == "hullo")
found=true;
}

if(!found)
//do not found stuff
}

I don't want to initialize found in this case (granted this isnt a big
deal but still...)
--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Sorry Jon for name typo in previous. One more thing...I didn't
understand what you meant by

If you want to just not execute some code, don't include it in the
first place, or write:

if (false)

with respect to my initial question. I would prefer a compilation
error for objects but not for primitives.

Regards
--
Wal
http://www.vooose.com

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4
vooose <no****@microsoft.com> wrote:
Thanks for your reply John. How about this case...

{
bool found=false;
foreach(string temp in arr)
{
if(temp == "hullo")
found=true;
}

if(!found)
//do not found stuff
}

I don't want to initialize found in this case (granted this isnt a big
deal but still...)


Why not? It makes it more explicit what you want - which is actually to
be initialised to false.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

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

Similar topics

1
by: ank | last post by:
Hi, all. I've come to think of the idea of automatic initialization/deinitialization of non-local reference count pointer. I've made an assumption that the user of the pointer only read...
2
by: ank | last post by:
Hi, all. I've come to think of the idea of automatic initialization/deinitialization of non-local reference count pointer. I've made an assumption that the user of the pointer only read...
2
by: TEK | last post by:
Hello We're having a issue with a part of our app that is very strange, and I'm unable to see that it should be possible. We have: A collection (ActivityCollection) that is inherited from a...
5
by: Andy B | last post by:
I am trying to figure out how to make an object instance available for all methods of a class. I tried to do something like this: public class test { TheObject Instance = new TheObject();...
0
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
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...
1
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.