C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2720
Number of posts: 5746

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
best approach Posted by Fraz on 18 Aug 2009 at 1:34 PM
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
Report
Re: best approach Posted by DataDink on 18 Aug 2009 at 3:32 PM
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
Report
Re: best approach Posted by Fraz on 19 Aug 2009 at 6:38 AM
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
Report
Re: best approach Posted by DataDink on 19 Aug 2009 at 7:18 AM
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.
Report
Re: best approach Posted by Fraz on 19 Aug 2009 at 8:02 AM
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
Report
Re: best approach Posted by DataDink on 19 Aug 2009 at 2:07 PM
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
Report
Re: best approach Posted by Fraz on 19 Aug 2009 at 2:57 PM
I'm really sorry, I honestly didn't know. I do appreciate you taking the time to explain this again.

Thank you
Report
Re: best approach Posted by DataDink on 19 Aug 2009 at 3:45 PM
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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.