Problem with IModifierProvider and IEnumerable<float>

Hi,

So after my last question I got sick of working with a broken project, with the promise of working through an issue encountered by many students new to coding large projects looming somewhere in the future. I couldn’t see my health update because of a race condition, it was many lessons to go until the fix happened, and I wouldn’t learn anything working through the lessons in a linear fashion with a broken project, and skipping ahead would potentially mean missing key factors, so the only thing lef tfor me to do was check out a break point in the project’s repository.

So I’m working with respoitory code, and I add what Sam adds in this lesson, so it should work, right? Like, there is no difference between his code and mine, as I have committed my version of the project and am working on his, essentially. So the same code in the same files should produce the same results, no?

In the video he returns a float to a return type of IEnumerable, and my code doesn’t like that, rejecting the offered float total. Returning a type of float makes the method work but un-implements IModifierProvider.

Only differences from repository follow.

Fighter.cs

(...)
        public IEnumerable<float> GetAdditiveModifiers(Stat stat)
        {
            float total = 0f;
            foreach(IModifierProvider provider in GetComponents<IModifierProvider>()){
                foreach(float additiveModifier in provider.GetAdditiveModifiers(stat)){
                    total += additiveModifier;
                }
            }
            return total;
        }
(...)

IModifierProvider.cs:

using System.Collections.Generic;

namespace RPG.Stats{
    public interface IModifierProvider
    {
        public IEnumerable<float> GetAdditiveModifiers(Stat stat);

    }
}

Also, a suggestion: Perhaps students should be informed what a race condition is and how to deal with them, earlier? Sam mentions it a couple of times before it’s actually introduced as a concept, and there are many lessons still between that and learning how to do anything about them.

Thank you for reading this far!

In IEnumerables, you need to yield return total; not return total;. It’s part of the syntax of an IEnumerable.

1 Like

Oh for heaven’s sake. (Not what I said when I read your reply.)

Thank you!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms