0

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;
}
2
  • Regardless of whether it works or not, the solution is not optimal. As far as I can see you want to be able to store account data of up to 10 persons. What if you want to save 11 one day? Delete one in the middle? ... Also your 4 fields for each record. What if you also want to save an address? You always have to change you whole program and have a good chance to break something. What I would suggest is to use a List<AccountData>. You can simply add, remove, sort, ... things. And Your AccountData is a class with all properties you need. Even if you are a beginner. Don't learn a dead end. Commented Dec 4, 2020 at 8:55
  • @Klamsi wow you go more of this question then i did. However, i cant read the language, so that probably doesn't help :) Commented Dec 4, 2020 at 8:58

1 Answer 1

1

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];

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

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.