Click here to Skip to main content
11,714,345 members (88,441 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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 1-Feb-13 18:12pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The explanation can be found here[^]. This is the code sample that may help you. Note the comments in the Main method.
// 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());
   }
}
  Permalink  
Comments
Abhinav S at 2-Feb-13 0:27am
   
Quite right. 5.
Marcus Kramer at 2-Feb-13 0:28am
   
Thanks, Abhinav
jibesh at 2-Feb-13 4:00am
   
good answer. 5
Marcus Kramer at 2-Feb-13 8:46am
   
Thanks, jibesh
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Solution is:

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..

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. Smile | :)
  Permalink  
v3

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 430
1 Maciej Los 229
2 Richard MacCutchan 210
3 CHill60 175
4 OriginalGriff 175
0 Sergey Alexandrovich Kryukov 365
1 CHill60 335
2 OriginalGriff 245
3 Andy Lanng 173
4 Richard MacCutchan 150


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100