How can I create the "Write Once Read Many" property in VB.NET?

What is the best way to make a property of the "Write Once, Read Many" class such that you can only set the property once?

I know that I can pass all the properties in the constructor and make them ReadOnly, but in cases with a lot of properties, I do not need a constructor with 20 + arguments.

In addition, I understand that I can "collapse my own" setters, but to do this for each property seems to be a coding excess.

Is there a clean way to do this in VB 2008.NET 3.5?

+7
set properties readonly
source share
3 answers

A Write Once A property is never "clean."

I would recommend creating a builder / factory class to avoid 20 param CTor. (Yes, I know this is quite a bit typing)

A similar discussion here: Should I use the given values ​​of the variables?

[edit] In addition, even if you insist that I do not see any other option, except for videos of your own setters, which also prints a lot.

+2
source share

I know it has been almost 3 years, but here is my solution, which I think is better:

public class Site { private int miID; public Site(int iNewID, string sName) { miID = iNewID; Name = sName; } // The ID property can only be set once in the constructor public int ID { get { return miID; } } public string Name { get; set; } } 
+2
source share

The β€œcleanest” way is to not do this at all and use auto properties. I do not see the need for this. Is it really that important that they can only be written once? If so, I would definitely go with a constructor that takes values ​​for all properties as parameters.

0
source share

All Articles