O'Reilly logo

Microsoft®C# Programming for the absolute beginner by Andy Harris

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a Property in the CritterProp Program

The public instance variable already created for name was easy to make, but it has some serious weaknesses. Generally, you want to write your programs to prevent problems. By making the name public, you have no way to ensure that it will get an appropriate value. Figure 4.11 shows the next version of the Critter program, Critter Prop, featuring a characteristic called the property.

Figure 4.11. You can change the name property so that the critter will reinforce special rules.

Also, you might want a characteristic to be read-only so that it can be changed only from within the class. For example, if you ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required