Yes, this code is correct. It is identical to the code we used when explaining public fields. You may wonder WHERE the call to the get and set statements is located. Actually, the calls are implied by the fact that we are reading the value in the ‘if’ statement (implied a call to the get method), and the fact that we are assigning it a value in the myCar.ElapsedMileage = 10000000 statement (implies a call to the set method). If we add one more statement:
what value do you think will be displayed? If you said 1 million
you'd be correct. That is because of the way that we wrote our set statement to filter out insane numbers like 10 million. One final point of clarification about the set; consider this line of code that was in the set statement above:
elapsedValue = value;
Where did the “value” identifier come from? In this case, C# uses this token (“value”) to represent the data that the property is being assigned to. It’s similar to an input parameter, but it’s built in and you can’t change the name of the identifier to anything else (like “mySetValue”, for example).
Supplemental Readings for the Express Edition Videos Copyright © 2005 LearnVisualStudio.NET