Ok - now for the fourth time I am trying to post this answer: (what the hell is going on with this forum?)
How To Write A Class would be the appropriate section to learn how to insert a method - but this isn't what we are talking about here.
You already know how to write a class. You want to know how to avoid writing the same code twice in the same class. This is done by taking the duplicate code and removing it from every place you have written it. Then you write a new method in the same class that contains this code and simply call this new method from all of your other methods.
So here is your current code:
public class Alpha
{
public Horse GetBestHorse(Horse[] horses)
{
var best = horses[0];
foreach (var horse in horses)
{
var bestOdds = 100/best.Races*best.Wins;
var horseOdds = 100/horse.Races*horse.Wins;
if (horseOdds > bestOdds)
best = horse;
}
return best;
}
public int HowMuchToBet(Horse horse, int totalCash)
{
var horseOdds = 100/horse.Races/horse.Wins;
var bet = totalCash/100*horseOdds;
return bet;
}
}
What you want to do is not have to type "100/x*y" over and over again. So what you do is you create a new method that does this operation and call that method from your other methods:
public class Alpha
{
public Horse GetBestHorse(Horse[] horses)
{
var best = horses[0];
foreach (var horse in horses)
if (GetHorseOdds(horse) > GetHorseOdds(best))
best = horse;
return best;
}
public int HowMuchToBet(Horse horse, int totalCash)
{
return totalCash/100*GetHorseOdds(horse);
}
public int GetHorseOdds(Horse horse)
{
return 100/horse.Races * horse.Wins;
}
}
By adding the GetHorseOdds method I have removed the need to type this "process" out in any other method. Every method that I add to this class can now call this method any time it wants to get the win odds for a horse.
Technically you would just put this method on the "Horse" class