0

This is my c# code and i am getting this error: The name 'viesti_s' does not exist in the current context

here's the code:

       string viesti_i = Console.ReadLine();
       Console.WriteLine("give number ");
        int avain = int.Parse(Console.ReadLine());

        for (int i = 0; i < viesti_i.Length; i += avain)
        {
            string viesti_s = viesti_i.Insert(avain, "b");
        }

        Console.WriteLine(viesti_s);
        Console.ReadKey();
1
  • because you have no viesti_s variable declared, you have viesti_i variable Commented Jan 21, 2017 at 17:23

1 Answer 1

1
Console.WriteLine("give number ");
int avain = int.Parse(Console.ReadLine());

string viesti_s = "HELLO !";

for (int i = 0; i < viesti_i.Length; i += avain)
{
   viesti_s = viesti_i.Insert(avain, "b");
}

Console.WriteLine(viesti_s);
Console.ReadKey();
Sign up to request clarification or add additional context in comments.

2 Comments

Please elaborate your answer and explain what the code does because code-only answers can be hard to understand.
It's pretty obvious I think. He declares the string viesti_s BEFORE the for loop, and then references it from within the for loop. Having it outside of the for loop allows him to reference it at any point after the declaration. Anything declared within the for loop is exclusive to the for loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.