 |
| General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category.
** PLEASE BE SPECIFIC WITH YOUR QUESTION **
When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 16th, 2005, 10:18 AM
|
|
Registered User
|
|
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Setting ReadOnly Property Values
Can anyone tell me if there is an exact method on setting values in an object that you want to be accessed through a readonly property.
I have an object that represent a user of an admin system for example, I want various values like Id and IsDefaultUser etc to be readonly. But I need to return this object with these readonly values set.
They are only readonly so that developers can see that these values are not there to be changed.
At present I have created another object called UserInfo that is passed to the constructor that then sets the values directly. As UserInfo has the same members but are all read/write.
It works but is it the correct way of setting readonly values 
|
|

May 16th, 2005, 10:31 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
So, this is how I do it...
Lets say you have a class, called user and you want to set the properties of the class when you create the object. However, you want all the properties of the object to be read only..
First you create global private variables inside the class.
Second you create properties to return the value of the properties.
Now you have read only properties.. but how do you set them?
You must load the data within the user class (or in a child of it- assuming you break out your data access) and then set the variables directly, rather than using the properties.. since the private variables are accessible from within the object.
I would use my constructor to create an instance of a data class that gets the data and then take the return data and set the private globals-- and then return the object.
Since you have a read write version of the object already- I would also say another option is to inherit the r/w version of the object and override the properties to be read-only.
I only use the properties to set/get when I am dealing with another class/object. I use the local private variables inside the same class.
Does that help at all?
Hal Levy
I am here to help you, not do it for you.
That is, unless you hire me. I am looking for work.
|
|

May 16th, 2005, 11:16 AM
|
|
Registered User
|
|
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the reply, I had thought of access the datasource from within the class, meaning that I would only need to pass in the Id of user when constructing the object.
The only issue I had was that it may be an issue if I was returning a collection of users. Each one would hit the database in turn when created, oppossed to iterating through a data reader which returns the users in one hit.
Although I could just load the relavent data for a user when someone actualy accesses the info they need, oppossed to getting all the data when creating the user to place in a collection.
Thanks for the advice, its helped and has given me some insite into ways around this.
:)
|
|

May 16th, 2005, 02:20 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
well- the design question here comes down to this: Do you need all of the user data in the collection? Often I create a small class (lets call it UserList), that contians only a small subset of the data in read-only format. Instead of creating many user objects and loading that into the UserList - I would create a struct (or a different object) and use that to populate the read only data.
This design comes from Rockford Lhotka - as mentioned in his Expert One-On-One VB.Net (or C#) Business Objects book (Previously a Wrox book, now published by APress). While I certainly think the CSLA.NET framework is something that can only be used in a very controled development environment (Meaning full time development shops-- places that change consultants a lot, it just won't work) the chapter where he goes through the design of his demo program is very helpful.
Hal Levy
I am here to help you, not do it for you.
That is, unless you hire me. I am looking for work.
|
|

May 16th, 2005, 04:34 PM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
|
|
My approach for what it's worth would be this:
- Build your class as you want it tobe consumed (with readonly properties).
- Create constructor(s) that take in the necessary data to set those properties within.
I dissagree with Hal on the approach of inheriting and overriding. In fact, "override" isn't the correct term. You can override a property, but if you plan on changing the signature (visibility, data type, etc) of a property (or any other class member for that matter) you have to "Shadow" the member. I feel this is poor design because it breaks the expected behavior of inheritance.
- Peter
|
|

May 17th, 2005, 02:34 AM
|
|
Registered User
|
|
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hal, so what your saying, if I'm understanding you right, is to hold all the data that would be accessed through a readonly property in a list (based oh the fact I want a collection of users).
On requesting a user, construct it taking the data required from the list and passing it to the constructor meaning that I am not returning to the database for each one as they are requested?
Planoie, from what I understand from Hal I think its the same concept as you mentioned, about passing values to the constructor.
Thanks to both of you for the input.
|
|

May 17th, 2005, 02:31 PM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Quote:
quote:Originally posted by Gavin265
Although I could just load the relavent data for a user when someone actualy accesses the info they need, oppossed to getting all the data when creating the user to place in a collection.
|
what about this
Code:
public enum PersonFields{ID=1,Name=2,Address=4,Phone=8};
public class Person
{
int m_id=0;
string m_name=string.Empty;
string m_address=string.Empty;
string m_phone=string.Empty;
public Person()
{
this.initialize(15);//fetchs all the fields
}
public Person(PersonFields personFields)
{
this.initialize(personFields);
}
private void initialize(PersonFields personFields)
{
int param=Convert.ToInt32(personFields);
if((param&1)==1)
//fetch just Id field
if(((param>>1)&1)==1)
//fetch just Name filed
if(((param>>2)&1)==1)
//fetch just Address filed
if(((param>>3)&1)==1)
//fetch just Phone filed
}
//other Public properties(readonly)
}
then for example
Code:
//fetch Adrres filed and ID field
Code:
Person p1=new Person(PersonFields.Address|PersonFields.ID);
//fetch just ID field
Person p2=new Person(PersonFields.ID);
//fetch all fields
Person p3=new Person();
_____________
Mehdi.
software student.
|
|

May 19th, 2005, 04:54 AM
|
|
Registered User
|
|
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Ok, I can see how that could work aswell, thanks.
But just one thing, I don't write in C# although I understand its systax etc due to my JavaScripting background, but what is the VB equivalant for the paramerters in the brackets in the line:
//fetch Adrres filed and ID field
Person p1=new Person(PersonFields.Address|PersonFields.ID);
Thanks
|
|

May 19th, 2005, 12:49 PM
|
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Dim p1 As New Person(PersonFields.Address Or PersonFields.ID)
|
|
 |