Properties vs. Objects

douglasH
StarLounger
Posts: 69
Joined: 10 Feb 2014, 19:30

Properties vs. Objects

Post by douglasH »

Given that Range.Areas is an object, why is it described as a property in Excel help:

"Range.Areas Property
Returns an Areas collection that represents all the ranges in a multiple-area selection. Read-only."

Appreciate any insight into this. Thanks.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Properties vs. Objects

Post by Rudi »

IMHO, anything that is not an Object is demoted to a property. In other words, you cannot start a line of code with Area. It is not a defined Object like Range or Worksheet etc...
In descriptive terms, properties DESCRIBE an Object whereas Methods are something you DO with Objects.
If you think of the line of code: Selection.Areas.Count > 1, or Selection.Areas(1), then Areas is describing the state of the Selection (Selection of course being the Object).
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Properties vs. Objects

Post by HansV »

The terms "object" and "property" are not mutually exclusive. An object can be a property of something else, for example of another object.
To take an example from Excel: a Worksheet is an object in VBA. It has a property Parent that represents the Workbook it belongs to. A Workbook is an object too.

Strictly speaking, Areas is not an object. It is a collection of Range objects that represent the contiguous areas within the range.
Best wishes,
Hans

douglasH
StarLounger
Posts: 69
Joined: 10 Feb 2014, 19:30

Re: Properties vs. Objects

Post by douglasH »

Thanks, that helped a lot. I guess the way to think of it is that an object isn't directly accessed, but rather that an object is accessed through one of it's properties (or the property of one of it's child-objects such as by using the Parent property.)

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Properties vs. Objects

Post by Rudi »

douglasH wrote:I guess the way to think of it is that an object isn't directly accessed, but rather that an object is accessed through one of it's properties...
I see it more that an Object presents you with its properties (and methods). I cannot get to any properties without first referring to an Object. Anyhow, I think its just different ways we are interpreting it.

Here is a practical (tangible) way to consider Objects vs. Properties...

If "CAR" is an object you can consider the following as properties of the car (things that describe the car)
- CAR.Color = vbRed
- CAR.Weight = 1500kg
- CAR.Length = 3.7m
- etc

Some methods (things you do with the object) connected to the CAR object could be:
- CAR.Drive Speed:=100km/h, CityMode:=False, Economy:=True
- CAR.Park Reverse:=True, ParkAssist:= HellYeah
- CAR.Race

Some "Child-Objects", some of which can be Objects too:
- CAR.Door.Color = vbCyan
- CAR.Door.Window.WindDownState = 0.5
- DOOR.Open =False >> (If no other object uses Door, it does not need to be fully qualified - Use CTRL+Spacebar to get a list)
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

douglasH
StarLounger
Posts: 69
Joined: 10 Feb 2014, 19:30

Re: Properties vs. Objects

Post by douglasH »

I think I mostly understand the difference between objects and properties in that a property is an attribute of an object. As per your example, Color is a property of the object Car. That makes sense to me. What I was trying to figure out was why for example Worksheet.Range was indicated as a property in Excel help and not as an object. Per Excel Help:

"Worksheet.Range Property
Returns a Range object that represents a cell or a range of cells."

I thought it should be:

"Worksheet.Range Object
Returns a Range object that represents a cell or a range of cells."

The conclusion that I've come to is that when we type in Worksheet we're not typing in an actual object, but rather a property that returns the Worksheet object. One of the properties of Worksheet is the Range property. Stringing together Worksheets(1).Range("A1") is a property that returns an object, but, again, is not the object itself. It's just a way to reference or return the object. At least, that's how I've come to think of it.

User avatar
HansV
Administrator
Posts: 78236
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Properties vs. Objects

Post by HansV »

In Worksheets("Sheet1").Range("A1"), Range("A1") is a property of the Worksheet object. The "value" of this property is not a single number or string, but an object of type Range. This object in turn has its own properties, which can be single numbers (such as Height and Width) or strings (such as Text), but can also be yet another object, for example the Font property, which returns an object of type Font, which too has its properties (Bold, Size, ...).
So Range("A1") is at the same time a property of the worksheet it resides on, and an object in its own right.
Best wishes,
Hans

douglasH
StarLounger
Posts: 69
Joined: 10 Feb 2014, 19:30

Re: Properties vs. Objects

Post by douglasH »

I suppose that is conceptually better. One of the properties of the object Car is DoorHandle, which also is an object and can itself have properties that are also objects in their own right.

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Properties vs. Objects

Post by Rudi »

douglasH wrote:I suppose that is conceptually better. One of the properties of the object Car is DoorHandle, which also is an object and can itself have properties that are also objects in their own right.
...and can itself be a property of Door... :grin:
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
Jay Freedman
Microsoft MVP
Posts: 1313
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: Properties vs. Objects

Post by Jay Freedman »

douglasH wrote:I think I mostly understand the difference between objects and properties in that a property is an attribute of an object. As per your example, Color is a property of the object Car. That makes sense to me. What I was trying to figure out was why for example Worksheet.Range was indicated as a property in Excel help and not as an object. Per Excel Help:

"Worksheet.Range Property
Returns a Range object that represents a cell or a range of cells."

I thought it should be:

"Worksheet.Range Object
Returns a Range object that represents a cell or a range of cells."
It's a matter of the "point of view": The parent (Worksheet) considers its child (Range) to be a property, because the only other thing a child could be is a method and it isn't one of those. The child (Range) considers itself to be an object because it has its own children, which may be properties and/or methods.

So Worksheet.Range is a property of a Worksheet object that returns a Range object.

User avatar
Jay Freedman
Microsoft MVP
Posts: 1313
Joined: 24 May 2013, 15:33
Location: Warminster, PA

Re: Properties vs. Objects

Post by Jay Freedman »

If you like a more graphic presentation of objects and properties, use the Watch Window in the VBA editor. Write a sample macro that uses the object of interest in a variable, add a watch for that variable, and set a breakpoint to stop the macro at some point where the variable has been assigned.

Image

In the watch window, anything that has a plus-in-square next to it is an object. Expand that, and you may see that it contains both objects and base data types (numeric, string, etc.).

douglasH
StarLounger
Posts: 69
Joined: 10 Feb 2014, 19:30

Re: Properties vs. Objects

Post by douglasH »

Thanks Jay. I had not used the Watch Window before now, and I think it will come in handy indeed.