Bill
Bill

Reputation: 123

c# implementing interfaces on properties

There is some VB code that I'm working off of. I'm rewriting some things from the original VB code, in c#

inside of my VB class:

    Implements actionBaseClass

    Private m_config_url As String
    Private m_config_unc As String
    Private m_connection_string As String
    Private m_xml_doc_path_or_data As String
    Private m_fpid As String
    Private m_cancelled As Boolean
    Private m_xml_doc_element As Xml.XmlElement
    Private m_removing As Boolean
    Private m_xml_doc As New Xml.XmlDocument


    Property XMLDocPathOrData() As String Implements actionBaseClass.XMLDocPathOrData
        Get
            XMLDocPathOrData = m_xml_doc_path_or_data
        End Get
        Set(ByVal Value As String)
            m_xml_doc_path_or_data = Value
        End Set
    End Property

    WriteOnly Property FPId() As String Implements actionBaseClass.FPId
        Set(ByVal Value As String)
            m_fpid = Value
        End Set
    End Property

my converted C# Interface:

interface actionBaseClass
{

    public bool UseStringData { get; }
    string XMLDocPathOrData { get; set; }
    string FPId { set; }
    string ConfigURL { set; }
    string ConfigUNC { set; }
    string ConnectionString { set; }
    bool Cancelled { get; }
    void ActionMe();
    void DisposeMe();

}

my converted C# class so far:

 public partial class Form1 : Form, actionBaseClass
    {


        #region Public Implements from base class

        private string m_config_url, m_config_unc, m_connection_string, m_xml_doc_path_or_data, m_fpid;
        private bool m_cancelled, m_removing;
        private XmlElement m_xml_doc_element;
        private new XmlDocument m_xml_doc;

        public bool UseStringData
        {
            get { return false; }
        }


        public string XMLDocPathOrData

        {
            get { return actionInterface.XMLDocPathOrData = m_xml_doc_path_or_data; }
            set { m_xml_doc_path_or_data = value; }
        }

        public string FPId
        {
            set { m_fpid = value; }
        }

How do you use Implements in the property declaration like in the above VB code, in C#? Or do you not have to in C# after using the Implements <interface_name>

Upvotes: 1

Views: 1717

Answers (2)

StriplingWarrior
StriplingWarrior

Reputation: 156708

If you're already implementing the interface with your class, then a property with the same signature will automatically implicitly implement the interface. If you want to explicitly implement the interface, you can do that too.

    bool actionBaseClass.UseStringData {
        get { 
            Console.WriteLine("actionBaseClass.UseStringData was called");
            return this.UseStringData; // call through to the other declared property
        }
    }

Upvotes: 1

Francesc Castells
Francesc Castells

Reputation: 2857

You don't have to specify an Implements keyword in C# for each property and method that implements something from an interface. As long as the signature matches, the compiler knows that that property or method is an implementation from the interface.

Two more notes on your code:

  1. The convention is that interface names start with a capital I, like IAction
  2. Use PascalCase for class and interface names
  3. If it's an interface, is doesn't make sense that it's called "BaseClass"
  4. You don't have to specify public in properties and methos in an interface (see UseStringData)

Upvotes: 2

Related Questions