473,624 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unassigned Local Variable Error

In the following (pseudo)code, why is it that C# returns an
"unassigned local variable" error for nVar? It seems I have to
declare the variable outside foo() for the error to dissapear.

void foo()
{
int nVar = 0;

SqlDataReader rdr = objCmd.ExecuteR eader();

while (rdr.Read())
{
nVar = rdr[0];
}

int nVar2 = nVar;
}

Thank you.
Nov 17 '05 #1
3 6469
Hi John,
If you remove everything in the function body except for the line
int nVar = 0
then the compiler throws unassigned local variable warning. This is because
the compiler treats the this line as declaration and not initialization.
There might be some other error occuring in the line
nVar = rdr[0]
where unboxing is required from object to int
best,
Subin Kushle
GAPS

"John Smith" <3r*********@sn eakemail.com> wrote in message
news:hc******** *************** *********@4ax.c om...
In the following (pseudo)code, why is it that C# returns an
"unassigned local variable" error for nVar? It seems I have to
declare the variable outside foo() for the error to dissapear.

void foo()
{
int nVar = 0;

SqlDataReader rdr = objCmd.ExecuteR eader();

while (rdr.Read())
{
nVar = rdr[0];
}

int nVar2 = nVar;
}

Thank you.

Nov 17 '05 #2
> In the following (pseudo)code, why is it that C# returns an
"unassigned local variable" error for nVar?


I might be missing something since you said this is pseudo-code, but I was
able to compile it just fine (with a few obvious changes to make it
non-pseudo-code).
Nov 17 '05 #3
John Smith <3r*********@sn eakemail.com> wrote:
In the following (pseudo)code, why is it that C# returns an
"unassigned local variable" error for nVar? It seems I have to
declare the variable outside foo() for the error to dissapear.

void foo()
{
int nVar = 0;

SqlDataReader rdr = objCmd.ExecuteR eader();

while (rdr.Read())
{
nVar = rdr[0];
}

int nVar2 = nVar;
}


As it is, that's fine. If you change the first line of the body to:

int nVar;

then you would get a problem, because there's no guarantee that it
would be assigned before nVar2 tries to use it (rdr.Read() could return
false immediately).

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

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

Similar topics

5
1337
by: Mike P | last post by:
I am instantiating a class in a switch statement as there are a number of different overloads depending upon the data entered by the user. The problem I have is that after instantiating my class, when I try to call methods in the class later on in my code using the same object I cannot, I get the error 'use of unassigned local variable'. How do I get around this as I need some sort of switch/if statement to instantiate my class?
12
1983
by: Rene | last post by:
I understand that if I don't assign a *local* variable before I use it, the compiler will generate a "Use of unassigned local variable" error. What I don't get is why doesn't the compiler just implicitly assign a default value to my unassigned variable? At this point you are getting ready to reply to me and tell me that this is just a way for the compiler to protect me from introducing a possible bug by preventing me from forgetting to...
29
2233
by: Joseph Geretz | last post by:
Use of unassigned local variable 'fs' Please see where I've indicated where the compiler is flagging this error in the method below. fs is initialized in the first line in the try block, so why is it flagged as unassigned in the finally block? Thanks for your help! - Joe Geretz -
2
3784
by: Doug | last post by:
I am trying to work with an object and create it like so: EXTRA.Sessions oSessions = new EXTRA.Sessions(); This doesn't work because I get this error: 'Cannot create an instance of the abstract class or interface 'EXTRA.Sessions'.' So I try to do this:
9
6182
by: tshad | last post by:
I am getting an error: Use of unassigned local variable 'postDateRow' But it is assigned. Here is the code: int payDateRow; int postDateRow;
3
529
by: Antonio | last post by:
Can somebody tell me what's wrong with this code? When I try to debug, I get "Use of unassigned local variable 'ip2se'. string ip2se; if(e.Item.Cells.Text == e.Item.Cells.Text) ip2se = e.Item.Cells.Text.ToString(); Thanks,
22
8408
by: Laura T. | last post by:
In the following example, c# 2.0 compiler says that a3 and ret are used before assigned. as far as I can see, definite assignment is made. If I add finally { ret = true; a3 = "b3";
8
11141
by: Dom | last post by:
This is a little tricky, but I think the error I'm getting isn't valid. The compiler just doesn't know my logic. MyObject o; switch (IntVariable) { case 1: o = new MyObject() break;
3
3854
by: Hegel | last post by:
Some body please help me. I finds this code really correct. However I am a beginner The following code yields Use of unassigned local variable 'fout' error in my pgm please help using System; using System.IO; using System.Collections.Generic;
0
8677
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...
1
8335
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
8474
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5563
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
4079
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
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
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.