Home > Software design >  How can I use variable inside of for loop?
How can I use variable inside of for loop?

Time:05-05

using System;

namespace something
{
    class Program
    {
        static void Main(string[] args)
        {
            int roomHeight = 10;
            int roomLenght = 10;
            string Lenght;

            for (int i=0; i< roomLenght; i  )
            {
                Lenght = string.Join(Lenght, ("a"));
            }
        }
    }
}

This doesn't compile, it does not recognize: Lenght = string.Join(Lenght, ("a")); I want it to recognize it as the string I declarated. Forgive me, i'm a starter :)

CodePudding user response:

using System;

namespace something
{
    class Program
    {
        static void Main(string[] args)
        {
            int roomHeight = 10;
            int roomLength = 10;
            string length = string.Empty;

            for (int i=0; i< roomLength; i  )
            {
                length= string.Join(length, ("a"));
            }
        }
    }
}

Length needs to be assigned before you have it as a parameter in string.Join

  •  Tags:  
  • c#
  • Related