473,395 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1796
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bill M. | last post by:
Hello, I would like to extend or sub-class the base HTMLSelectElement and add some custom properties and methods. So far this works to create a new select element. var new_select= new...
10
by: Jerzy Karczmarczuk | last post by:
Gurus, before I am tempted to signal this as a bug, perhaps you might convince me that it should be so. If I type l=range(4) l.extend() l gives , what else... On the other hand, try
2
by: Boobie | last post by:
I switched to using this function to create element: ---------------------------------------------------- function elem(name, attrs, style, text) { var e = document.createElement(name); if...
6
by: jk | last post by:
Looking through WebUIValidation.js, I discovered that the standard validators don't cater for non-numeric date formats (e.g. dd-MMM-yyyy) which I would like to do To keep code to a minimum, I...
3
by: Kevin | last post by:
Hello, I do not have experience extending a vb.net class and would like some assistance. I want to extend the Microsoft.Solutions.Framework.Address class to include a phone number. How do i do...
3
by: efiryago | last post by:
Can someone clarify what is EXTEND USING option in the CREATE INDEX command for, what kind of index structure it creates? Is it a new feature introduced in FP10, or it's been around for awhile yet?...
7
by: Matt Kruse | last post by:
Is it possible to extend the HTMLCollection prototype in Firefox (>=2.0)? It looks like I can do it, but it doesn't work: HTMLCollection.prototype.funk = function() { alert(this.length); }...
3
by: jacobstr | last post by:
I've noticed Object.extend used in a few different ways and I'm having trouble distinguishing why certain usages apply to a given situation. On line 804 Ajax.Base is defined as follows: ...
2
by: jidixuelang | last post by:
As I know,it's not well to extend Object.prototype derictly. In the Prototype(JS Framewoke),there is no extend Object.prototype. It only add some static method for Object class. I want to konw the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.