#42 – Interacting with an Object

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();
Advertisements

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: