Working with objects
71Members: properties, methods, and events
be better if you thought of this as a data structure, which is a more generic term than database. This data structure has some visible features associated with it. There are six columns, for example, and five rows (the first row, which contains column names, doesn’t count). Each row contains data for each of the six columns.
We could’ve put this information into lots of other data structures. For example, we could’ve put it into a SQL Server database, which would look visually similar, although it’d be physically quite different if you looked at how the data was saved to disk. The point is that they’re both data structures. It’s true that they use different names. For example, what Excel calls a column could be called either a column or a domain in SQL Server; what Excel refers to as a row would be called a row, a tuple, or an entity in SQL Server. Excel has a sheet, whereas SQL Server would have a table. The names are only names, and they don’t affect what’s being stored in those structures.
Objects, it turns out, are another kind of data structure. As with Excel or SQL
Server, you don’t ever get to see how objects physically store their data, nor do you need to. You only need to know that objects are a way of storing data, usually in mem- ory, so that you can work with the data. Objects use a slightly different terminology than Excel or SQL Server:
■ Excel stores a bunch of things on sheets, and SQL Server stores them in tables.
In object lingo, a bunch of things is called a collection.
■ Excel uses a row to represent a single thing, such as a user in our example. In object-speak, a row is an object.
■ In Excel and SQL Server, you have columns to store the individual bits of data
about a thing. In the world of objects, they’re called properties.
Try to mentally visualize objects as looking like an Excel spreadsheet, with some minor changes in terminology. Instead of a sheet containing rows and columns, you have a col- lection consisting of objects, which have properties.
At this point, most object tutorials will indulge in a noncomputer, real-world anal- ogy, and we’re obligated by tradition to do the same. Let’s say you wander onto a used car lot—you’re standing in the middle of a collection of car objects. It’s worth nothing that objects come in many different types. For example, a car object would look entirely different from a television object. All of those car objects have various properties, which describe the objects: color, number of doors, type of engine, and so forth. Some of these properties you can change, such as the color, and some you can’t, such as the manufacturer. Thus some properties you can read and write and some are read-only. All this will become more relevant when you start working with PowerShell objects. But for now you can imagine making a spreadsheet, with columns for the color, doors, and so on, and with each row representing a single car on the lot.
7.2
Members: properties, methods, and events
It’s obvious that objects have a lot of things associated with them, and this is where the spreadsheet analogy will start to break down, so we’ll stick with cars. And maybe televi- sions, because who doesn’t like a nice TV show now and again?
Consider some of the things you might use to describe a television or a car; these are the properties of the objects. Obviously, each different type of object will have a dif- ferent set of properties, so one of the things you’ll always want to keep in mind is the type name of the object you’re working with. Table 7.1 provides some examples.
Both of these types of objects can perform various actions, which in the world of objects are called methods. Specifically, a method is something that you can tell an object to do or have done to it. Thinking in terms of cars and televisions, look at table 7.2 for some examples.
In the world of Windows, consider a service. What kinds of actions can a service take? You can stop them, start them, pause them (sometimes), resume them (from pause), and so on. Therefore, if you were looking at an object of the type Service, you might expect to find methods named Start, Stop, Pause, Resume, and so on.
Collectively, the properties and methods of an object are referred to as its members, as if the object type is some kind of exclusive country club and the properties and methods belong to it.
There’s one other type of member, called events. You don’t work with events in PowerShell a whole lot, but you’ll see them, so we want you to know what they’re for.
NOTE Their lack of use isn’t a PowerShell deficiency, because you’ll find many good cmdlets for working with WMI-, .NET-, and PowerShell-related events. We’ll cover these more in later chapters. Based on our experiences,
Table 7.1 Example properties for a car type and for a television type
TypeName: Car TypeName: Television
Manufacturer Manufacturer
Model Model
Color Size
EngineType Resolution
Length CurrentChannel
Table 7.2 Example methods for a car type and for a television type
TypeName: Car TypeName: Television
Turn ChangeChannel
Accelerate PowerOn
Brake PowerOff
DeployAirbags RaiseVolume
73