0
using System;

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

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

This doesn't compile, it does not recognize Length = string.Join(Length, ("a"));

I want it to recognize it as the string I declarated.

5
  • assign Lenght before using it like string Lenght = string.Empty Commented May 4, 2022 at 17:01
  • 1
    You need to initialize Length. You can change string Length to e.g. string Length="". Commented May 4, 2022 at 17:01
  • 1
    Lenght = string.Join(Lenght, ("a")); <-- What exactly are you trying to do here? Commented May 4, 2022 at 17:09
  • If you're just trying to create a string of a certain length, you can use var length = new string('a', roomLength); No loop needed. Commented May 4, 2022 at 17:15
  • When asking a question make sure to include the exact error message you are getting. In your case you are likely getting CS0165 Use of unassigned local variable 'Lenght' This error message gives you most of what you need to know to fix the problem. Commented May 4, 2022 at 17:33

1 Answer 1

1
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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.