How do I extend a method?

C

cbmeeks

Hope I'm using the right terminology. Anyway, say I have a class
like:

class Animal
{
public double GetValues()
{......}

public void FilterBy(string text);
{......}
}


Now, I would like to add something like:

Animal a = new Animal();

a.GetValues();
a.GetValues().FilterBy("cats");

How would I add the FilterBy method extension?

I already have the logic for the FilterBy but its a separate method.

Thanks for any suggestions!
 
A

Alberto Poblacion

cbmeeks said:
Hope I'm using the right terminology. Anyway, say I have a class
like:

class Animal
{
public double GetValues()
{......}

public void FilterBy(string text);
{......}
}


Now, I would like to add something like:

Animal a = new Animal();

a.GetValues();
a.GetValues().FilterBy("cats");

The "FilterBy" does not extend the GetValues method; instead, it extends
the *result* of the GetValues method. In your example above, your GetValues
method returns a double, but I imagine that this is not a realistic example.
Assuming that GetValues returned an object of type MyClass, you would simply
add the FilterBy method inside MyClass. If you can't modify MyClass, and you
are using C# 3.0, you can extend that class with an "Extension Method"
defined inside a static class:

public static class ExtensionMethods
{
public static FilterBy(this MyClass obj, string arg)
{
//obj is the MyClass returned by GetValues
//arg is "cats"
}
}
 
C

cbmeeks

The name "GetValues" implies that the method will return multiple values.
But yours only returns a single double.


What does the FilterBy() method actually do?




The above syntax would work only if you added an extension method to the
double type. But honestly, given the description so far it's not at all
clear that doing so would accomplish what you're really trying to do. In
particular, I'd be surprised if given a single double value, a method that
takes a string as a parameter that describes an animal and is presumably
supposed to be filtering something based on that string could do anything
useful with the double.


Extension methods are described here:http://msdn2.microsoft.com/en-us/library/bb383977.aspx

However, it's not clear at all that what you really want is an extension
method.


If you could show that method, it might help explain what you're really
trying to do. So far, the outline you've given is very confusing and
doesn't look like anything that would normally be found in a correct
program. Maybe if we could see code that actually implements your goal,
but in a different way than you want, that would help.

Pete

Well, basically what I am doing is writing a framework to track
trending data.

A more realistic value would be:

class Metric
{
public double Value(){ ....return calculation....}
public AddValueAndTag(double value, string tag){.....add a double
to a List....}
public List<string> Tags;
public FilterBy(string tag){....add tag checking to whatever
method.....}
}

Metric m = new Metric();
m.AddValueAndTag(100,"Sales");
m.AddValueAndTag(200,"Taxes");

m.Value(); // 300
m.Value().FilterBy("Taxes"); // 200

This is a CRUDE example of what I have working. Method/Class names
are different but you should get what I am trying to do.
Right now, I have it working but I have to do something like:

m.Value(); // 300
m.Value("Taxes"); // 200

I don't like my current version because I actually have many methods
besides "Value". Things like Average, Sum, etc. I would hate to have
to overload every method to support tags.
I thought about having a "Tags Property" and have every method check
to see if there are tags present but I don't like that idea either.

Thanks Pete, you've been a real help.
 
B

Ben Voigt [C++ MVP]

Well, basically what I am doing is writing a framework to track
trending data.

A more realistic value would be:

class Metric
{
public double Value(){ ....return calculation....}
public AddValueAndTag(double value, string tag){.....add a double
to a List....}
public List<string> Tags;
public FilterBy(string tag){....add tag checking to whatever
method.....}
}

Metric m = new Metric();
m.AddValueAndTag(100,"Sales");
m.AddValueAndTag(200,"Taxes");

m.Value(); // 300
m.Value().FilterBy("Taxes"); // 200

This looks backwards.

Try to implement it as

m.FilterBy(tag).Value()

instead.
 
C

cbmeeks

If you do get rid of the overloads in this way, then you could just make
the calculations properties instead of methods. Personally, I think
that'd be a little nicer, though obviously it's not a critical part of the
design.

Once again Pete, I owe you a beer. That is a good idea.

I think I like that. Using Properties instead of methods.

So I could have;

Metric m = new Metric();
m.Sum; // property....get returns
calculation
m.FilterBy("tag"); // sets the filter....all
properties would look at this tag

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top