How to get Value by Id from List in another Class

65 Views Asked by At

I have the following code strucutre:

I have a Population class that has a List with all Humans of the population. The population class does also have a List with all existing Genes.

A Gene struct stores an Id and a Value (for simplicity just a simple string).

Now i want to pass the Id of the Gene that a Human has, and not the whole Gene struct because multiple Humans could have the same Gene and that would be redundant.

Imagine there are 100 different Genes and the Population is has a size of 100 and a Human has 5 Genes. Then i would have to store 100 * 100 * 5 Genes in total across all Humans. But if i would have a List of all available Genes stored in the Population and just pass the Id's of the Genes to the Human, then i would just need to store 100 Genes no matter how big the Population is and how many Genes a Human has.

But how do i retrieve the value of a Gene inside of a Human when the Human just has the Id?

internal class Population
    {
        List<Human> population = new List<Human>();

        List<Gene> Genes = new List<Gene>();

        public Population() 
        {
            CreateGenes(10);
            CreatePopulation(10);
        }

        private void CreateGenes(int GeneCount)
        {
            for (int i = 0; i < GeneCount; i++)
            {
                Genes.Add(new Gene() { Id = i, Value = "abc" });
            }
        }

        private void CreatePopulation(int populationCount)
        {
            for (int i = 0; i < populationCount; i++)
            {
                population.Add(new Human() { GenID = Genes[i].Id });
            }
        }
    }

internal class Human
    {
        public int GenID;

        public string GeneValue()
        {
            // HOW DO I GET THE VALUE FROM THE LIST IN THE POPULATION?
        }
    }

public struct Gene
    {
        public int Id;
        public string Value;
    }


2

There are 2 best solutions below

3
SMSTJ On BEST ANSWER

Ok so this will only work if Humans can access the Genes List. But id still recommend to store the Genes in the Humans, because Lists dont store the "Object" but a reference to that Object. Also your GenID in humans wont allow for a Human to have multiple Genes since you only have one ID. But Ill still try to do it along your lines One little change to population

public class Population{

    public static List<Gene> Genes = new List<Gene>();
    ...
}

And now a change in your Human class


public string GeneValue()
{
    return Population.Genes.First(g =>g.Id == GenID).Value;
}

But do not overarching Lists like theese arent really good to do. It does make sense to have a List from where you can access all Genes, if needed, but id still reccommend to have a Genes List in the Humans.

Or if the GeneValue Method is called from Population you can just make the List a parameter:


public string GeneValue(List<Gene> genes)
{
    return genes.First(g =>g.Id == GenID).Value;
}
0
gobes On

Human instances could have the same reference to a given Gene instance : you don't duplicate the object, but only the reference. Actually, to duplicate an object in C#, you have to do it really explicitly.

So, you could replace the Human.GenID by Human.HumanGene :

internal class Human
{
    public Gene HumanGene;

    public string GeneValue()
    {
        return HumanGene.Value;
    }
}

And redefine your CreatePopulation method like this :

private void CreatePopulation(int populationCount)
{
    for (int i = 0; i < populationCount; i++)
    {
        population.Add(new Human() { HumanGene = Genes[i] });
    }
}