Click here to Skip to main content
15,883,853 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
C#
interface IXYZ
{
    string Name { set; }
}

class Logger : IXYZ
{
    public string IXYZ.Name { get; set; }

    private void GenerateAutoName()
    {
        Name = "Name";        // compilation error!
    }
}
Posted

The explanation can be found here[^]. This is the code sample that may help you. Note the comments in the Main method.
C#
// explicit1.cs
interface IDimensions 
{
   float Length();
   float Width();
}

class Box : IDimensions 
{
   float lengthInches;
   float widthInches;

   public Box(float length, float width) 
   {
      lengthInches = length;
      widthInches = width;
   }
   // Explicit interface member implementation: 
   float IDimensions.Length() 
   {
      return lengthInches;
   }
   // Explicit interface member implementation:
   float IDimensions.Width() 
   {
      return widthInches;      
   }

   public static void Main() 
   {
      // Declare a class instance "myBox":
      Box myBox = new Box(30.0f, 20.0f);
      // Declare an interface instance "myDimensions":
      IDimensions myDimensions = (IDimensions) myBox;
      // Print out the dimensions of the box:
      /* The following commented lines would produce compilation 
         errors because they try to access an explicitly implemented
         interface member from a class instance:                   */
      //System.Console.WriteLine("Length: {0}", myBox.Length());
      //System.Console.WriteLine("Width: {0}", myBox.Width());
      /* Print out the dimensions of the box by calling the methods 
         from an instance of the interface:                         */
      System.Console.WriteLine("Length: {0}", myDimensions.Length());
      System.Console.WriteLine("Width: {0}", myDimensions.Width());
   }
}
 
Share this answer
 
Comments
Abhinav S 2-Feb-13 0:27am    
Quite right. 5.
fjdiewornncalwe 2-Feb-13 0:28am    
Thanks, Abhinav
Jibesh 2-Feb-13 4:00am    
good answer. 5
fjdiewornncalwe 2-Feb-13 8:46am    
Thanks, jibesh
Solution is:

C#
interface IXYZ
{
    string Name { set; }
}
class Logger : IXYZ
{
    string IXYZ.Name {  set { ((IXYZ) this).Name = value; }}
    private void GenerateAutoName()
    {
        ((IXYZ) this).Name = "Name";         // compilation error!
    }
}


1)First, if it is explicit interface implementation, then you have to cast that member while setting it, where you were getting compilation error.
((IXYZ)this).Name is correct.
2)Then the next problem is, you should have set property implemented, can not keep it as set; unless class is abstract, so make it as set{}.
3) Interface properies are by default public, remove explicit public modifier of Name.
4) You can not have get; in your class if it is not declared in interface.
5) You will be able to access that property with reference of IXYZ and not with Logger class reference.
Please see below code snippet..

C#
class Test
{
    public void TestMethod()
    {
        IXYZ xyzRef = new Logger();
        xyzRef.Name = "Hello"; //Name accessible
        Logger logRef = new Logger();
       // logRef.Name Not available...
    }
}


Hope it helps. :)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900