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. Commented Jan 31, 2011 at 23:52
  • 1
    Without showing the signatures of CalculatePrice and CalculateAllPrice it's hard to know how to help you. Commented 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. Commented 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) Commented Feb 1, 2011 at 0:04

3 Answers 3

9

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);
Sign up to request clarification or add additional context in comments.

4 Comments

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: The answers there say pretty much what I would. You almost certainly don't want to go down that road.
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.
@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.
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 Comment

i agree but how to pass the Action from main method " PriceMachine.CalculateAllPrice(cart.Computers,() => ComputerConverter(??));"
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 Comments

Seems a bit overkill. (cart.Computers, (Computer x) => ComputerConverter(x)) is the same as (cart.Computers, ComputerConverter).
@Juliet: Sure, I was just keeping with the lambda style he posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.