Dashcarwash
Dashcarwash

Reputation: 3

wrong number of indices inside [], expected 5

I'm having a problem with my code. Ive made a multidimensional array to store elements of a person.

Now visual studio is telling me that i have 2 diffrent errors in my code.

First one is: wrong number of indices inside [], expected , and the second error message i get is: cannot implicitly convert type string[*,*] to string[*,*,*,*,*]

The code:

static void Main(string[] args)
{
    //wil je meer vrienden in de array pas hier het aantal aan :)
    int vrienden = 2;

    //maak array. de comma in de brackets is voor meer items, eerste waarde voor hoeveel mensen, tweede aantal elementen
    string[, , , ,] vriendenarray = new string[vrienden, 5];
    //^^ here i get the error code of cannot implicitly convert type 'string[*,*] to string[*,*,*,*,*]'

    Console.WriteLine("welkom bij het online vriendenboekje!");
    Console.WriteLine("hier kan je een antaal elementen van je vrienden opslaan");

    //vul de array met de elementen / vraag de mensen voor input
    for (int i = 0; i < vrienden; ++i)
    {
        Console.Write("Vul hier de naam in van je vriend -->");
        vriendenarray[i, 0] = Console.ReadLine(); //<-- here i get the error message of wrong number of indices inside []
        Console.Write("Vul hier zijn/haar favoriete kleur in -->");
        vriendenarray[i, 1] = Console.ReadLine(); 
        Console.Write("Vul hier zijn/haar favourite eten in -->");
        vriendenarray[i, 2] = Console.ReadLine();
        Console.Write("Vul hier zijn/haar favourite seizoen in -->");
        vriendenarray[i, 3] = Console.ReadLine();
        Console.Write("Vul hier zijn/haar favourite dier in -->");
        vriendenarray[i, 4] = Console.ReadLine();
    }

    //leest/displayed wat er allemaal is ingevuld
    for(int i = 0; i < vrienden; i++)
    {
        Console.WriteLine("Vriend nummer {0} is {1} en zijn favoriete elementen zijn {2}{3}{4}", i, vriendenarray[i, 0], vriendenarray[i, 1], vriendenarray[i, 2], vriendenarray[i, 3], vriendenarray[i, 4]);
    }
}

Upvotes: 0

Views: 164

Answers (4)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

You are actually working with 2D array: please, note that you always address it as

vriendenarray[x, y]

where x and y some values. That's why

string[,] vriendenarray = new string[vrienden, 5];

...

Edit: I suggest implementing a tailored class with properties, e.g.

public class Vriend {
  public string Naam {get; set;}
  ...
  public string Dier {get; set;}
}

then you can work with evident 1D array:

// Now we have an array of friend (not misterious 2d array of strings)
Vriend[] vriendenarray = new Vriend[vrienden];

// We create friends
for (int i = 0; i < vriendenarray.Length; ++i)
  vriendenarray[i] = new Vriend();

for (int i = 0; i < vriendenarray.Length; ++i) {
  // Please, note how it's easy now: we assign Naam, not second index 0
  Console.Write("Vul hier de naam in van je vriend -->");
  vriendenarray[i].Naam = Console.ReadLine();  
  ...  
  Console.Write("Vul hier zijn/haar favourite dier in -->");
  vriendenarray[i].Dier = Console.ReadLine();
}

// for each vriend in vriendenarray is more readable
foreach (var vriend in vriendenarray)
{
    // String interpolation allows us to put code - vriend.Naaam - within string
    Console.WriteLine($"Vriend nummer {vriend.Naaam}...");
} 

Upvotes: 3

Ahmed Msaouri
Ahmed Msaouri

Reputation: 316

You have declared a dimension to your list and would like to assign another demincion, this code corrected, but it makes no sense to declare all these demenciones:

string[,] vriendenarray = new string[vrienden, 5];

This code will work for you but I think you don't have to declare all these tables:

//wil je meer vrienden in de array pas hier het aantal aan :)
int vrienden = 2;

//maak array. de comma in de brackets is voor meer items, eerste waarde voor hoeveel mensen, tweede aantal elementen
string[,,,,] vriendenarray = new string[vrienden, 5,1,1,1];
//^^ here i get the error code of cannot implicitly convert type 'string[*,*] to string[*,*,*,*,*]'

Console.WriteLine("welkom bij het online vriendenboekje!");
Console.WriteLine("hier kan je een antaal elementen van je vrienden opslaan");

//vul de array met de elementen / vraag de mensen voor input
for (int i = 0; i < vrienden; ++i)
{
    Console.Write("Vul hier de naam in van je vriend -->");
    vriendenarray[i, 0, 0, 0, 0] = Console.ReadLine(); //<-- here i get the error message of wrong number of indices inside []
    Console.Write("Vul hier zijn/haar favoriete kleur in -->");
    vriendenarray[i, 1, 0, 0, 0] = Console.ReadLine();
    Console.Write("Vul hier zijn/haar favourite eten in -->");
    vriendenarray[i, 2, 0, 0, 0] = Console.ReadLine();
    Console.Write("Vul hier zijn/haar favourite seizoen in -->");
    vriendenarray[i, 3, 0, 0, 0] = Console.ReadLine();
    Console.Write("Vul hier zijn/haar favourite dier in -->");
    vriendenarray[i, 4, 0, 0, 0] = Console.ReadLine();
}

//leest/displayed wat er allemaal is ingevuld
for (int i = 0; i < vrienden; i++)
{
    Console.WriteLine("Vriend nummer {0} is {1} en zijn favoriete elementen zijn {2}{3}{4}", i, vriendenarray[i, 0, 0, 0, 0], vriendenarray[i, 1, 0, 0, 0], vriendenarray[i, 2, 0, 0, 0], vriendenarray[i, 3, 0, 0, 0], vriendenarray[i, 4, 0, 0, 0]);
}

Upvotes: 0

Mortaza Ghahremani
Mortaza Ghahremani

Reputation: 392

if you want to create 5 dimensional array you must do like below:

string[,,,,] vriendenarray = new string[vrienden, 5,1,1,1];

Your declaration is wrong , according to this mistake you shoud correct assignments such as:

 vriendenarray[i, 0] = Console.ReadLine(); 

to

 vriendenarray[i, 0,0,0,0] = Console.ReadLine(); 

becouse vriendenarray has 5 dimension

Upvotes: 0

mu88
mu88

Reputation: 5384

Welcome! As usual, Visual Studio is right ;)

By saying string[, , , ,], you're declaring an array with five dimensions. But you're initializing an array with two dimensions.

Due to your remaining code, I'd assume that you want a two-dimensional array. You can declare this with either string[,] vriendenarray = new string[vrienden, 5]; or var vriendenarray = new string[vrienden, 5];.

Upvotes: 0

Related Questions