views:

386

answers:

4

I'm looking for a way to write a custom .net class that would allow for nested methods.

For example... say I have a class X with a function Y that returns a list. Then I have another function that returns a sorted list...

I would like to be able to do something like x.y().z() where z would accept the output of y() as its input.

Basically how .toLower() or .toUpper() can be tacked on to any string.

I'm trying to google it but I'm not even sure if I'm asking the right question.

Thanks

+1  A: 

Extension methods might be what you are looking for (can accept the output of y()), but that depends on the version of .NET you are using.

so if you wanted to create an extension method called x that takes y as a parameter, you would create a method:

public static object z(input as y)
{
    //do your manipulations here
}

so if you wanted your function to do sorting, you would call your sort method, pass the object, y, and return the object sorted.

Steve Wright
A: 

There's nothing magic which needs to happen. If class A has a method which returns an object of class B, then you can call methods on the function in A directly.

Given:

public static class MyClass     
{
  public MyList getList()
  {
    MyList retVal = new MyList();
    ...
    return retVal;
  }
}

public class MyList
{
    public MyList sort()
    {
        // Sort list
        ...
        return sortedList;
    }
}

then this is legal: MyList list = MyClass.getList().sort();

Jesse Weigert
A: 

In asp.net vb you can use a Module instead of a Class like this:

    Imports System.Runtime.CompilerServices
    Public Module Extensions
       <Extension()> _
       Public Function extendedMethod(ByRef input As String) As String
           Return input & "extended method"
       End Function
    End Module

Then in your code behind you import it the same as you would any class:

    Imports Extensions
    Partial Class _Default
    Inherits System.Web.UI.Page
       Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim st As String = "a string "
        Response.Write(st.ToUpper.extendedMethod)
       End Sub
    End Class

In this case you can use the "extendedMethod" method from the module on any string value in the same way you would use .toUpper() or .toLower()

Birk
A: 

Steve is right (no pun intended): Extension methods are what you're asking for. In C# you'd do something like this:

namespace ExtensionsNamespace; // Name this whatever you want.
public static class ListExtensions // must be public static!
{
    // must be public static and the first parameter needs a "this"
    public static IList<T> ToOrderedList<T>(this IList<T> originalList, IComparer<T> comparer)
    {
        // Code to take the original list and return an ordered version
    }
}

And then in your code:

using ExtensionsNamespace;
...
IComparer<Book> comparer = GetBookComparer();
IList<BooK> books = GetBookList().ToOrderedList(comparer);

There are some additional things you can do using lambda expressions to avoid the need to write your own comparer class in certain cases, and so forth. However, before you go reinventing the wheel I'd suggest you look at LINQ to Objects, which already has a lot of these functionalities built in. For example:

using System.Linq;
...
IEnumerable<Book> booksInOrder1 = GetBookList().OrderBy(b => b.Title);

Does that answer your question?

StriplingWarrior