0

Hello i'm new to csharp and i experiment alot to see and solve difficult problems, however is one is a new error and i havent seen before:

"Wrong number of indices inside []; expected 1"

, i tryed to google it and try to solve it in my own way but i didnt find the solution. Could anyone help me solve this problem and say what i did wrong?

    public Form1()
    {
        InitializeComponent();
    }

    class Planeet
    {
        public String Naam;
        public Byte Grootte;
        public Brush Kleur;
        public short Afstand;


        public Planeet(String argNaam, Byte argGrootte, Brush argKleur, short gAfstand)
        {
            Naam = argNaam;
            Grootte = argGrootte;
            Kleur = argKleur;
            Afstand = argAfstand;
        }

    }

    private void pbZonnestelsel_Click(object sender, EventArgs e)
    {

        Planeet[] arrPlaneten = new Planeet[9]
        {
        new Planeet("Mercurius", 4, Brushes.DarkGray, 4),
        new Planeet("Venus", 10, Brushes.White, 20),
        new Planeet("Aarde", 10, Brushes.LightBlue, 40),
        new Planeet("Mars", 5, Brushes.Red, 60),
        new Planeet("Jupiter", 112, Brushes.Orange, 80),
        new Planeet("Saturnus", 94, Brushes.Beige, 200),
        new Planeet("Uranus", 40, Brushes.Green, 300),
        new Planeet("Neptunus", 38, Brushes.Blue, 350),
        new Planeet("Pluto", 2, Brushes.LightGray, 400)
        };


        for (int i = 0; i < arrPlaneten.GetLength(0); i++)
        {
            listBox1.Items.Add(arrPlaneten[i, 0,0,0]);
            listBox1.Items.Add(arrPlaneten[0, i, 0,0]);
        }

    }
}

}

4
  • 1
    arrPlaneten is a one dimensional array, so you cannot acess it this way: arrPlaneten[i, 0,0,0] but only arrPlaneten[i]. Commented Mar 13, 2013 at 11:25
  • To say what is wrong we need to know the expected result. Onw way to make it compile is replace the arrPlaneten.GetLength(0) with arrPlaneten.Length (not required but will have better performance) and inner loop with listBox1.Items.Add(arrPlaneten[i]); Commented Mar 13, 2013 at 11:25
  • That's not a multidimensional array, you should use it as arrPlaneten[i] Commented Mar 13, 2013 at 11:27
  • Hallo Nederlander... :) Commented Mar 13, 2013 at 12:35

3 Answers 3

3

What you are doing here, makes no sense. As you have a one-dimensional array, and treating it like a 4th dimensional array. Also the second add to the listbox is unnecessary when you are looping through all planets in the for loop.

    for (int i = 0; i < arrPlaneten.GetLength(0); i++)
    {
        listBox1.Items.Add(arrPlaneten[i, 0,0,0]);
        listBox1.Items.Add(arrPlaneten[0, i, 0,0]);
    }

What do you want to accomplish? Adding a planet to a listbox? You would that like so:

    for (int i = 0; i < arrPlaneten.GetLength(0); i++)
    {
        listBox1.Items.Add(arrPlaneten[i]);
    }

Note Your Planeet class needs to implement ToString() or else nothing will be shown in the listbox. So add

public String ToString() { return this.Naam; } to your class like so:

class Planeet
{
    public String Naam;
    public Byte Grootte;
    public Brush Kleur;
    public short Afstand;


    public Planeet(String argNaam, Byte argGrootte, Brush argKleur, short argAfstand)
    {
        Naam = argNaam;
        Grootte = argGrootte;
        Kleur = argKleur;
        Afstand = argAfstand;
    }

    public String ToString()
    {
        return Naam; //Or add more info here like return Naam + " " + Afstand;
    }
}

Second note: A listbox can hold a list of items of any object. But if you do not want to reference to object, but only its name you should do listBox1.Items.Add(arrPlaneten[i].Naam) instead.

you also have a typo in short argAfstand in your code, where it says short gAfstand

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

4 Comments

Ah, i see my mistake i should try to convert my one array into a multidimensional array, using each element in my array seperately. For example : listbox1: Mercurius,Venus, Aarde, Mars, Jupiter, Saturnus, Uranus.. instead of all the elements
Read my additional note in the answer. As you aren't there yet by solving the index problem. And I hope you mean " i shouldn't" instead of "i should".
@MikedeKlerk for completeness sake, maybe add an example with databinding the listbox too, rather than adding items manually. It's very likely to give the desired result and learning it now will save mrName a lot of work in the future.
@WillemDuncan Yes you are right, databinding is a nicer way for the listbox. But what if he is trying to do the same on a object without databinding possibilities? He had to learn about arrays and their indices first. I woudn't want to bother him with databinding now...
0

In listBox1.Items.Add(arrPlaneten[i, 0,0,0]); you are giving four comma-separated indices to the array, this is a syntax-error. If you want to retrieve an element from an array, do this:

for (int i = 0; i < arrPlaneten.GetLength(0); i++)
    {
        listBox1.Items.Add(arrPlaneten[i]);
    }

Use only a single index!

Comments

0

It looks like you're trying mis-addressing the array. You're indexing it as if it had four dimensions.

If you are trying to add all the items to the listbox, you can do this:

foreach(Planeet aPlanet in arrPlaneten)
{
    listbox1.Items.Add(aPlanet);
}

However, you will need to override ToString in your class in order for the listbox entry to make sense...

or you can use data binding listbox1.DataSource = arrPlaneten; and the select Listbox1.displayMember = "Naam"

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.