CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2008
    Posts
    4

    Readonly Objects returned from methods?

    Hi

    let's assume i have the following function

    void Person getPerson(int ID)
    {
    Person p= ....
    return p;
    }

    Is it possible to get the Person as an readonly (in C++ const) object from this function without cloning the Person-object? In C++ i would return a const object and therefore only can read public members or call other const-methods of this object. How could i do this with C# ??

    Thanks for reading!

  2. #2
    Join Date
    Nov 2006
    Posts
    146

    Re: Readonly Objects returned from methods?

    Quote Originally Posted by Killersheep2
    Hi

    let's assume i have the following function

    void Person getPerson(int ID)
    {
    Person p= ....
    return p;
    }

    Is it possible to get the Person as an readonly (in C++ const) object from this function without cloning the Person-object? In C++ i would return a const object and therefore only can read public members or call other const-methods of this object. How could i do this with C# ??

    Thanks for reading!
    You can possibly use Properties.

    private Person p;
    public Person P
    {
    get { return p; }
    }

    Maybe I am not understanding your questions correctly.
    If this post helps you out, please rate it!

  3. #3
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Readonly Objects returned from methods?

    C# doesn't have the concept of const. I guess its debatable whether it really exists in C++ either, but no, there's no such functionality provided in C#.

    for accessors like that properties are the way C# does it (they're method like things that look like public variables. if no set {} block is defined inside the property, then it is readonly, but that doesn't stop someone from changing the value of the object's members).

  4. #4
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Readonly Objects returned from methods?

    Quote Originally Posted by MadHatter
    C# doesn't have the concept of const. I guess its debatable whether it really exists in C++ either, but no, there's no such functionality provided in C#.

    for accessors like that properties are the way C# does it (they're method like things that look like public variables. if no set {} block is defined inside the property, then it is readonly, but that doesn't stop someone from changing the value of the object's members).
    using const or readonly wouldn't work here?

  5. #5
    Join Date
    Jun 2008
    Posts
    4

    Re: Readonly Objects returned from methods?

    Quote Originally Posted by MadHatter
    C# doesn't have the concept of const. I guess its debatable whether it really exists in C++ either, but no, there's no such functionality provided in C#.

    for accessors like that properties are the way C# does it (they're method like things that look like public variables. if no set {} block is defined inside the property, then it is readonly, but that doesn't stop someone from changing the value of the object's members).
    This is exactly the problem! I want to return an object, but dont want the receiver to get writeaccess to the members! If C# doesnt support this, i have to return a copy of my object. Sometimes copying is to slow, because the objects may be large!

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Readonly Objects returned from methods?

    You can either

    (1) Make Person immutable i.e. don't have any public 'set' methods. If you want to change a 'Person' then you create a copy. This is the trick that system.string uses.
    (2) Have a read only interface, IPerson, which Person implements and which only has public get methods. Return this from the method. This isn't a complete solution : outside people could still cast to the concrete implementation but it's better than nothing.
    (3) Have an interface class which has public get methods and return this e.g.

    Code:
    public class Person
    {
        int _numberOfLegs;
    
        public int NumberOfLegs
        {
            get
            {
                return _numberOfLegs;
            }
    
            set
            {
                _numberOfLegs = value;
            }
        }
    } ;
    
    public class ReadonlyPerson
    {
        private Person _person;
    
        public ReadonlyPerson(Person person)
        {
            _person = person;
        }
    
        public int NumberOfLegs
        {
            get
            {
                return _person.NumberOfLegs;
            }
        }
    }
    
    public class ContainingPerson
    {
        private Person _person;
    
        public ReadonlyPerson Person
        {
            get
            {
                return new ReadonlyPerson(_person);
            }
        }
    }
    Create an instance of ReadonlyPerson to return to the outside world.

    Darwen.
    Last edited by darwen; August 13th, 2008 at 03:00 PM.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    Nov 2006
    Posts
    146

    Re: Readonly Objects returned from methods?

    Quote Originally Posted by Killersheep2
    This is exactly the problem! I want to return an object, but dont want the receiver to get writeaccess to the members! If C# doesnt support this, i have to return a copy of my object. Sometimes copying is to slow, because the objects may be large!
    Code:
        public class Person
        {
            private Person p;
            public Person P
            {
                get { return p; }
            }
        }
    That should do it. It will allow read-only access to it.
    If this post helps you out, please rate it!

  8. #8
    Join Date
    Jun 2008
    Posts
    4

    Re: Readonly Objects returned from methods?

    Quote Originally Posted by darwen
    ...
    Thanks! I think about this immutable object. Sounds good to me. The wrapper-class sounds good too. Generic.List implements such a wrapper (.AsReadOnly() )

  9. #9
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: Readonly Objects returned from methods?

    Quote Originally Posted by eclipsed4utoo
    using const or readonly wouldn't work here?
    the C# const keyword is only able to decorate string and value type members of a class. readonly allows a class variable to be accessed but its reference cannot be changed outside of the classes constructor.

    in C++ you can decorate method parameters and return values as const, which isn't directly possible here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured