Objects as property values

Let's take a closer look at classes and their properties. We will see that the value of a property is often an object.

One of the first classes we worked with was Button. We saw that Buttons have many properties with simple data types. For example:

The data types of the properties listed above are string, boolean and integer, but some of the other properties of Buttons seem different than these. Consider, for example, the Size and Location properties:

The values of the Size and Location properties are not simple data types like integer or string, they are objects. The value of the Location property is an object that is an instance of the class Point and the value of the Size property is an instance of the class Size.

A Point is an object with two properties, X and Y. In this example, the value of the X property is 100 and the value of the Y property is 200.

A Size is an object with two properties, Height and Width. In this example, the value of the Height property is 25 and the value of the Width property is 75.

The value of the Font property of a Button would similarly be an object, an instance of the class Font, which spelled out properties like the Name and Size of the Font.

We have been assigning strings and numbers to variables and properties all along. It turns out that we can assign objects to object variables and properties as well.

Of course, the object we assign to an object variable or an object property must be of the correct class. You could not assign a Font to the Location property or a Point to the Size property.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.