#42 – Interacting with an Object
July 29, 2010 Leave a comment
Once you create an instance of a reference type using the new keyword, you can interact with the new object through the variable that serves as a reference to that object. You can call the object’s methods or read and write its properties.
// Make a new Person object
Person p1 = new Person("Sean", 46);
// Read some properties
string theName = p1.Name; // Sean
int howOld = p1.Age; // 46
// Set Age property to new value
p1.Age = p1.Age - 10;
int younger = p1.Age; // Now 36
// Call method that takes no parameters, returns description
// Will return: Sean is 36 yrs old.
string describe = p1.Description();