473,382 Members | 1,775 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,382 software developers and data experts.

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.dataTable[0].GetUpperBound(0);ktr++)
{
mDt.Columns.Add(new DataColumn(rtDataBean.dataTable[0][ktr]));
if (rtDataBean.dataTable[0][ktr] == "Pay Date") payDateRow = ktr;
if (rtDataBean.dataTable[0][ktr] == "Post Date") postDateRow = ktr; <-
assigned here
};
for(ktr=0;ktr<= rtDataBean.dataTable.GetUpperBound(0);ktr++)
{
DataRow mDr = mDt.NewRow();
for (ktr1=0;ktr1<=rtDataBean.dataTable[0].GetUpperBound(0);ktr1++)
{
mDr[ktr1] = rtDataBean.dataTable[ktr][ktr1];
};
mDr[ktr1] = rtDataBean.dataTable[ktr][ktr1] + " / " +
rtDataBean.dataTable[ktr][postDateRow]; <- error here
mDt.Rows.Add(mDr);
};

Why is this an error?

Thanks,

Tom
Feb 28 '06 #1
9 6164
"tshad" <ts**********@ftsolutions.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**********@ftsolutions.com> wrote in message
news:eU**************@TK2MSFTNGP11.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.dataTable[0].GetUpperBound(0);ktr++)
{
mDt.Columns.Add(new DataColumn(rtDataBean.dataTable[0][ktr]));
if (rtDataBean.dataTable[0][ktr] == "Pay Date") payDateRow = ktr;
if (rtDataBean.dataTable[0][ktr] == "Post Date") postDateRow = ktr;
<- assigned here
};
for(ktr=0;ktr<= rtDataBean.dataTable.GetUpperBound(0);ktr++)
{
DataRow mDr = mDt.NewRow();
for (ktr1=0;ktr1<=rtDataBean.dataTable[0].GetUpperBound(0);ktr1++)
{
mDr[ktr1] = rtDataBean.dataTable[ktr][ktr1];
};
mDr[ktr1] = rtDataBean.dataTable[ktr][ktr1] + " / " +
rtDataBean.dataTable[ktr][postDateRow]; <- error here
mDt.Rows.Add(mDr);
};

Why is this an error?

Thanks,

Tom

Feb 28 '06 #3

"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:OW**************@TK2MSFTNGP09.phx.gbl...
"tshad" <ts**********@ftsolutions.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**********@ftsolutions.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.com>
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.com> wrote in message
news:MP************************@msnews.microsoft.c om...
tshad <ts**********@ftsolutions.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.com>
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**********@ftsolutions.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
tshad <ts**********@ftsolutions.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.com>
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.dataTable[0].GetUpperBound(0);ktr++)
{
mDt.Columns.Add(new DataColumn(rtDataBean.dataTable[0][ktr]));
if (rtDataBean.dataTable[0][ktr] == "Pay Date") payDateRow = ktr;
if (rtDataBean.dataTable[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.machin 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.com>
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.machin AT dot.state.fl.us> wrote
in message news:eT**************@TK2MSFTNGP09.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.dataTable[0].GetUpperBound(0);ktr++)
{
mDt.Columns.Add(new DataColumn(rtDataBean.dataTable[0][ktr]));
if (rtDataBean.dataTable[0][ktr] == "Pay Date") payDateRow = ktr;
if (rtDataBean.dataTable[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
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...
2
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...
5
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,...
12
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...
3
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...
29
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...
22
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
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;
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.