• Advertisement
Sign in to follow this  

[.net] Warning CS0628

This topic is 4581 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Lets say you have a simple singleton class ...
public class A
{
	private static A m_oA = null;
	private int m_iRefCount;

	protected A()
	{
		m_iRefCount = 0;
	}

	public A CreateInstance()
	{
		if (m_oA == null)
		{
			m_oA = new A();
		}

		++m_iRefCount;

		return m_oA;
	}
}

OK, lets now say you don't want anyone to inherit from this class, so you add the sealed keyword to the class ...
public sealed class A
{
   ...
}

The compilation generates a warning; warning CS0628: 'SampleApplication.A.A()' : new protected member declared in sealed class So I check out the help text for the warning ... "A sealed class cannot introduce a protected member because no other class will be able to inherit from the sealed class and use the protected member." WTF I think to myself, no other class will be able to inherit from the sealed class .... isn't that the point I was aiming for, so I check the help file for the sealed keyword... "A sealed class cannot be inherited. It is an error to use a sealed class as a base class. Use the sealed modifier in a class declaration to prevent inheritance of the class." So why the warning, I am at a loss to see why the compiler would warn you about a situation that cannot happen, unless I am completely missing the point (which may well be the case) I know c++ allows the #pragma warning disable, is there a c# equiv ? Cheers Steve

Share this post


Link to post
Share on other sites
Advertisement
I don't find the warning contradictory, but your code... with the 'sealed' keyword, you express that this class won't be derived from, and with 'protected', that this class is expected to be derived from.[headshake]
So, it is completely natural for the compiler to go 'Huh? WTF?' and spit out a warning.[wow]

Think about it, this is the kind of logic that makes evil supercomputers explode. [smile]

Share this post


Link to post
Share on other sites
Konfusius IS right. There is absolutely no point in declaring a protected member in a sealed class.

Read the warning more thoroughly:
A sealed class cannot introduce a protected member because no other class will be able to inherit from the sealed class and use the protected member.

That's all the information you needed.

Also, why are you building a singleton following the C++ style? There is NO need to reference count it, as the .NET framework will ref count it for you. In fact, you can't even destroy it(Except assigning null to the reference, but that has no garantuees the object will be destroyed). Leave out the ref count, it saves you some hassle

Toolmaker

Share this post


Link to post
Share on other sites
I wouldn't say that using the protected keyword gives the impression that the class should be inherited from, no more than the public keyword.

I initially used protected to stop creation of the class with the new operator, it was an after thought that made me seal the class. As I said I accept that I maybe should have altered the ctor to private, but i still think warning is a little odd.

Edit: I guess I use the c++ style because I am more used to it :)

Share this post


Link to post
Share on other sites
Quote:
Original post by MonkeyChuff
I wouldn't say that using the protected keyword gives the impression that the class should be inherited from, no more than the public keyword.

But that is the only purpose of the proteced keyword: Giving an interface to the derived classes.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I suggest you consult the manual about private and protected.


Your code is odd, not the compiler ;)


It just makes no sense to declare a protected member so the compiler tells you.
Because this indicates you have done something wrong. Either you accidentially sealed the class or you confused the keywords.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement