locked
Explicit and Implicit Property Accessors Implementation RRS feed

  • Question

  • Is it possible to implement two accessors of one Interface property in different ways: one explicitly and one implicitly? E.g.:

    public interface MyInterface
        {
            int MyProperty
            {
                get;
                set;
            }
        }
    
        public class MyClass : MyInterface
        {
            private int? _myField = null;
    
            public int MyProperty
            {
                set
                {
                    _myField = value;
                }
            }
    
            int MyInterface.MyProperty
            {
                get
                {
                    return _myField ?? 0;
                }
                
            }
        }


    The setter here is implicit, while the getter is explicit. This code, of course, does not build. Are the any possible modifications/tricks to make that work?

    Surely, that's not a problem to add an explicit setter which would call implicit one, but I want more elegant solution, if possible.


    Software Developer at Coherent Solutions
    • Edited by Michael Sagalovich Friday, October 30, 2009 11:54 AM formatiing died, reposted code
    Friday, October 30, 2009 11:44 AM

Answers

  • You'd have to use two interfaces... one that allows for get and one that allows for set. You can either implement both interfaces at the class level, or inherit one from the other and use the "new" keyword to hide the underlying value.  The overall class would look like this:

    namespace ConsoleApplication9
    {
        public interface ISetterInterface
        {
            int Value { set; }
        }

        public interface IGetterInterface : ISetterInterface
        {
            new int Value { get; }
        }

        public class SplitBrainSetterAndGetter : IGetterInterface
        {
            private int _value;
     
            int IGetterInterface.Value
            {
                get { return _value; }
            }

            public int Value
            {
                set { _value = value; }
            }        
        }
    }


    Or this:

    namespace ConsoleApplication9
    {
        public interface ISetterInterface
        {
            int Value { set; }
        }

        public interface IGetterInterface
        {
            int Value { get; }
        }

        public class SplitBrainSetterAndGetter : IGetterInterface, ISetterInterface
        {
            private int _value;
     
            int IGetterInterface.Value
            {
                get { return _value; }
            }

            public int Value
            {
                set { _value = value; }
            }        
        }
    }

    Or you could do it with one interface, only implementing the side you want to explicitly implement.  eg:

    namespace ConsoleApplication9
    {
        public interface IGetterInterface
        {
            int Value { get; }
        }

        public class SplitBrainSetterAndGetter : IGetterInterface
        {
            private int _value;
     
            int IGetterInterface.Value
            {
                get { return _value; }
            }

            public int Value
            {
                set { _value = value; }
            }        
        }
    }


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Marked as answer by eryang Thursday, November 5, 2009 9:04 AM
    Friday, October 30, 2009 2:06 PM

All replies

  • This compiles. 



        public interface MyInterface
        {
            int MyProperty
            {
                get;
                set;
            }
        }

        public class MyClass : MyInterface
        {
            private int _MyProperty;
            public int MyProperty
            {
                get
                {
                    return _MyProperty;
                }
                set
                {
                    _MyProperty = value;
                }
            }


            #region MyInterface Members

            int MyInterface.MyProperty
            {
                get
                {
                    return _MyProperty;
                }
                set
                {
                    _MyProperty = value;
                }
            }

            #endregion
        }


    Your code has several syntax errors, including the nullable declaration. 
    What are you really trying to do?

    Rudy   =8^D

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, October 30, 2009 1:18 PM
  • Your code has several syntax errors, including the nullable declaration.

    Indeed? Everything compiles at my side right after I add needed explicit setter
    What are you really trying to do?

    I want to have an explicit getter, as the class that is implementing my interface can have its own getter for the same property, which differs from the needed one, but I want to have only one setter - an implicit one, as I do not want to either repeat the code or call an implicit setter from the explicit one. It is not really a problem, just a little trifle that can be easily bypassed, still I wonder whether I really need to bypass it or there is something ready for me. The logic behind my assumption that getter and setter can be one implicit and one explicit is that actually these are methods, and usually it does not matter for one method whether an other is explicit or not. Thus, I expected the same when dealing with getters and setters.
    Software Developer at Coherent Solutions
    Friday, October 30, 2009 1:53 PM
  • You'd have to use two interfaces... one that allows for get and one that allows for set. You can either implement both interfaces at the class level, or inherit one from the other and use the "new" keyword to hide the underlying value.  The overall class would look like this:

    namespace ConsoleApplication9
    {
        public interface ISetterInterface
        {
            int Value { set; }
        }

        public interface IGetterInterface : ISetterInterface
        {
            new int Value { get; }
        }

        public class SplitBrainSetterAndGetter : IGetterInterface
        {
            private int _value;
     
            int IGetterInterface.Value
            {
                get { return _value; }
            }

            public int Value
            {
                set { _value = value; }
            }        
        }
    }


    Or this:

    namespace ConsoleApplication9
    {
        public interface ISetterInterface
        {
            int Value { set; }
        }

        public interface IGetterInterface
        {
            int Value { get; }
        }

        public class SplitBrainSetterAndGetter : IGetterInterface, ISetterInterface
        {
            private int _value;
     
            int IGetterInterface.Value
            {
                get { return _value; }
            }

            public int Value
            {
                set { _value = value; }
            }        
        }
    }

    Or you could do it with one interface, only implementing the side you want to explicitly implement.  eg:

    namespace ConsoleApplication9
    {
        public interface IGetterInterface
        {
            int Value { get; }
        }

        public class SplitBrainSetterAndGetter : IGetterInterface
        {
            private int _value;
     
            int IGetterInterface.Value
            {
                get { return _value; }
            }

            public int Value
            {
                set { _value = value; }
            }        
        }
    }


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Marked as answer by eryang Thursday, November 5, 2009 9:04 AM
    Friday, October 30, 2009 2:06 PM
  • Is it possible to implement two accessors of one Interface property in different ways: one explicitly and one implicitly? E.g.:
    The setter here is implicit, while the getter is explicit.

    This code, of course, does not build . Are the any possible modifications/tricks to make that work?

    Surely, that's not a problem to add an explicit setter which would call implicit one, but I want more elegant solution, if possible.


    Software Developer at Coherent Solutions


    Fooled me.  You have/had a nullabe int? property using a regular int as the backing field.

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, October 30, 2009 2:15 PM