2

I am working in Unity but I guess the same applies to C# in general.

this is a class I made:

    public class KeyboardInput
    {
        private string name;
        private KeyCode btn;

        public KeyboardInput(string buttonName, KeyCode button)
        {
            name = buttonName;
            btn = button;
        }
    }

When I create an instance of the class, if I don't specify the values required by the constructor, I will get an error.

Now I want to create an array of the class and I want to specify the values, but how ?

This seems to be working fine without specifying the values

    public class InputController
    {
        private KeyboardInput[] defaultKeyBinding = new KeyboardInput[4];

        public InputController()
        {
            for (int i = 0; i < defaultKeyBinding.Length; i++)
            {
                //Something inside here
            }
        }
    }

I can tweak the code to be able to set the values inside the for loop, but I am very curious to know if there is a way!

3
  • 1
    You mean defaultKeyBinding[i] = new KeyboardInput(string, KeyCode)? Commented Sep 29, 2016 at 18:50
  • You should create a new instance of every KeyboardInput array item. Commented Sep 29, 2016 at 18:51
  • I'm confused - do you want to create instances when initializing the array or in the constructor? Where do the values needed by the constructor come from? What kind of error are you getting now? Commented Sep 29, 2016 at 18:52

3 Answers 3

3

The line

private KeyboardInput[] defaultKeyBinding = new KeyboardInput[4];

is just declaring an array, nothing is getting initialized yet. In your loop you probably want something like this.

for (int i = 0; i < defaultKeyBinding.Length; i++)
{
    //should look something like this
    defaultKeyBinding[i] = new KeyboardInput("Ayy", KeyCode.A);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Exactly what I was looking for!
0

Something like this will allow you to put your objects in the array without using the for-loop:

KeyboardInput[] defaultKeyBinding = new KeyboardInput[4];
defaultKeyBinding[0] = new KeyboardInput("someName", KeyCode.A);
defaultKeyBinding[1] = new KeyboardInput("someName2", KeyCode.B);

However, to avoid getting the error that happens when you don't specify values for the paramters in the constructor, you can use optional values. See the example on this page. In your case I don't know if it makes sense to assign default values to these parameters, but it would look something like this:

public KeyboardInput(string buttonName = "defaultButtonName", KeyCode button = KeyCode.A)
{
  name = buttonName;
  btn = button;
}

1 Comment

Thank you for the answer. I am not looking to go around the for loop. but your answer was helpful!
0
  KeyboardInput[] array = new KeyboardInput[]
  {
    new KeyboardInput("a",b),
    new KeyboardInput("a", b),
    new KeyboardInput("a", b)
  }

1 Comment

Please explain your answer as it relates to the question. Don't just post code.

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.