mardi 29 mars 2016

Applying one IEnumerable

I have 2 functions that are returning an IEnumerable<Stat>.

This function returns all the individual properties belonging to the CharacterStats class:

private IEnumerable<Stat> GetAllStats()
{
    yield return Level;
    yield return Health;
    yield return Damage;
    yield return Defense;
    //several others
    yield return LifePerSecond;
}

This function gets the stored (base) values from my newly added sqlite db:

private IEnumerable<Stat> GetBaseStatsFromDB()
{
    //connection code ...

    while (rdr.Read())
    {
        yield return new Stat
        {
            Name = (Enums.StatName)Enum.Parse(typeof(Enums.StatName), rdr.GetString(1)),
            MinValue = rdr.GetInt32(2),
            MaxValue = rdr.GetInt32(3),
            CurrentValue = rdr.GetInt32(4)
        };
    }

    //close connection
}

What I would like to do is return a new instance of CharacterStats where each Stat is initialized from the DB and assigned to the instance, something like:

GetAllStats.ToList().ForEach( //apply matching stat from GetBaseStatsFromDB());

I am able to successfully get the values from the DB and create a new Stat object out of each. However, I've been unable to apply these values to the CharacterStats class.

There are currently ~ 16 stats and these may be added/removed as I continue building this project, so ideally I'd like to find a method that will continue to work as this list changes.

How can I apply the result of GetBaseStatsFromDB() to all properties on CharacterStats by using the GetAllStats() method?

Aucun commentaire:

Enregistrer un commentaire