Variable assigned but never used error

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

Tom Spink

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?
 
R

Rudy Velthuis

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

Loren Pechtel

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

Eric Kiernan

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.
 

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