views:

181

answers:

5

Hello,

I asked a question about interfaces previously and got some excellent responses. I'm really excited to start developing really flexible code.

I now have a question about overriding a virtual method.

I am currently working with the Community Server SDK. One of the controls is a 'tag' bread crumb. So it will list the 'tags' on the relevant object.

<CSBlog:TagBreadCrumb />

I am using tags to allow adminstrators to target specific spots for content to appear in a layout, however those tags are not relevant to the end user. For example, 'Home_Main' tag is used to target the main panel on the home page, but I don't want user to see that.

Since I have the SDK for Community Server, I found the control which renders the 'tag list' and made modifications to it that do what I want - however my understanding is that it's not good to modify the core for many reasons.

So what I'd like to do is create my own control, which is essentially almost 100% identical to the base control, but override one method.

Here is the method:

 public virtual string FormatTags(string[] tagList)

Inside of there is code to format the tags, and I'd like to write my own code which does all of the same stuff, except checks a configuration file for 'tags' to exclude.

So my understanding is that I create my control, deriving from the base control - and I can write my own FormatTags() method - is that correct?

I only need to change one aspect of it, but does this mean I have to copy the original code for that method from the base class - and change what I need to in there? It just doesn't feel right copyign code from one spot, but I don't know of another way to extend the method without changing code in the base class.

Thanks for any suggestions.

+4  A: 

You can still run the dervied method and operate on it's results:

public override string FormatTags(string[] tagList) {
    string result = base.FormatTags(tagList);
    // do something with result
    return result;
}
BC
Interesting! One of the things that happens in FormatTags is the tags are turned into links - so that makes it harder for me to parse them out, rather then just not including them. Should I deal with that - or at that point do I copy the base code and make some changes? Thank you.
+1  A: 

So my understanding is that I create my control, deriving from the base control - and I can write my own FormatTags() method - is that correct?

That's right; when you override, you provide your own implementation of a base class's method.

In the best case, one of two things happen:

  • There is an easy way to transform the output of the base class's method into the desired result.

  • The base class is using a Strategy pattern or variant thereof, so that you simply provide an alternate implementation of the appropriate portion of the Strategy.

Unfortunately, it doesn't sound like either of those is the case here, so you may have to write it from scratch.

John Feminella
+1  A: 

So my understanding is that I create my control, deriving from the base control - and I can write my own FormatTags() method - is that correct?

Absolutely correct.

If you're finding that you're needing to copy a lot of code around, you should look at restructuring your method. Put the code that's being copied into another method that you won't override, and then when you override FormatTags, change what you need to and just access that code you would have copied by calling the other method.

patjbs
+1  A: 

Take a look at extension methods. These allow you to extend the capability of a sealed class that you cannot edit or extend, by defining your function in your own static class. Here's an example I've just written (may have syntax errors).

public static class MyExtensions
{
    public static string AppendCrazyText(this string s, string crazyText)
    {
        return s + crazyText;
    }
}

Now, to access this, I can simply call:

string myString = "Hello world";
string myCrazyText = ", lets go crazy!";

string myResult = myString.AppendCrazyText(myCrazyText);
nbolton
+5  A: 

In your case (Where you want to suppress certain tags from being printed in the breadcrumbs), you would strip out all special tags before calling the base method.

So your code would look something like:

public override string FormatTags(string[] tagList)
{
  // strip special tags
  string[] newTagList = stripTags(tagList);
  return base.FormatTags(newTagList);
}

This way you don't need to understand how the base class formats the output string.

If you instead wanted to modify the format of the output string, you wouldn't make any calls to the base method (Since that method might be changing it's formatting in future releases, which you don't want to be impacted by).

PeterR
Wow. Seeing that made a light bulb go off!! Thank you!