Reputation: 181124
I'm writing a WebPart, which means that I inherit from System.Web.UI.WebControls.WebParts.WebPart
and I override the method 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!
Upvotes: 1
Views: 6800
Reputation: 1167
A protected member can be seen by sub classes so you're slightly altering the interface of the class; If you declare it 'private' it's only ever seen locally so it doesn't affect the interface.
Declare your method private not protected and the warning should go away.
Upvotes: 6
Reputation: 25429
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?
Upvotes: 0
Reputation: 115897
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
Upvotes: 5
Reputation: 1504062
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...)
Upvotes: 4