Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

best approach

FrazFraz Member Posts: 38
Ive been thinking about this for sometime and although im not close to actually starting to program this, its something thats nagging me.

the problem:

2 class (Alpha1 and Alpha2)

both of these classes use a class (Beta1) to perform some calculation but with slightly different inputs.

classes Alpha1 and Alpha2 are also effected by a third class, Charlie1. Charlie1 can change another input inside Alpha1 and Alpha2 via another calculation (note: the input in Alpha1 doesnt have the same value as Alpha2).

now i know code reuse is a big thing in c# so i figured there must be a better way than just creating Alpha1 and making two methods that do the calculations Beta1 and Charlie1 and then copying and pasting that code over to Alpha2. the calculation in both Beta1 and Charlie1 is going to be long.

is this something where i could use inheritance for all of them? i.e. Beta1 inherits Alpha1 and Charline1 inherits Beta1 n values are passed to both Alpha1 and Beta1 to instantiate them? or is this a stupid idea?

Thanks for your help

Rgds

Fraz

Comments

  • DataDinkDataDink Member Posts: 135
    The description you have here is a little vague - can you maybe give some example code of your current implementation?

    I can't tell if B is being called by A or vice versa - and the same with A and C
  • FrazFraz Member Posts: 38
    i havent actually written any code for it, but was more looking for the best general approach.

    yeah my explaination was kinda cr-p so i will try again.

    (1) i have a class which i use to place bets on horses. (Alpha1)

    (2) i also have some method which i use to calculate which horse im going to bet on

    (3) i finally have another method which is used to determine how much i will bet based on the results of the previous bets and my capital etc

    im assuming (2) n (3) are better off as methods even though they may be quite large?

    if i had to then implement steps (2) and (3) on a new class (Alpha2) would i have to cut n paste them or is there a better approach?

    if this is still a poor explaination please point out where.

    Rgds

    Fraz
  • DataDinkDataDink Member Posts: 135
    No, you would just break up the two methods into reusable parts. Anything that can be copy-pasted, can also be inserted into another method and then called by both of your primary methods.
  • FrazFraz Member Posts: 38
    What's the topic which covers inserting methods. Is there a specific technical name for it? Or is it covered in the method sections of most textbooks?

    Thanks for your help

    Fraz
  • DataDinkDataDink Member Posts: 135
    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:

    [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;
    }
    }
    [/code]

    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:

    [code]
    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;
    }
    }
    [/code]

    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
  • FrazFraz Member Posts: 38
    I'm really sorry, I honestly didn't know. I do appreciate you taking the time to explain this again.

    Thank you
  • DataDinkDataDink Member Posts: 135
    I wasn't upset at answering your question - I just had to re-write the same post like 4 times because ProgrammersHeaven kept logging me out and losing everything that I had typed.

    You're fine - it's this forum software that appears to be falling apart.
Sign In or Register to comment.