Click to See Complete Forum and Search --> : Readonly Objects returned from methods?
Killersheep2
August 13th, 2008, 01:12 PM
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!
messycan
August 13th, 2008, 01:45 PM
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.
MadHatter
August 13th, 2008, 01:56 PM
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).
eclipsed4utoo
August 13th, 2008, 02:25 PM
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 (http://msdn.microsoft.com/en-us/library/e6w8fe1b%28VS.80%29.aspx) or readonly (http://msdn.microsoft.com/en-us/library/acdd6hb7%28VS.80%29.aspx) wouldn't work here?
Killersheep2
August 13th, 2008, 02:51 PM
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!
darwen
August 13th, 2008, 02:56 PM
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.
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.
messycan
August 13th, 2008, 03:03 PM
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!
public class Person
{
private Person p;
public Person P
{
get { return p; }
}
}
That should do it. It will allow read-only access to it.
Killersheep2
August 13th, 2008, 03:37 PM
...
Thanks! I think about this immutable object. Sounds good to me. The wrapper-class sounds good too. Generic.List implements such a wrapper (.AsReadOnly() )
MadHatter
August 13th, 2008, 04:01 PM
using const (http://msdn.microsoft.com/en-us/library/e6w8fe1b%28VS.80%29.aspx) or readonly (http://msdn.microsoft.com/en-us/library/acdd6hb7%28VS.80%29.aspx) 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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.