Use of unassigned local variable 'Cst' - Error message

  • Thread starter Thread starter Ed Hochberg
  • Start date Start date
E

Ed Hochberg

Hi All,

New to C# and I don't understand why I am getting this error.

I declare Cst as a string at the top of the procedure

String Cst, Prv, Geo, Grp, Itm;

Later on I have this "if" statement (where lvCust is a Listview):


if (lvCust.SelectedItems.Count > 0)
{
Cst = lvCust.SelectedItems[0].SubItems[0].Text;
}

Then I am trying to pass Cst to a Stored Procedure but I have this error
from the compilier?? (The funny thing is that the other varaiable don't have
any error messages)

"Use of unassigned local variable 'Cst'"


Thanks
 
Hi All,

New to C# and I don't understand why I am getting this error.

I declare Cst as a string at the top of the procedure

String Cst, Prv, Geo, Grp, Itm;

Later on I have this "if" statement (where lvCust is a Listview):

if (lvCust.SelectedItems.Count > 0)
{
Cst = lvCust.SelectedItems[0].SubItems[0].Text;
}

Then I am trying to pass Cst to a Stored Procedure but I have this error
from the compilier?? (The funny thing is that the other varaiable don't
have
any error messages)

"Use of unassigned local variable 'Cst'"

You didn't post any code that uses "Cst". How are we supposed to explain
why the error occurs?

The general answer is that C# has rules about what counts as "definitely
assigned", and you must not have code that "definitely assigns" the
variable "Cst". Certainly if the use of the variable "Cst" occurs outside
that "if" statement you did show, then you should get the error you're
getting.

If you need a better explanation than that, you need to post a better code
example than that. There's lots of good advice out there regarding how to
post a good question, but one of the most important rules is that if you
have a question about some error that is occurring (compiler or run-time),
then you need to post at _least_ the code where the error actually happens
(and usually somewhat more than that, but that's secondary to the point
here :) ).

Pete
 
Thanks for the reply Peter.

I am not sure why you think I didn't post any code that uses "Cst". I did,
the "if" statement uses "Cst".



Peter Duniho said:
Hi All,

New to C# and I don't understand why I am getting this error.

I declare Cst as a string at the top of the procedure

String Cst, Prv, Geo, Grp, Itm;

Later on I have this "if" statement (where lvCust is a Listview):

if (lvCust.SelectedItems.Count > 0)
{
Cst = lvCust.SelectedItems[0].SubItems[0].Text;
}

Then I am trying to pass Cst to a Stored Procedure but I have this error
from the compilier?? (The funny thing is that the other varaiable don't
have
any error messages)

"Use of unassigned local variable 'Cst'"

You didn't post any code that uses "Cst". How are we supposed to explain
why the error occurs?

The general answer is that C# has rules about what counts as "definitely
assigned", and you must not have code that "definitely assigns" the
variable "Cst". Certainly if the use of the variable "Cst" occurs outside
that "if" statement you did show, then you should get the error you're
getting.

If you need a better explanation than that, you need to post a better code
example than that. There's lots of good advice out there regarding how to
post a good question, but one of the most important rules is that if you
have a question about some error that is occurring (compiler or run-time),
then you need to post at _least_ the code where the error actually happens
(and usually somewhat more than that, but that's secondary to the point
here :) ).

Pete
 
Thanks for the reply Peter.

I am not sure why you think I didn't post any code that uses "Cst". I
did,
the "if" statement uses "Cst".

No, it doesn't. Not in the way that the error message you're asking about
means. It _assigns_ "Cst", but it doesn't read it.

You won't get that error on an assignment. Only on a read. You didn't
post any code that reads the variable.

We can tell from the error message what the most likely cause of the error
is (based simply on the knowledge of what that error means), but in truth
there's not enough code there for anyone to know for sure.

Pete
 
String Cst, Prv, Geo, Grp, Itm;

Later on I have this "if" statement (where lvCust is a Listview):


if (lvCust.SelectedItems.Count > 0)
{
Cst = lvCust.SelectedItems[0].SubItems[0].Text;
}

Then I am trying to pass Cst to a Stored Procedure but I have this error
from the compilier?? (The funny thing is that the other varaiable don't
have
any error messages)

"Use of unassigned local variable 'Cst'"

Because the only time (given the code you've shown) you actually assign a
value to Cst is inside an if statement, the compiler has to assume that it
is POSSIBLE that Cst may not be assigned by the time you get to the code
(which you didn't post but hinted at) that passes Cst to a stored procedure.
This is why the compiler is complaining, because Cst MIGHT not be assigned.

The easiest way around this is to simply initialize the variable to null on
declaration:

string Cst = null;
 
Peter is right. The Cst variable has a potential to never being assigned,
thus it generates a compile error if it is later referenced in the code.

Darek
 
Back
Top