473,799 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Uninitialized boolean

Hello,
Is it possible if a boolean was initialized or not? For other types of
variable, I usually check if it is null. But this not possible for a
boolean.

Thank you
Julien
Nov 17 '05 #1
3 9584
First of all, if it's a local variable in a method, like

public void MyFunc()
{
bool unInitialized;

if (unInitialized) { ... }
}

this will not compile. The compiler will complain that it is
uninitialized.

That said, it is possible to create scenarios in which you can get at
uninitialized _properties_ and the compiler won't complain. In this
situation, every value type in the Framework has a default value to
which it is guaranteed to be initialized. For reference types (and
strings, which are also reference types) it is null. For numeric types,
it is 0. I believe that for booleans it is false, although I'm open to
being corrected on that.

Nov 17 '05 #2
Bruce is correct. This seems to be a common question in the newsgroups so
I'll try to clarify the rules about definite assignment. I'm pretty
confident these are correct. The formal rules are in the C# specification,
section 5.3

1. Local variables of any type must be initialized before they are first
used. A compiler error occurs if they are not.
public void foo ( )
{
int nVar;
if (nVar == 0) //Compiler error
}

2. Member variables of a class are default initialized when the instance is
created. For references it is null. For all other types it is the
equivalent of 0. Boolean is false. This is unless you use an initialization
expression in the declaration in which case it will have the expression's
value. Even then however I think it will still first be default initialized.

public class Test
{
int m_nVar; //Initialized to 0 when the instance is
created
int m_nVar2 = 4; //Initialized to 4 when the instance is created
}

3. Member variables in a struct are always default initialized. You must
assign all variables in a struct a value within any custom constructor you
create prior to invoking any property or method of the struct. The compiler
will generate an error if you don't.

public struct TestStruct
{
public TestStruct ( bool dummy )
{
if (m_nVar == 0) //Error, must initialize all vars first
}

int m_nVar; //Default initialized
int m_nVar2 = 4; //Error, struct vars can't be initialized
}

4. Static member variables are initialized prior to their first reference.
However if the owning type has a static constructor then the static members
are initialized when the static constructor is called.

public class TestStatic
{
static int m_nVar; //Initialized prior to referencing
m_nVar
}

public class TestStatic2
{
static TestStatic2 ( ) { }

static int m_nVar; //Initialized when static constructor
called
}

5. Out parameters are not default initialized. They must be assigned a
value prior to the function returning.

public void foo ( out string parm )
{
return; //Error, must initialize out parameter
}

There is one special case where you can actually access a variable before it
is initialized. It is a boundary case that you should really avoid anyway.
I don't remember the details exactly but it has something to do with
initializing member variables with other member variables before they are
truly initialized. In this rare case you'll see a member variable's value
before it is truly initialized. Even in this case however I think it is
going to be default initialized.

public class TestWeird
{
public static int m_nVar = m_nVar2 + 2; // Value is 2
public static int m_nVar2 = m_nVar + 2; // Value is 2
}

Hope this clarifies,
Michael Taylor, MCP, MCAD - 6/29/05

"Bruce Wood" wrote:
First of all, if it's a local variable in a method, like

public void MyFunc()
{
bool unInitialized;

if (unInitialized) { ... }
}

this will not compile. The compiler will complain that it is
uninitialized.

That said, it is possible to create scenarios in which you can get at
uninitialized _properties_ and the compiler won't complain. In this
situation, every value type in the Framework has a default value to
which it is guaranteed to be initialized. For reference types (and
strings, which are also reference types) it is null. For numeric types,
it is 0. I believe that for booleans it is false, although I'm open to
being corrected on that.

Nov 17 '05 #3
I should have said _class members_, not properties. :-)

Nov 17 '05 #4

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

Similar topics

1
14158
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of uninitialized value in concatenation (.) at register.pl line 38, <STDIN> line 10." The PERL code is as follows:
1
15537
by: rk | last post by:
Hi, I'm a beginner for perl/cgi programs and i tried to write a cgi script and when i ran it, i got the following error. But when i verified it from the book i typed exactly whatever it is there and i checked other examples too. I did't get any clue.Can someone please help me on this. #!/usr/bin/perl use warnings;
2
5621
by: Liang | last post by:
Hi, I use "defined $r_libs->{$name}" to check first if a key exists in a hash table. But Perl gives a warning WHENEVER the key exists: "Use of uninitialized value". Would u please help to check the script, and let me know the reason? Thanks in advance. Liang
13
20306
by: rswanster | last post by:
When I compile and run the following: #include <iostream> int main() { bool f; std::cout << f << std::endl; f = not(f); std::cout << f << std::endl; }
12
2585
by: jyu.james | last post by:
I'm trying to detect reads of uninitialized global variables (that are declared in one file, and used in another as an extern). I know that ANSI C initializes all global variables to 0, however, I do not want to rely on this for initialization. Instead, I want to explicity initialize all variables myself. I've looked at tools like Compuware BoundsChecker, which does an amazing job in detecting uninitialized variables, but doesn't...
21
2286
by: sanjaymeher | last post by:
Hi, Right now addDynamicMemory(char **ptr, int size) method can able to handle if input ptr is intitialized to NULL or something. But how to improve this method to handle uninitialized pointed even. Any Answer ?? Thanks, Sanjay
1
2135
by: lavu | last post by:
I have a struct that I am creating an object for but not initializing it. Later on under certain conditions it gets set to the values of another struct. However I have to check this struct for an valid instance and sometimes it is not a valid instance and gives an exception. Since I cannot use a null for a value type how would I check for the validity of the object. For example:
2
7016
by: pnsreee | last post by:
Hi All, I have the following code. The array @sub_data will contain integers or a string"NO". I have to validate if the array contain integer. If it contain "NO" then no need to validate. for ($i=1;$i<10;$i++) { Tie::CheckVariables->on_error(sub{print "ERROR!"}); tie my $data,'Tie::CheckVariables','integer';
6
4274
by: samthemist | last post by:
When perl comes out with errors like this: Use of uninitialized value in concatenation (.) or string at NQII.pl line 18, <STDIN> line 2. Argument "" isn't numeric in numeric ne (!=) at NQII.pl line 342, <STDIN> line 2. Argument "neopets.com/hi.phtml" isn't numeric in numeric ne (!=) at NQII.pl line 342, <STDIN> line 2. Argument "POST" isn't numeric in numeric eq (==) at NQII.pl line 343, <STDIN> line 2. Argument "POST" isn't numeric in...
0
9686
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
9540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10475
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
10250
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
10222
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,...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.