NeIT
NeIT

Reputation: 155

When i added buttons in array. I got an error " An array initializer of length "12" is required"

I added to the array with buttons, in the array and gave an error

I wanted to have arrays with buttons in the array and I could output them but it gave an error

class CalendarBase
{
    public Button[] Mounth = new Button[12] 
    {
        public Button[] January = new Button[32];
        public Button[] February = new Button[32];
        public Button[] March = new Button[32];
        public Button[] April = new Button[32];
        public Button[] May = new Button[32];
        public Button[] June = new Button[32];
        public Button[] July = new Button[32];
        public Button[] August = new Button[32];
        public Button[] September = new Button[32];
        public Button[] November = new Button[32];
        public Button[] December = new Button[32];
    }
}

Upvotes: 0

Views: 104

Answers (2)

ui_guy
ui_guy

Reputation: 133

To expand on the excellent answer by @tim-rutter :

What I think you are wanting to do is best achieved using a Dictionary. A Dictionary allows you to specify a unique key (in this case a Month) and associate it with a value (in this case, an array of buttons).

Simple Implementation:

class CalendarBase
{

        public Dictionary<string, Button[]> Month = new Dictionary<string, Button[]>()
        {
            {"January", new Button[32] },
            {"February", new Button[32] },
            {"March", new Button[32] },
            {"April", new Button[32] },
            {"May", new Button[32] },
            {"June", new Button[32] },
            {"July", new Button[32] },
            {"August", new Button[32] },
            {"September", new Button[32] },
            {"October", new Button[32] },
            {"November", new Button[32] },
            {"December", new Button[32] },
        };
}

Then, you can access each array of buttons like you would an array:

var februaryButtons = Month["February"];

A better implementation would be to use an Enum for the key so you don't need to worry about a misspelling of a string throwing an exception when attempting to access a value.

Better:

enum Months
    {
        January,
        February,
        March,
        April,
        May,
        June,
        July,
        August,
        September,
        October,
        November,
        December
    }

class CalendarBase
{
        public Dictionary<string, Button[]> Month = new Dictionary<string, Button[]>()
        {
            {Months.January, new Button[32] },
            {Months.February, new Button[32] },
            {Months.March, new Button[32] },
            {Months.April, new Button[32] },
            {Months.May, new Button[32] },
            {Months.June, new Button[32] },
            {Months.July, new Button[32] },
            {Months.August, new Button[32] },
            {Months.September, new Button[32] },
            {Months.October, new Button[32] },
            {Months.November, new Button[32] },
            {Months.December, new Button[32] },
        };
}

Then, you can access each array of buttons like this:

var februaryButtons = Month[Months.February];

Upvotes: 1

Tim Rutter
Tim Rutter

Reputation: 4679

The way you are initialising the array is wrong, this is how you would initialise a array of 12 buttons

class CalendarBase
{
    public Button[] Months = new Button[12] 
    {
        new Button(),
        new Button(),
        new Button(),
        new Button(),
        new Button(),
        new Button(),
        new Button(),
        new Button(),
        new Button(),
        new Button(),
        new Button(),
        new Button()
    }
}

Or more succinctly

Button[] Months = new Button[12];
for(int i =0; i<12; i++)
    Months[i] = new Button();

If you want to be able to access the buttons by a named member for each month you could create a property for each:

public Button January => Months[0];
public Button February=> Months[1];
//etc

Its not completely clear what you want though. If you want an array of 12 arrays of 32 buttons, it would be initialised like this:

Button[][] Months = new Button[12][];
for (int i = 0; i < 12; i++)
{
    Months[i]= new Button[32];
    for (int j = 0; j < 32; j++)
        Months[i][j] = new Button();
}

which if you're a fan of doing things in one line can be done like this:

Months = Enumerable.Range(0, 12)
                   .Select(e =>  
                        Enumerable.Range(0, 32)
                        .Select(f => new Button()).ToArray())
                   .ToArray();

Upvotes: 1

Related Questions