473,626 Members | 3,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unassigned local variable

I am getting an error:

Use of unassigned local variable 'postDateRow'

But it is assigned.

Here is the code:

int payDateRow;
int postDateRow;

for(ktr=0;ktr<= rtDataBean.data Table[0].GetUpperBound( 0);ktr++)
{
mDt.Columns.Add (new DataColumn(rtDa taBean.dataTabl e[0][ktr]));
if (rtDataBean.dat aTable[0][ktr] == "Pay Date") payDateRow = ktr;
if (rtDataBean.dat aTable[0][ktr] == "Post Date") postDateRow = ktr; <-
assigned here
};
for(ktr=0;ktr<= rtDataBean.data Table.GetUpperB ound(0);ktr++)
{
DataRow mDr = mDt.NewRow();
for (ktr1=0;ktr1<=r tDataBean.dataT able[0].GetUpperBound( 0);ktr1++)
{
mDr[ktr1] = rtDataBean.data Table[ktr][ktr1];
};
mDr[ktr1] = rtDataBean.data Table[ktr][ktr1] + " / " +
rtDataBean.data Table[ktr][postDateRow]; <- error here
mDt.Rows.Add(mD r);
};

Why is this an error?

Thanks,

Tom
Feb 28 '06 #1
9 6182
"tshad" <ts**********@f tsolutions.com> a écrit dans le message de news:
eU************* *@TK2MSFTNGP11. phx.gbl...

|I am getting an error:
|
| Use of unassigned local variable 'postDateRow'
|
| But it is assigned.

No it isn't, it is only assigned *if* a condition is true.

If assignment cannot be guaranteed, you need to initialise the variable to
some default value.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Feb 28 '06 #2
tshad,

At runtime there is no guarntees that the loop will be executed at least
once, thus no guarantee that the variables will be initialized. To make the
compiler happy give them some intialie value e.g *null* upon declaration.
--
HTH
Stoitcho Goutsev (100)

"tshad" <ts**********@f tsolutions.com> wrote in message
news:eU******** ******@TK2MSFTN GP11.phx.gbl...
I am getting an error:

Use of unassigned local variable 'postDateRow'

But it is assigned.

Here is the code:

int payDateRow;
int postDateRow;

for(ktr=0;ktr<= rtDataBean.data Table[0].GetUpperBound( 0);ktr++)
{
mDt.Columns.Add (new DataColumn(rtDa taBean.dataTabl e[0][ktr]));
if (rtDataBean.dat aTable[0][ktr] == "Pay Date") payDateRow = ktr;
if (rtDataBean.dat aTable[0][ktr] == "Post Date") postDateRow = ktr;
<- assigned here
};
for(ktr=0;ktr<= rtDataBean.data Table.GetUpperB ound(0);ktr++)
{
DataRow mDr = mDt.NewRow();
for (ktr1=0;ktr1<=r tDataBean.dataT able[0].GetUpperBound( 0);ktr1++)
{
mDr[ktr1] = rtDataBean.data Table[ktr][ktr1];
};
mDr[ktr1] = rtDataBean.data Table[ktr][ktr1] + " / " +
rtDataBean.data Table[ktr][postDateRow]; <- error here
mDt.Rows.Add(mD r);
};

Why is this an error?

Thanks,

Tom

Feb 28 '06 #3

"Joanna Carter [TeamB]" <jo****@not.for .spam> wrote in message
news:OW******** ******@TK2MSFTN GP09.phx.gbl...
"tshad" <ts**********@f tsolutions.com> a écrit dans le message de news:
eU************* *@TK2MSFTNGP11. phx.gbl...

|I am getting an error:
|
| Use of unassigned local variable 'postDateRow'
|
| But it is assigned.

No it isn't, it is only assigned *if* a condition is true.

If assignment cannot be guaranteed, you need to initialise the variable to
some default value.
I had thought that scalars were set by default (int=0,char='' etc).
Apparently this is not the case.

Then I assume

int postDateRow=0;

will work?

Thanks,

Tom
Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Feb 28 '06 #4
tshad <ts**********@f tsolutions.com> wrote:
If assignment cannot be guaranteed, you need to initialise the variable to
some default value.


I had thought that scalars were set by default (int=0,char='' etc).
Apparently this is not the case.


Instance variables and static variables have default values (the
default value for a char is '\0' by the way - '' isn't a valid
character literal) but local variables don't.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 28 '06 #5
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
tshad <ts**********@f tsolutions.com> wrote:
> If assignment cannot be guaranteed, you need to initialise the variable
> to
> some default value.
I had thought that scalars were set by default (int=0,char='' etc).
Apparently this is not the case.


