No syntax error?

1

Error: (only assignment call increment decrement and new object expressions can be used as a statement)

I do not understand why you are giving this error, since the variable is of type int ...

int deQuantidade = 0, ateQuantidade = 0;
string tipo = "";

deQuantidade = Convert.ToInt32(txtDe.Text);
ateQuantidade = Convert.ToInt32(txtAte.Text);
tipo = Convert.ToString(cmbTipo.SelectedIndex);

for (deQuantidade ; deQUantidade < ateQuantidade; deQuantidade++)
{

Error presented:

  

only assignment call increment decrement and new object expressions can be used as a statement

    
asked by anonymous 26.01.2018 / 02:34

2 answers

3
  

First of all ... Documentation . Thanks @Krismorte by

Understanding what happened

The error is in the use of for . The for consists of 3 (optional) fields: initial assignment, comparison, increment. In the first field, you did not perform any assignment operation, just put the variable name.

This applies to most languages that share the syntax of the traditional for of C. Some languages have syntax for for-each (another loop expression). Other languages allow the first field to also be declared variable, but C-ANSI89 does not.

Okay, but how to fix?

  

Thanks to @Isac for realizing that the value started was from reading the text, not 0

You could start the value int deQuantidade = Convert.ToInt32(txtDe.Text) in the first field of for :

for (int deQuantidade = Convert.ToInt32(txtDe.Text); deQuantidade < ateQuantidade; deQuantidade++) {
  // ...

This field, by the way, can be used for multiple initializations (separated by commas , ):

for (int deQuantidade = Convert.ToInt32(txtDe.Text), int ateQuantidade = Convert.ToInt32(txtAte.Text); deQuantidade < ateQuantidade; deQuantidade++) {
  // ...

Or, by maintaining the statement structure prior to the loop (but not initializing the variable):

int deQuantidade;
// ...

for (deQuantidade = Convert.ToInt32(txtDe.Text); deQuantidade < ateQuantidade; deQuantidade++) {
  // ...

Or, keeping the boot structure before the loop:

int deQuantidade = 0;
// ...
deQuantidade = Convert.ToInt32(txtDe.Text);
// ...

for (; deQuantidade < ateQuantidade; deQuantidade++) {
  // ...
    
26.01.2018 / 03:18
-2

Attempts to make the for structure in this way, as the quantity variable inside for, becomes a local variable

*

for (deQuantidade = 0; deQuantidade < ateQuantidade; deQuantidade++){
}

*

    
26.01.2018 / 03:03