X hits on this document

PDF document

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

252 views

0 shares

0 downloads

0 comments

22 / 73

Properties

Let’s 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)

Visual Basic:

Public Class Car

Public ElapsedMileage As Integer

End Class

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

Visual Basic:

Dim myCar As Car

If myCar.ElapsedMileage = 0 Then myCar.ElapsedMileage = 10000000 End If

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. Let’s 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 views252
Page views252
Page last viewedSat Jan 21 14:13:34 UTC 2017
Pages73
Paragraphs821
Words15988

Comments