Property that can only be set once...
-
Hello, I have a class with a property that allows the getting and setting of a field. However, once that field has been set, I don't want it to be set again. Is it reasonable to have a get/set property that only allows the property to be set once? Am I trampling on some best practice here or am I completely OK? (In my particular case, it won't work to have the field set by the constructor.) Thanks!
-
Hello, I have a class with a property that allows the getting and setting of a field. However, once that field has been set, I don't want it to be set again. Is it reasonable to have a get/set property that only allows the property to be set once? Am I trampling on some best practice here or am I completely OK? (In my particular case, it won't work to have the field set by the constructor.) Thanks!
If you are making this available for use elsewhere then it's not good practice to have a single set field. Part of the problem you are going to have is that your field can be got to and set via reflection, so any single-set updates can be bypassed.
Deja View - the feeling that you've seen this post before.
-
Hello, I have a class with a property that allows the getting and setting of a field. However, once that field has been set, I don't want it to be set again. Is it reasonable to have a get/set property that only allows the property to be set once? Am I trampling on some best practice here or am I completely OK? (In my particular case, it won't work to have the field set by the constructor.) Thanks!
I was going to suggest using a read-only variable, but then i read about not being able to use the constructor. Instead, try setting up a private (or protected) variable for whatever it is you want (which you already have), then just use a regular method to set it, but check that it is null, or whatever its default value is first, like this:
private string name;
public void setName(string Name)
{
if(name != null)
name = Name;
else
//name is already set.
}You could just do the same thing using a set method (and then being able to just use
Y = X;
) but it just doesn't seem to be the type of place to put it. Plus, you could have your regular method return a bool so that you know if you have set the variable or not.My current favourite word is: Bauble!
-SK Genius
-
If you are making this available for use elsewhere then it's not good practice to have a single set field. Part of the problem you are going to have is that your field can be got to and set via reflection, so any single-set updates can be bypassed.
Deja View - the feeling that you've seen this post before.
Thanks for your thoughts.
-
I was going to suggest using a read-only variable, but then i read about not being able to use the constructor. Instead, try setting up a private (or protected) variable for whatever it is you want (which you already have), then just use a regular method to set it, but check that it is null, or whatever its default value is first, like this:
private string name;
public void setName(string Name)
{
if(name != null)
name = Name;
else
//name is already set.
}You could just do the same thing using a set method (and then being able to just use
Y = X;
) but it just doesn't seem to be the type of place to put it. Plus, you could have your regular method return a bool so that you know if you have set the variable or not.My current favourite word is: Bauble!
-SK Genius
Thanks for your reply. I'll ponder this some more.
-
If you are making this available for use elsewhere then it's not good practice to have a single set field. Part of the problem you are going to have is that your field can be got to and set via reflection, so any single-set updates can be bypassed.
Deja View - the feeling that you've seen this post before.
Pete O'Hanlon wrote:
your field can be got to and set via reflection
Ummm... what? My privates aren't private?
-
Pete O'Hanlon wrote:
your field can be got to and set via reflection
Ummm... what? My privates aren't private?
PIEBALDconsult wrote:
Ummm... what? My privates aren't private?
Exactly. Using reflection there's not much that can't be done. Accessing otherwise not accessible members via reflection is one of the easier tasks.
Regards, mav -- Black holes are the places where God divided by 0...
-
Pete O'Hanlon wrote:
your field can be got to and set via reflection
Ummm... what? My privates aren't private?
PIEBALDconsult wrote:
My privates aren't private
Ignoring the innuendo that arises here, the answer is no. Reflection allows you to get right at the heart of your code if you know what you are looking for.
Deja View - the feeling that you've seen this post before.
-
I was going to suggest using a read-only variable, but then i read about not being able to use the constructor. Instead, try setting up a private (or protected) variable for whatever it is you want (which you already have), then just use a regular method to set it, but check that it is null, or whatever its default value is first, like this:
private string name;
public void setName(string Name)
{
if(name != null)
name = Name;
else
//name is already set.
}You could just do the same thing using a set method (and then being able to just use
Y = X;
) but it just doesn't seem to be the type of place to put it. Plus, you could have your regular method return a bool so that you know if you have set the variable or not.My current favourite word is: Bauble!
-SK Genius
Unfortunately, this design (while being good) doesn't address the fact that the private member could still be set directly. The poster needs to think some more about his design before he tries this.
Deja View - the feeling that you've seen this post before.