Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

cannot be declared in scope (among other errors)

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

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!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading