2

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!

Toadums
  • 2,772
  • 8
  • 44
  • 67

3 Answers3

6

You're looking for Extension Methods. Here's the MSDN documentation.

Writing an extension method looks like this:

public static class OrderSelectionExtensionMethods
{
    public static void reorder(this OrderSelection[] orders, int x, int y) 
    {
        // Do something with each order
    }
}

Extension methods are usually defined in an entirely separate class.
Also, two things are required for Extension Methods:

  • The entire class must be static
  • The first parameter for each extension method must have the keyword this before it

With the above code, your example code would compile fine:

OrderSelection[] order = new OrderSelection[10];
//do stuff
order.reorder(1,2);

This is the exact same as writing the following:

OrderSelection[] order = new OrderSelection[10];
//do stuff
OrderSelectionExtensionMethods.reorder(order, 1, 2);
Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
1

You should make your own collection class that inherits Collection<OrderSelection> and contains additional methods.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Yeah, that's definitely a better solution than creating extension methods. – Scott Rippey Dec 28 '11 at 01:21
  • @ScottRippey are you being sarcastic? lol the extensible thing is sooo easy – Toadums Dec 28 '11 at 01:24
  • @Toadums Not sarcastic. I love creating extension methods, but it's not as organized and refactorable as creating a custom collection. A custom collection says "here is a bunch of objects, and a bunch of standard things you can do with the objects". But Extension Methods say "here's a bunch of shortcuts for doing standard things with these objects". You never know if an extension method exists until you import the proper namespace, but collection methods always exist. And they take about the same amount of code. – Scott Rippey Dec 28 '11 at 01:31
  • @Toadums And while I agree that this is the best **solution** ... it clearly does not **answer the question**, so no upvote from me. – Scott Rippey Dec 28 '11 at 01:33
  • @Scott Rippey ya, thats why you still got the checkmark. I cant figure out how to make this guys solution work. Yours was almost too easy haha – Toadums Dec 28 '11 at 01:37
0

I agree with Slaks, Making use of Generics and implementing Interfaces such as IEnumerable, ICollection will make your code cleaner and maintainable, for instance, by implementing the IEnumerable interface it makes possible to use the foreach statement. Depending on the complexity/size of your "OrderSelection" class you may decide the most suitable way to perform operations such as sorting. You may want to look at: http://msdn.microsoft.com/en-us/library/system.collections.ilist.aspx, http://support.microsoft.com/kb/320727,

I hope this helps