0

I can't append an object to an array after a foreach loop. The object is okay, it contains all the right values which I found out through debugging.

In the end I want to have a custom Exercise object which contains a user-chosen number of custom ExerciseAnswer objects also. The array of ExerciseAnswer objects is the problem.

Here is the interesting part of my method:

static void CreateNewExerciseTest()
{
     string? exerciseName = "Test Exercise";
     string? exerciseTopic = "Test";
     string exerciseQuestion = "Does it work?";
     int numberOfAnswers = 2;
     int numberOfApplicableAnswers = 0;
     ExerciseAnswer[] exerciseAnswers = new ExerciseAnswer[2];                    

     foreach (ExerciseAnswer answer in exerciseAnswers)
     {
         int exerciseAnswerId = GenerateId();
         Console.WriteLine("\nEnter a name for this Exercise answer: ");
         string? exerciseAnswerName = Console.ReadLine();
         Console.WriteLine("Enter this answer for the Exercise: ");
         string exerciseAnswerContent = Console.ReadLine();
         Console.WriteLine("Enter y (yes) if this Exercise answer is applicable, 
                            otherwise press n (no) or any other key: ");
         char applicableAnswer = Console.ReadKey().KeyChar;
         bool applicable = ExerciseAnswer.EvaluateExerciseAnswer(applicableAnswer);
         if (applicable == true)
         {
             numberOfApplicableAnswers++;
         }

         ExerciseAnswer exerciseAnswer = new ExerciseAnswer(exerciseAnswerId,   
         exerciseAnswerName, exerciseAnswerContent, applicable);
         exerciseAnswers.Append(exerciseAnswer);
         // ... 
    }
}

This the GenerateId method:

static int GenerateId()
{
    return ++id;
}

The array exerciseAnswers does not contain the ExerciseAnswer elements it should while the exerciseAnswer object in the line above does. Maybe the problem is related to the declaration and initialization of exerciseAnswers and the foreach loop.

Has somebody have an idea?

Thank you!

8
  • 3
    Where does the Append method come from? You cannot append to an array in C#. You can to a List or similar datastructure but arrays are fixed-size. This shouldn't even compile. Commented Jul 8, 2022 at 14:45
  • Unless there is some unknown (and pretty useless) extension that add an Append method to the Array class. Like this Commented Jul 8, 2022 at 14:52
  • The Append method is part of the array. There is no compile time error. Commented Jul 8, 2022 at 14:52
  • 1
    "The Append method is part of the array." - It's most definitely not. Commented Jul 8, 2022 at 14:54
  • 2
    No, the Array class has no native Append method. So it should be an extension. But we digress here. Just use a List<ExerciseAnswer> and loop for two times Commented Jul 8, 2022 at 14:54

2 Answers 2

2

I believe you are using Append method from System.Linq namespace

public static IEnumerable<TSource> Append<TSource>(this IEnumerable<TSource> source, TSource element);

This method returns a new IEnumerable which contains your exerciceAnswer

With this piece of code you can understand what is going on:

var result = exerciseAnswers.Append(exerciseAnswer);
Console.WriteLine($"exerciseAnswers count = {exerciseAnswers.Count()}");
Console.WriteLine($"result count = {result.Count()}");

Console output:

exerciseAnswers count = 2
result count = 3
Sign up to request clarification or add additional context in comments.

2 Comments

That sounds reasonable. That would then be this one: Enumerable.Append - OP: If you use this be aware of the implications of the copy: Remarks: "This method does not modify the elements of the collection. Instead, it creates a copy of the collection with the new element." => this is very expensive.
Thank you, Append seems just to come from IEnumerable, it is also there, if I don't use Linq.
0

Append will just append to existing elements of array, since in your case size is already defined as 2(new ExerciseAnswer[2]) so it is not appending anything. What you can do is either have a new array and get the elements added to it or just get the index of element you are running the loop and replace same in the array. Something like below:- int elementIndex = Array.IndexOf(exerciseAnswers,answer); exerciseAnswers[elementIndex] = exerciseAnswer;

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.