For Programmers: Free Programming Magazines  


Home > Archive > C# > February 2005 > "Extending" an object vs. creating a "helper" class - newbie ques









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author "Extending" an object vs. creating a "helper" class - newbie ques
Joel Thornton

2005-02-11, 8:58 pm

This is probably a newbie question, but here goes.

Say I want to extend functionality of a given object, e.g. I want to
add an LoadFrom() method to
Collections.Specialized.NameValueCollection.

So I thought I would do something like this:

========================================
===================
public class NameValueCollectionEx : NameValueCollection
{
public void LoadFrom(string source, char InnerDelim, char
OuterDelim)
{
// implementation of LoadFrom()
}
}
========================================
===================

Well the problem is then that any existing .Net class that takes
NameValueCollection as its argument, can NOT take my derived
NameValueCollectionEx as an argument.

This leads me to think I should instead create a helper class, e.g.
NameValueCollectionLoader with a function to return NameValueCollection
objects as needed.

It seems like it would make a lot more sense to be able to
transparently "augment" the existing NameValueCollection class here,
though. Is that possible in C#?

Thanks much ... I looked for an answer in the MSDN best practices
documents but I'm still not clear.

Joel

Keenan Newton

2005-02-13, 8:59 am

I am a little here. I beleive your conclusion that a .Net
class that takes NameValueCollection as parameter can not take
NameValueCollectionEx is not right. Since NameValueCollectionEx
inherits from NameValueCollection the CLR can determine the base
implementation now granted any .Net class that uses NameValueCollection
could only interface with the NameValueCollection class members.
However to use the NameValueCollectionEx implmentation simply cast the
NameValueCollection into a NameValueCollectionEx assuming of couse your
object derived orginally from NameValueCollectionEx. Here is some
sample code

NameValueCollectionEx myCollectionEx = new NameValueCollectionEx();
myCollectionEx.LoadFrom(someString);
myWebClient.UploadValues(uriString,myCollectionEx);

you also do this.
NameValueCollectionEx myCollectionEx = new NameValueCollectionEx();
myCollectionEx.LoadFrom(someString);
NameValueCollection myCollection = myCollectionEx;
NameValueCollectionEx secondCollectionEx =
(NameValueCollectionEx)myCollection;

Joel Thornton

2005-02-16, 4:04 am

Awesome! Thanks so much for the reply. Evidently I've been working in
gimped languages like VBScript for so long that the obvious is anything
but obvious to me! :)

On a related question, when I do

public class NameValueCollectionEx : NameValueCollection
{ /* something good goes here */ }

and then instantiate this object, I notice that it doesn't
automatically inherit any of NameValueCollection's constructors. In
order to achieve this do I need to manually 'clone' each of the parent
object's constructors like so?:

public NameValueCollectionEx() : base() { }
public NameValueCollectionEx(int capacity) : base(capacity) { }
public NameValueCollectionEx(NameValueCollectio
n col) : base(col)
{}
...

Thanks again for your help.

Joel

Pat

2005-02-16, 4:01 pm

Yep, that's exactly right. Although, I think that by default if you
create the default constructor, the compiler will insert the call to
:base() automatically. But, you're always better off speicfying it
yourself for clarity.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com