Instance variables and static variables have default values (the
default value for a char is '\0' by the way - '' isn't a valid
character literal) but local variables don't.


So they would be something like this?:

int static test;

Thanks,

Tom
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 1 '06 #6
tshad <ts**********@f tsolutions.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
tshad <ts**********@f tsolutions.com> wrote:
> If assignment cannot be guaranteed, you need to initialise the variable
> to
> some default value.

I had thought that scalars were set by default (int=0,char='' etc).
Apparently this is not the case.f


Instance variables and static variables have default values (the
default value for a char is '\0' by the way - '' isn't a valid
character literal) but local variables don't.


So they would be something like this?:

int static test;


Well,
static int test;
instead, but yes. (Not inside a method declaration though.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 1 '06 #7
Hi,


So they would be something like this?:

int static test;


In addition to Jon comment you should not do this just to get it
initialized, a static member is shared to all the isntances of the class
therefore you need to keep special attention when using threads, it could
get you in trouble down the road if not used right
IMO you have to change your code, are you 100% sure that both values will
ALWAYS be present in the datatable?
What to do if this is not the case?

you could do somethign like:

int payDateRow = -1;
int postDateRow = -1;

for(ktr=0;ktr<= rtDataBean.data Table[0].GetUpperBound( 0);ktr++)
{
mDt.Columns.Add (new DataColumn(rtDa taBean.dataTabl e[0][ktr]));
if (rtDataBean.dat aTable[0][ktr] == "Pay Date") payDateRow = ktr;
if (rtDataBean.dat aTable[0][ktr] == "Post Date") postDateRow = ktr;

};

if ( payDateRow <0 )
thrw new Exception( " Pay date undefined:");
if ( postDateRow <0 )
thrw new Exception( " Post date undefined:");

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Mar 1 '06 #8
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.mach in AT
dot.state.fl.us >> wrote:
So they would be something like this?:

int static test;


In addition to Jon comment you should not do this just to get it
initialized, a static member is shared to all the isntances of the class


Sorry to pick on you Ignacio - I know you know what's really going on
here, but I find that people get confused by the idea that a static
member is "shared to all the instances of the class". It's not that
it's shared by all the instances - it's that it's not specific to *any*
instance. It effectively belongs to the type, so it's shared whether
there are any instances or not.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 1 '06 #9
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
in message news:eT******** ******@TK2MSFTN GP09.phx.gbl...
Hi,


So they would be something like this?:

int static test;

In addition to Jon comment you should not do this just to get it
initialized, a static member is shared to all the isntances of the class
therefore you need to keep special attention when using threads, it could
get you in trouble down the road if not used right
IMO you have to change your code, are you 100% sure that both values will
ALWAYS be present in the datatable?
What to do if this is not the case?


Yes, they will always be there.

you could do somethign like:

int payDateRow = -1;
int postDateRow = -1;
I ended up doing this, but set it as 0.

Thanks,

Tom
for(ktr=0;ktr<= rtDataBean.data Table[0].GetUpperBound( 0);ktr++)
{
mDt.Columns.Add (new DataColumn(rtDa taBean.dataTabl e[0][ktr]));
if (rtDataBean.dat aTable[0][ktr] == "Pay Date") payDateRow = ktr;
if (rtDataBean.dat aTable[0][ktr] == "Post Date") postDateRow = ktr;

};

if ( payDateRow <0 )
thrw new Exception( " Pay date undefined:");
if ( postDateRow <0 )
thrw new Exception( " Post date undefined:");

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Mar 1 '06 #10

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

Similar topics

3
3951
by: Mike P | last post by:
I keep getting the error 'Use of unassigned local variable' in my code, which I have used before and it works fine : SqlTransaction Trans1, Trans2; SqlConnection objConnectionDeactivateInvisilinkLNX, objConnectionDeactivateInvisilinkSQLSRVXwireless; SqlCommand objCommandDeactivateInvisilinkLNX, objCommandDeactivateInvisilinkSQLSRVXwireless;
2
1511
by: Eric Sabine | last post by:
I just need to understand this bit. In some code, I had the following give me an error at build time XmlDocument xmlDoc; xmlDoc.LoadXml(xmlString); The error was "unassigned use of local variable" on the second line at xmlDoc I ammended the first line to include " = null" and it compiled fine. Could
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...
3
6471
by: John Smith | last post by:
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.ExecuteReader();
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 -
22
8409
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;
0
8266
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
8199
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,...
1
8365
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
8505
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
7196
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...
1
6125
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
4092
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...
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
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.