Cannot implement an interface member because it is not public

I wrote some VB code that I converted to C # using the Sharp Develop IDE program. I defined an IElement interface that is implemented by objects that return the XML views themselves. Any object that implements this interface should be able to return its tag name and its representation of the XML string. To get an XML string, it may need to loop through a child / sub collection to get an XML representation of all its children.

Classes that inherit from Element can use GetXml and GetNestedXml of their base class or override it, but the GetNestedXml function should not be publicly available, since it will only be called from the public GetXml function of derived classes. Therefore, in the original version of VB, the scope of GetNestedXML was configured to protect. However, Sharp Develop and I have problems trying to convert this code to C #. See the error below.

On the side of the note, I understand that there may be better ways to implement this, and I will be interested in side offers that are easy to flame. :-) Thank you.

 Public Interface IElement ReadOnly Property TagName() As String ReadOnly Property GetXml(Optional ByVal targetXml As Integer = TargetXmlEnum.All) As String Function GetNestedXml() As String End Interface Public Class Element Implements IElement Public ReadOnly Property TagName() As String Implements IElement.TagName Get '.... End Get End Property Public Overridable ReadOnly Property GetXml(Optional ByVal targetXml As Integer = TargetXmlEnum.All) _ As String Implements IElement.GetXml Get '.... End Get End Property Protected Overridable Function GetNestedXml() As String Implements IElement.GetNestedXml '.... End Function End Class 

Converted C #:

 public interface IElement { string TagName { get; } string GetXml { get; } string GetNestedXml(); } public class Element : IElement { public string TagName { get { //... } } public virtual string GetXml { get { //... } } protected virtual string GetNestedXml() { //... } } 

Mistake:

 Error 1 'Smit.SpreadsheetML.Element' does not implement interface member 'Smit.SpreadsheetML.IElement.GetNestedXml()'. 'Smit.SpreadsheetML.Element.GetNestedXml()' cannot implement an interface member because it is not public. D:\Users\Chad\Desktop\SMIT\SMIT.SpreadsheetML.ConvertedToC#\Element.cs 41 24 Smit.SpreadsheetML.Converted 
+7
c #
source share
4 answers

A quick way to solve this problem is to make GetNestedXml publicly available. If you do not want this, you can also declare GetNestedXml a protected abstract method in an abstract base class. But this means that all classes must be extracted from this base class and implement the method. If you want to provide an implementation in a base class, you can also make the method virtual so that derived classes can, but not necessarily, override it. To do this, follow these steps:

  • Create a new abstract base class.
  • Add a secure abstract / virtual implementation of GetNestedXml (). If it's virtual, also provide the body of the method (and you don't need to create abstract classes).
  • Remove this method from the interface.
  • Derive all classes that implement the interface (and want you to have a base implementation of GetNestedXml) from the base class.

Another way to hide this method would be to explicitly implement IElement so that callers only call it when they access the object using the interface.

+1
source share

Because interface implementations must be public or explicit:

change this method

 protected virtual string GetNestedXml() { //... } 

to

 protected virtual string IElement.GetNestedXml() { //... } 

Edit

create an interface like this:

 public interface IElement { string TagName { get; } string GetXml { get; } } 

create an abstract base class like this

 abstract class ElementBase:IElement { public abstract string TagName { get; } public abstract string GetXml { get; } protected abstract string GetNestedXml(); } 

Element class immunity

 public class Element : ElementBase { public override string TagName { get { //... } } public override string GetXml { get { //... } } protected override string GetNestedXml() { //... } } 
+4
source share

The interface declares the responsibilities of all its inheriting instances. Thus, you cannot use a non-public method to implement your interface method.

If it is not publicly available for any reason, I suggest you use an abstract class and use an abstract / virtual method to declare it.

abstract method:

 public interface IElement { string TagName { get; } string GetXml { get; } } public abstract class ElementBase : IElement { public string TagName { get; private set; } public string GetXml { get; private set; } protected abstract string GetNestedXml(); } 

virtual method:

 public interface IElement { string TagName { get; } string GetXml { get; } } public abstract class ElementBase : IElement { public string TagName { get; private set; } public string GetXml { get; private set; } protected virtual string GetNestedXml() { throw new NotImplementedException(); } } 
+3
source share

The fact is that when you declare Element implements IElement , you say: "Hey, I know how to get my embedded XML, and everyone can use it! (Public ...)".

In your class, GetNestedXml is protected, i.e. You do not comply with your declaration.
Even if you are executing an explicit implementation of protected :

  protected override string IElement.GetNestedXml() { //Implementation... } 

Behind the scenes, he will still be public .

0
source share

All Articles