Click here to Skip to main content
15,895,606 members

Comments by evedvvv (Top 3 by date)

evedvvv 29-Nov-21 3:16am View    
Nice and simple way to do it. I have tried it but I get an error; The type arguments for method 'method' cannot be inferred from the usage. Try specifying the type arguments explicitly.
evedvvv 28-Nov-21 17:58pm View    
No, but I've been reading about IEnumerable and it seems like it is what I'm looking for. But since we haven't learned it I'm not sure if there is a more "simple/beginner"- approach to do this.
evedvvv 28-Nov-21 11:58am View    
My program, with all of my methods that I have at the moment, is working fine. The problem is that I have one method that distributes chocolates and three other methods that ORDER my list, and then I call my distribution method in them. So basically I have several methods almost doing the exact same thing:

public void DistributeChocolatesByAge(int chocolates)
{
People = People.OrderBy(x => x.Age).ToList();
ChocolatesToDistribute(chocolates);
}

public void DistributeChocolatesByFirstName(int chocolates)
{
People = People.OrderBy(x => x.FirstName).ToList();
ChocolatesToDistribute(chocolates);
}

public void DistributeChocolatesByLastName(int chocolates)
{
People = People.OrderBy(x => x.LastName).ToList();
ChocolatesToDistribute(chocolates);
}

My professor said that my code looks right but that it would be nicer to just have +1 overload on my general distribution method, that will take a parameter that handles the sorting/order of my list:

public void ChocolatesToDistribute(int chocolates) //instead of chocolates
{
int chocolatesAmount = chocolates;
for(int i = 0; i < chocolates; i++)
{
foreach (Person person in People)
{
if(chocolatesAmount > 0)
person.Chocolates++;
chocolatesAmount--;
}
}
}