Unassigned local variable

T

tshad

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
 
J

Joanna Carter [TeamB]

"tshad" <[email protected]> a écrit dans le message de (e-mail address removed)...

|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
 
T

tshad

Joanna Carter said:
"tshad" <[email protected]> a écrit dans le message de (e-mail address removed)...

|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
 
S

Stoitcho Goutsev \(100\)

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.
 
J

Jon Skeet [C# MVP]

tshad said:
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.
 
T

tshad

Jon Skeet said:
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
 
J

Jon Skeet [C# MVP]

tshad said:
So they would be something like this?:

int static test;

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

Ignacio Machin \( .NET/ C# MVP \)

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:");
 
J

Jon Skeet [C# MVP]

<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
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.
 
T

tshad

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,



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:");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top