Variable assigned but never used error

  • Thread starter Thread starter Eric Kiernan
  • Start date Start date
E

Eric Kiernan

I'm trimmed out some of the extraneous code, but below is the code
snippet. It retains all code structures. Compiler claims that
"variable emptySpending is assigned but never used" But it clearly is,
and the variables declared along with it don't generate a warning.

private bool saveComponentsToGridRow() {
bool emptySpending = true;
bool emptySaving = true;
bool emptyBudget = true;
int i = 0;

if ( tbAccount.Text == "") {
}

for (i = 0; i < 12; i++) {
spendingString = spending.Text;
if (spendingString == "") {
spendingValue = 0;
}
else {
emptySpending = false; // IT IS USED HERE. }
}
 
Hi Eric,

Eric said:
I'm trimmed out some of the extraneous code, but below is the code
snippet. It retains all code structures. Compiler claims that
"variable emptySpending is assigned but never used" But it clearly is,
and the variables declared along with it don't generate a warning.

private bool saveComponentsToGridRow() {
bool emptySpending = true;
bool emptySaving = true;
bool emptyBudget = true;
int i = 0;

if ( tbAccount.Text == "") {
}

for (i = 0; i < 12; i++) {
spendingString = spending.Text;
if (spendingString == "") {
spendingValue = 0;
}
else {
emptySpending = false; // IT IS USED HERE. }
}


The compiler is telling you the variable is assigned, but never read.
Which is exactly the case... you assign a value to emptySpending - but
you never read that value - so what's the point?
 
Eric said:
I'm trimmed out some of the extraneous code, but below is the code
snippet. It retains all code structures. Compiler claims that
"variable emptySpending is assigned but never used" But it clearly
is, and the variables declared along with it don't generate a warning.

private bool saveComponentsToGridRow() {
bool emptySpending = true;
bool emptySaving = true;
bool emptyBudget = true;
int i = 0;

if ( tbAccount.Text == "") {
}

for (i = 0; i < 12; i++) {
spendingString = spending.Text;
if (spendingString == "") {
spendingValue = 0;
}
else {
emptySpending = false; // IT IS USED HERE. }
}


No, it is not used there, it is only written to there. You are not
using the value anywhere, and assigning values to a variable without
ever using that value is pretty useless. The compiler is telling you so.

Ok, since we, the readers of your post, only see a snippet of your
code, you may be using the value elsewhere, but that is not apparent in
what you posted here. From what we can see, the compiler is right.
 
I'm trimmed out some of the extraneous code, but below is the code
snippet. It retains all code structures. Compiler claims that
"variable emptySpending is assigned but never used" But it clearly is,
and the variables declared along with it don't generate a warning.

private bool saveComponentsToGridRow() {
bool emptySpending = true;
bool emptySaving = true;
bool emptyBudget = true;
int i = 0;

if ( tbAccount.Text == "") {
}

for (i = 0; i < 12; i++) {
spendingString = spending.Text;
if (spendingString == "") {
spendingValue = 0;
}
else {
emptySpending = false; // IT IS USED HERE. }
}


That's assignment, not use.
 
I understand now. thanks

Loren said:
I'm trimmed out some of the extraneous code, but below is the code
snippet. It retains all code structures. Compiler claims that
"variable emptySpending is assigned but never used" But it clearly is,
and the variables declared along with it don't generate a warning.

private bool saveComponentsToGridRow() {
bool emptySpending = true;
bool emptySaving = true;
bool emptyBudget = true;
int i = 0;

if ( tbAccount.Text == "") {
}

for (i = 0; i < 12; i++) {
spendingString = spending.Text;
if (spendingString == "") {
spendingValue = 0;
}
else {
emptySpending = false; // IT IS USED HERE. }
}


That's assignment, not use.
 
Back
Top