3

I want to pass a condition as Action to another method. First line in "ComputerPriceGenerator" works, but how to make the array work (second line)?.. Any ideas

I'm looking for advice..., CalculateAllPrice is not designed yet

public void ComputerPriceGenerator()
{
    //Below line Works
    PriceMachine.CalculatePrice(cart.Computers[0],() => ComputerConverter(cart.Computers[0]));           
    //How to make this work, i don't want to loop it???
    PriceMachine.CalculateAllPrice(cart.Computers,() => ComputerConverter(??));
}

public void ComputerConverter(Computer comp)
{
    if (comp.Memory <= 2)
        comp.Discount = 10;
}
4
  • 2
    Your code would have been a great candidate to use LINQ, however it is full of side-effects. I'd consider refactoring. Jan 31, 2011 at 23:52
  • 1
    Without showing the signatures of CalculatePrice and CalculateAllPrice it's hard to know how to help you.
    – Jon Skeet
    Jan 31, 2011 at 23:53
  • 2
    @Jon Skeet: It seemed like he was looking partly for advice on what the signature of CalculateAllPrice should be. I could be wrong, though. Jan 31, 2011 at 23:57
  • @JonSkeet, @Justin Morgan... I'm looking for advice..., CalculateAllPrice is not designed yet, but for CalculatePrice it takes type Action CalculatePrice(Computer comp, Action myAction)
    – kayak
    Feb 1, 2011 at 0:04

3 Answers 3

10

Your CalculatePrice method shouldn't take just Action, IMO - both methods should take Action<Computer>. So I would have the methods like this:

public static void CalculatePrice(Computer computer, Action<Computer> action)
public static void CalcuateAllPrices(IEnumerable<Computer> computers,
                                     Action<Computer> action)

and call them like this:

PriceMachine.CalculatePrice(cart.Computers[0], ComputerConverter);
PriceMachine.CalculateAllPrice(cart.Computers, ComputerConverter);
4
  • Jon, i have a unsolved question(this question is not related to this one, i will delete this) .., Let me know if you have any suggestion " stackoverflow.com/questions/4444972/… "
    – kayak
    Feb 1, 2011 at 0:32
  • 1
    @kayak: The answers there say pretty much what I would. You almost certainly don't want to go down that road.
    – Jon Skeet
    Feb 1, 2011 at 0:35
  • Jon, is your second method an example of a monad? I've been trying to wrap my head around what they're all about and how to use them. Feb 1, 2011 at 5:19
  • @Justin: No, it's not really a monad IMO - there needs to be rather more involved than just a single method signature, for one thing.
    – Jon Skeet
    Feb 1, 2011 at 5:27
1

As you want to apply the method to all elements of the array, you won't get around iterating over it.

You could define PriceMachine.CalculateAllPrice as such:

public void CalculateAllPrice(IEnumerable<Computer> data, Action<Computer> action)
{
  foreach(Computer c in data)
    action(c);
}
1
  • i agree but how to pass the Action from main method " PriceMachine.CalculateAllPrice(cart.Computers,() => ComputerConverter(??));"
    – kayak
    Feb 1, 2011 at 0:11
1
PriceMachine.CalculateAllPrice(cart.Computers, (Computer x) => ComputerConverter(x));

Then make CalculateAllPrice iterate through cart.Computers and pass each one to the anonymous function.

2
  • 1
    Seems a bit overkill. (cart.Computers, (Computer x) => ComputerConverter(x)) is the same as (cart.Computers, ComputerConverter).
    – Juliet
    Feb 1, 2011 at 0:02
  • @Juliet: Sure, I was just keeping with the lambda style he posted. Feb 1, 2011 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.