I didnt know how to word the question, so sorry if it doesnt really make sense, but it should start making sense here. Also, I am sorry if the solution is really, really simple. Google couldnt understand what I was asking (probs cause I was asking it wrong :P)
So I wrote a class that I called OrderSelection
In my program I need to have an array of OrderSelection objects, and I need to perform actions on this array (re-ordering, sorting etc).
What I am doing right now is keeping methods in the OrderSelection class that accept, among others, the array which you want to re-order, for example.
something like:
public void reorder(OrderSelection[] ord, int switchX, int switchY){....}
But What I want to be able to do is this:
OrderSelection[] order = new OrderSelection[10];
//do stuff
order.reorder(1,2);//which is WAY better than order[0].reorder(order, 1,2) as a horrid example
So yeah…how can I add these functions which I want to apply to an array of objects of my class?
thanks!
You’re looking for Extension Methods. Here’s the MSDN documentation.
Writing an extension method looks like this:
Extension methods are usually defined in an entirely separate class.
Also, two things are required for Extension Methods:
staticthisbefore itWith the above code, your example code would compile fine:
This is the exact same as writing the following: