471,337 Members | 882 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

How do I extend a method?

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!
Jun 27 '08 #1
7 1737
On Mon, 28 Apr 2008 10:57:20 -0700, cbmeeks <cb*****@gmail.comwrote:
Hope I'm using the right terminology. Anyway, say I have a class
like:

class Animal
{
public double GetValues()
{......}
The name "GetValues" implies that the method will return multiple values..
But yours only returns a single double.
public void FilterBy(string text);
{......}
}
What does the FilterBy() method actually do?
Now, I would like to add something like:

Animal a = new Animal();

a.GetValues();
a.GetValues().FilterBy("cats");
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.
How would I add the FilterBy method extension?
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.
I already have the logic for the FilterBy but its a separate 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
Jun 27 '08 #2
"cbmeeks" <cb*****@gmail.comwrote in message
news:65**********************************@m36g2000 hse.googlegroups.com...
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"
}
}

Jun 27 '08 #3
On Apr 28, 2:14 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 28 Apr 2008 10:57:20 -0700, cbmeeks <cbme...@gmail.comwrote:
Hope I'm using the right terminology. Anyway, say I have a class
like:
class Animal
{
public double GetValues()
{......}

The name "GetValues" implies that the method will return multiple values.
But yours only returns a single double.
public void FilterBy(string text);
{......}
}

What does the FilterBy() method actually do?
Now, I would like to add something like:
Animal a = new Animal();
a.GetValues();
a.GetValues().FilterBy("cats");

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.
How would I add the FilterBy method extension?

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.
I already have the logic for the FilterBy but its a separate 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<stringTags;
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.

Jun 27 '08 #4

I also forgot to mention I am using 2.0
Jun 27 '08 #5
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<stringTags;
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.
>
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.

Jun 27 '08 #6
On Mon, 28 Apr 2008 13:21:14 -0700, cbmeeks <cb*****@gmail.comwrote:
[...]
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 admit, I don't really see what the problem of having overloaded methods
is. Presumably the "unfiltered" version would just call the same
implementation as the filtered version. There should be very little extra
work.

However, as Ben alludes to, if you reverse the semantics you could do it
more like you're presenting here. In particular, create a Filter() method
that, given a specific tag, returns a new Metric instance that includes
only the data associated with that tag. Then the basic "Value",
"Average", etc. would still work without having to have each one support
an overload version.

A very basic implementation would simply create a standalone Metric
instance, unconnected to the original. It would be just like the original
Metric instance, but containing only the data that passes through your
filter.

But if you wanted to get fancy, and assuming it works well with your
present paradigm, you could design the Metric class and the Filter()
method so that the new instance is actually just a reference back to the
original one that contains the data. In the filtered instance, rather
than having a copy of the data, you'd just keep the tag you're using for
filtering, and do the appropriate thing in your calculations.

If you did this, then the new Metric would always be a filtering version
onto the original data. As data in the original changed, you could keep
using the filtering version to do the same filtered calculations on the
new data. Even if you didn't keep the previous filtering version,
creating new ones should be inexpensive, compared to the alternative of
creating a new copy of the data for each filtered Metric.

Doing it that way would be sort of like the "Tags property" solution you
mentioned, but without layering the filter state onto the existing Metric
instance. Instead, the filtering state would be unchanged for any given
Metric instance.

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.

As an alternative to the above, you might consider breaking the
functionality into two classes. One class to manage the data collection,
and another to do the filtering and calculations. This would be basically
the same as the "fancy" version of what I suggest above, with the
advantage that by making a second class responsible for filtering and
calculating, you would never have the ambiguity of a single class that is
doing two fundamentally different things. You'd always create an instance
of the filter-and-calculate class by passing to the constructor at a
minimum the data collection class, and optionally whatever filter criteria
is important (or you could keep the Filter() method on the collection
class, making it essentially a factory for the filter-and-calculate class).

And if that's not a complicated enough list of suggestions :), I'll point
out that the problem sounds suspiciously like a special case of a
database. You can "select" (which is the filtering process) and run
computations on the results. It may be that this could all be handled in
a more general way, using a basic collection class to contain your data,
and then LINQ syntax to do the filtering and computations.

Pete
Jun 27 '08 #7
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!
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Bill M. | last post: by
10 posts views Thread by Jerzy Karczmarczuk | last post: by
2 posts views Thread by Boobie | last post: by
3 posts views Thread by Kevin | last post: by
3 posts views Thread by efiryago | last post: by
7 posts views Thread by Matt Kruse | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.