Banane777
Banane777

Reputation: 25

Multidimensional array assignment does not work C#

I would like to check a multidimensional array, if it contains a value. Then I would like to fill them with a value. But for one reason, because I did not discover yet, he does not want to fill the value. I am a beginner. Don't expect too much...

switch(menuChoose) -> case 1

string[,] kontodaten = new string[10, 4];
....
switch (menuChoose)
{
    case 1:
        bool eingabe = true;
        for (int i = 0; i <= 9; i++)
        {
            if (kontodaten[i, 0] == null || kontodaten[i, 1].Length == 0)
            {
                row = i;
                i = 9;
            }
        }
        while (eingabe == true)
        {
            try
            {
                Console.WriteLine("Bitte geben Sie ihren Vornamen ein: ");
                kontodaten[row, 0] = Console.ReadLine();
                Console.WriteLine("Bitte geben Sie ihren Nachnamen ein: ");
                kontodaten[row, 1] = Console.ReadLine();
                Console.WriteLine("Bitte geben Sie ihren PIN ein: ");
                kontodaten[row, 2] = Console.ReadLine();
                eingabe = false;
            }
            catch
            {
                Console.WriteLine("\n\a\tFEHLER: Eingabe ungültig");
            }
        }
        break;
    case 10:
        for (int j = 0; j <= 9; j++)
        {
            Console.WriteLine("Vorname:{0};Nachname:{1};PIN:{2};Kontostand:{3} -- {4}", kontodaten[j, 0], kontodaten[j, 1], kontodaten[j, 2], kontodaten[j, 3], j);
        }
        break;
}

Upvotes: 0

Views: 84

Answers (1)

CodeMonkey1770
CodeMonkey1770

Reputation: 104

This looks like some kind of programming exercise, so I won't go into the problems of having an array with a limited size.

Is kontodaten a class variable or a local variable in your method? If it is a local variable you are reinitializing it new every time the function is called. So when you run into "case 10" you have a perfectly new and empty array.

Try to put it outside your function with an access modifier like this: private string[,] kontodaten = new string[10, 4];

Upvotes: 1

Related Questions