Home > Software engineering >  cannot be declared in scope (among other errors)
cannot be declared in scope (among other errors)

Time:11-28

hello im new to programing with c# and i wanted to re-create an old proyect i made in scratch but with c#

the scratch code

but i have several problems with the code i wrote.

int qny = 4;
int ADD = 1;
int B = 1;
string C = " ";
for (int i = 0; i < qny; i  ) ;
{
    int R = qny   ADD;
    for (int i = 0; i < R; i  ) ;
    {
        string C = string.Join(C, B);
    }
    int qny = qny - 1;
    int B = B   1;
}
Console.WriteLine(C);

the main problem is that i cant use the variables "qny","B" and "C" inside the "for". the only one that doesnt show an error message is the "ADD" variable

its suposed to write numbers like this

qny result
2 112
3 111223
4 1111222334

errors:

Error   CS0841  Cannot use local variable 'qny' before it is declared. A variable must be declared before it is used.

Error   CS0136  A local variable named 'C' cannot be declared in this scope because it would give a different meaning to 'C', which is already used in a 'parent or current/child' scope to denote something else

Error   CS0165  Use of unassigned local variable 'C'


Error   CS0841  Cannot use local variable 'B' before it is declared.


Error   CS0136  A local variable named 'qny' cannot be declared in this scope because it would give a different meaning to 'qny', which is already used in a 'parent or current/child' scope to denote something else


Error   CS0136  A local variable named 'B' cannot be declared in this scope because it would give a different meaning to 'B', which is already used in a 'parent or current/child' scope to denote something else

CodePudding user response:

Your for loops end with a semicolon, which ends the loop. The following code blocks are not treated as part of the loop body because of that. Additionally as aybe mentioned you use i for both loops which will update the same variable.

EDIT: As mentioned below you also shouldn't redeclare the variable C as it won't update the variable C at the top, same reason as to why you don't want to use i for both loops.

EDIT: Code below still has redeclared variables, check author's answer for updated code.

I suggest changing it to this:

int qny = 4;
int ADD = 1;
int B = 1;
string C = " ";
for (int i = 0; i < qny; i  )
{
    int R = qny   ADD;
    for (int j = 0; j < R; j  )
    {
        C = string.Join(C, B);
    }
    int qny = qny - 1;
    int B = B   1;
}
Console.WriteLine(C);

To avoid such errors in the future I would recommend checking out some tutorials on the basics of C#. I wish you good luck on your coding journey!

CodePudding user response:

thanks to the few people that helped me. as someone suggested ill chech out some tutorials. the final code looks like this:

int qny = 4;
int ADD = 1;
int B = 1;
string C = " ";
for (int i = 0; i < qny; i  )
{
    int R = qny   ADD;
    for (int j = 0; j < R; j  )
    {
        C = string.Join(C, B);
    }
    qny = qny - 1;
    B = B   1;
}
Console.WriteLine(C);
  • Related