1

I want to encapsulate delegate methods, which will be invoked later on. This delegate method, when invoked, will calculate some arguments at that instance of time and assign/return the value to a variable outside of the class.

For example, I have a class Tool

This tool should generate, let's say Point, when the delegate method is called...

public delegate void Action();

class Tool
{
public Action Select;
public Action Cancel;
public Point position;
public Tool(ref Point outPoint) //Cannot use ref with lambda...
{
Select = () => outPoint = position;
}

public void Update(Point newPosition)
{
position = newPosition
}
public void UpdateKey(Keys k, bool IsPress)
{
//Invoke Select/Cancel when press some keys
}

}

What I am trying to accomplish here is a simple function that should receive my input and fill it with correct argument after Select is invoked. If this was a single method, then it is easily done using ref. However, I have to keep track of many arguments, so I have to turn it into a class. Another thing is, this invocation happens inside the class, so I cannot return the calculated value from within the class. Even though I could change the design so that the update method is updated from outside of the class, what I desire is to have the class manages its own update routines. I know that there should be a better way, so if there is any, please tell me.

2
  • For instance: if he/she's using System.Drawing.Point, it's a struct. Commented Jun 10, 2012 at 15:40
  • It's a struct. Sorry for forgetting to inform that Commented Jun 11, 2012 at 2:24

2 Answers 2

1

If Point is a struct, you'll need to have a class that contains OutPoint.

class OutPointContainer { //Please, think of a better name :)
    public Point OutPoint{ get; set; }
}

class Tool
{
public Action Select;
public Action Cancel;
public Point position;

public Tool(OutPointContainer outPointContainer)
{
    Select = () => outPoint.OutPoint = position;
}

public void Update(Point newPosition)
{
position = newPosition
}
public void UpdateKey(Keys k, bool IsPress)
{
//Invoke Select/Cancel when press some keys
}

}

Later, you always use the Point from OutPointContainer.OutPoint

Sign up to request clarification or add additional context in comments.

Comments

0

As Point is a class, you can always change it's values and not the complete reference:

public Tool(OutPointContainer outPointContainer)
{
    Select = () => outPoint.UpdateWith(position);
}

And the method UpdateWith will do something like this:

public void UpdateWith(Point other){
    this.X = other.X;
    this.Y = other.Y;
}

3 Comments

So I have to create a container specifically for the Point that provide UpdateWith()? What if Point is a struct?
No, you don't need a container, just to update the values. If it's a struct you will be passing it as a value and not reference, so you'll need to create a container class
It sounds kinda like a workaround, so is there any other option?

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.