473,800 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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().F ilterBy("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 1814
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().F ilterBy("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******** *************** ***********@m36 g2000hse.google groups.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().F ilterBy("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 ExtensionMethod s
{
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...@nn owslpianmk.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().F ilterBy("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.AddValueAndTa g(100,"Sales");
m.AddValueAndTa g(200,"Taxes");

m.Value(); // 300
m.Value().Filte rBy("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.AddValueAndTa g(100,"Sales");
m.AddValueAndTa g(200,"Taxes");

m.Value(); // 300
m.Value().Filte rBy("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().Filte rBy("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
4102
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 Selector('new_select'); Selector = function(id) { var container = document.getElementById(id);
10
2039
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
1784
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 (attrs) { for (key in attrs) { if (key == 'class') { e.className = attrs; } else if (key == 'id') { e.id = attrs;
6
2695
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 would like to extend the existing validator to handle other common date formats and still be able to do both client and/or server side validation as normal. Unfortunately, I am unable to get the extended code to ever be called I implemented the...
3
2593
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 this? Kevin
3
1787
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? Thanks, -Eugene
7
4695
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); } window.onload= function() { var x = document.getElementsByTagName('div'); alert(HTMLCollection.prototype.funk); // function() { ... } alert(x instanceof HTMLCollection); // true alert(x.item===HTMLCollection.prototype.item); // false x.funk(); //...
3
3592
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: Ajax.Base = function() {}; Ajax.Base.prototype = { setOptions: function(options) { <...>
2
1709
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 reason.Who can give me some advise!?
0
9691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10035
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7580
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.