X hits on this document

PDF document

“Beginning Visual C# 2005 Express Edition Video Series” - page 42 / 73

187 views

0 shares

0 downloads

0 comments

42 / 73

Properties

Lets add a public field for a moment just to show how we would reference that class variable in our code:

(This is just a re-cap of the Car class … all we’re doing is adding a Public field)

C#:

public class Car {

public int ElapsedMileage;

}

(This code would be used to create an instance of the Car object and use the new Public field we just added)

C#:

Car myCar = new Car();

if (myCar.ElapsedMileage == 0) {

myCar.ElapsedMileage = 10000000; }

This illustrates two ideas. First, it shows how to use the . (dot) syntax to access public fields and set or retrieve their values. Second, it shows the problem with public fields. You could set their values to nonsensical values. Very few cars have traveled 10 million miles. We would obviously like to limit that to a sane amount. We can do validation (also known as "sanity checks") on values, or filter the value of a field by using a property instead. Lets change the ElapsedMileage from a public field to a private field:

Supplemental Readings for the Express Edition Videos Copyright © 2005 LearnVisualStudio.NET



Document info
Document views187
Page views187
Page last viewedThu Dec 08 14:34:33 UTC 2016
Pages73
Paragraphs821
Words15988

Comments