|
-
August 13th, 2008, 01:12 PM
#1
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!
-
August 13th, 2008, 01:45 PM
#2
Re: Readonly Objects returned from methods?
 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!
-
August 13th, 2008, 01:56 PM
#3
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).
-
August 13th, 2008, 02:25 PM
#4
Re: Readonly Objects returned from methods?
 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?
-
August 13th, 2008, 02:51 PM
#5
Re: Readonly Objects returned from methods?
 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!
-
August 13th, 2008, 02:56 PM
#6
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.
-
August 13th, 2008, 03:03 PM
#7
Re: Readonly Objects returned from methods?
 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!
-
August 13th, 2008, 03:37 PM
#8
Re: Readonly Objects returned from methods?
 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() )
-
August 13th, 2008, 04:01 PM
#9
Re: Readonly Objects returned from methods?
 Originally Posted by eclipsed4utoo
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|