Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a property "Write Once Read Many" in VB.NET?

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

I know that I could pass all the properties in the constructor and make them ReadOnly, but in cases with a lot of properties I don't want a constructor that has 20+ arguments.

Also, I realize I can "roll my own" setters, but having to do that for every property seems like a bunch of redundant coding.

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

like image 256
Casey Wilkins Avatar asked Jan 12 '10 15:01

Casey Wilkins


1 Answers

A Write Once Property is never "clean".

I'd recommend to create a builder/factory class to avoid the 20 param CTor. (Yes, I know it is quite some typing)

Similar discussion here: Should I use set once variables?

[edit] Furthermore, even if you insist I don't see another option than rolling your own setters, which is a lot of typing, too.

like image 136
peterchen Avatar answered Sep 28 '22 04:09

peterchen