tags:

views:

467

answers:

3

I'm writing a WebPart, which means that I inherit from System.Web.UI.WebControls.WebParts.WebPart and I override protected override void CreateChildControls().

However, I'd like to make the class sealed if possible, but that gives two issues: Just sealing the class gives a warning "new protected member declared in sealed class".

Changing the Access Modifier from protected to private or internal gives a compiler error telling me I can't change the modifier when inheriting.

That leaves me wondering: Is there any problem with sealing it and ignoring the warning? Or could this lead to any negative side effects further down the road? It seems to work just fine, but the devil is usually in the details.

Edit: I was just being stupid. The "new protected member" error was for a function that was indeed NOT overridden and just accidentially was declared as protected. Thanks for the Pragma-Tip though!

+1  A: 

The fact that it says there's a new protected member declared in the class is slightly worrying.

Hmm... I can't reproduce this in simple test code:

using System;

public class Base
{
    protected virtual void Foo()
    {
    }
}

public sealed class Derived : Base
{
    protected override void Foo()
    {
    }
}

compiles without warnings with .NET 3.5SP1. Are you definitely overriding the base method? Check that you really have got the override modifier. (Sorry if that sounds patronising - I'm not trying to accuse you of being lax or anything. I'm just stumped otherwise...)

Jon Skeet
+2  A: 

Are you sure you're overriding correctly? Personally I cannot repro this behavior. But if it worries you, you can use

#pragma warning disable 0628
// Offending code
#pragma warning restore 0628
Anton Gogolev
A: 

Sounds to me like it is being daft. I'd ignore the warning, after all it's only stating that what you are doing is illogical like having a public ctor on an abstract type. The worst case scenario is a bit of confusion.

I think i've had this as well but only in Compact Framework code, is it Full Framework in this case?

Quibblesome