Deborah's Developer MindScape

         Tips and Techniques for Web and .NET developers.

August 31, 2009

What Are Objects?

Filed under: C#,OOP,VB.NET @ 3:42 pm

Objects are things. People, companies, employees, time sheets, and ledger
entries are all types of objects. In object-oriented terms, the word object
is used to describe one specific thing, such as Sam Smith the carpenter at
3322 Main Street and the May 15th time sheet for Jessica Jones.

[To begin with an overview of OO, start here.]

Objects have data associated with them called properties. Sam Smith
has a name, occupation, and address. The time sheet has an employee
name, time period, and hours worked.

NOTE: In object-oriented literature, properties are sometimes called attributes, resources, or even just data.

Objects also do things. The time sheet is filled out, validated, and
submitted for payment. In real life, we fill out time sheets, validate them,
and submit them for payment. In a computer system, the time sheet can
perform these operations for itself. The things an object can do are defined
with methods.

NOTE: In object-oriented literature, methods are also called behaviors,
services, operations, or responsibilities.

Objects can be real-world things, such as an employee or time sheet.
Objects can be conceptual things, such as an engineering process or payroll.
Objects can also be implementation-specific things, such as forms,
controls, and DataSets. The same object-oriented concepts apply regardless
of whether the object is based on the real world, on a concept, or on
the implementation.

Since objects are fundamental to understanding object orientation,
it is important that you can recognize objects and define their appropriate
properties and methods. So take a moment to think about the things
around you. What objects do you see? How would you define their properties
and methods?

Your phone has properties such as color, volume, and mute. It has
methods such as increase volume, decrease volume, turn on mute, turn off
mute, dial, answer, and transfer. An employee has properties such as name,
address, and occupation. An electronic time sheet has properties such as
employee name, date, and hours. It has methods such as calculate pay.

An object’s property values should not be directly accessed by something outside the object. For example, the time period should not be adjusted except through a get time period method, and the employee name would not be retrieved except through a get employee name method. This concept of hiding the internal data is a part of what is called encapsulation and is a key premise of object-oriented programming.

Every object is of a specific type. For example, the object defined as
Sam Smith the carpenter at 3322 Main Street is an employee object type,
and the May 15th time sheet for Jessica Jones is a time sheet object type.
All the objects of a particular type have the same set of properties and

From a programming perspective, you can define your own object
types using classes. Or you can use the object types provided in the .NET Framework. In either case, you first create an object of the desired type, and then you can set or get the object’s properties and call its methods.

The following code creates an object using the .NET Framework
Timer object type. It then sets the object’s Interval property to define
how often the timer goes off and executes its Start method to start the

In C#:

Timer myTimer = new Timer();
myTimer.Interval = 1000;

In VB:

Dim myTimer As New Timer
myTimer.Interval = 1000

The new keyword in the first line of code creates a new Timer object
and assigns a reference to that new object to the myTimer object variable.
To access a property or method of the new Timer object, use the object
variable and a period (.) and then the name of the property or method.

NOTE: When you type the object variable name and a period in Visual Studio,
you see a list of the properties, methods, and events that are appropriate for that object. This demonstrates the List Members feature of Intellisense. Intellisense provides auto-completion and display of class documentation within Visual Studio as you are coding.

Everything in .NET can be accessed as an object: forms, strings,
even integers. Check out this code:

In C#:

int i = new int();
string s;
i = 5;
s = i.ToString();

In VB:

Dim i As New Integer
Dim s As String
i = 5
s = i.ToString

The first line creates a new Integer object and assigns it to i. The variable
i is then assigned a value of 5. The last line calls the ToString method of
the Integer object to convert the number to a string.

NOTE: Even though you can use the new keyword to create integers, it is not
necessary and is not commonly done in practice. It is shown in this example to illustrate that you can work with simple data types as objects.

In .NET, an integer and other primitive data types, such as Boolean
and decimal, are called value types. Value types store and pass their contents
by their actual value. Technically speaking, value types are allocated
either on the stack or inline in a structure. A standard set of value types
are built into the .NET Framework, such as integer and decimal. You can
also create your own value types. You do not need to use the new keyword
for any value type.

More complex data types in .NET, such as strings and arrays, are
reference types. Reference types store and pass their contents as a reference to the value’s memory location. Technically speaking, reference types
are allocated on the memory heap. The .NET Framework provides a set of
built-in reference types, such as string and array. Classes that you create are reference types.

.NET automatically creates a boxed value type for each value type when necessary. Boxed value types allow value types to be accessed like reference types (such as the preceding integer example). In addition, boxed value types
allow you to convert value types to reference types. For example, adding
to the preceding code example, you could assign the integer to an object.
This converts the integer value type to an object reference type.

In C#:

object o;
o = i;

In VB:

Dim o As Object
o = i

This conversion of value types to reference types and vice versa is
called boxing. Boxing has a performance hit, so you want to minimize the
amount of boxing in your application wherever possible.

Variables that are value types each have their own copy of the data.
Therefore, operations on one variable do not affect other variables.
Variables that are reference types, such as object variables, can refer to the
same object. Therefore, operations on one variable can affect the same
object referenced by another variable. Check this out:

In C#:

Timer myTimer = new Timer();
myTimer.Interval = 1000;

Timer myTimer2;
myTimer2 = myTimer;
myTimer2.Interval = 500;

Debug.WriteLine(myTimer.Interval); // Displays 500
Debug.WriteLine(myTimer2.Interval); // Displays 500

In VB:

Dim myTimer As New Timer
myTimer.Interval = 1000

Dim myTimer2 As Timer
myTimer2 = myTimer
myTimer2.Interval = 500

Debug.WriteLine(myTimer.Interval) ‘ Displays 500
Debug.WriteLine(myTimer2.Interval) ‘ Displays 500

This code creates a new Timer object, assigns it to the myTimer object
variable, and sets its Interval property to 1000. It then defines a second
myTimer2 object variable, assigns it to the same Timer object, and sets the
Interval property to 500.

Since myTimer and myTimer2 are object variables, they are reference
types. Assigning one object variable to another object variable sets
both variables to reference the same object. Changing the properties
using either object variable makes the changes to the underlying object.
Hence, the sample code results in 500 for both myTimer.Interval and

The one exception to this behavior is strings. Even though strings are
a reference type, assigning one string to another string copies the data so
that both strings do not reference the same data. This provides a more
natural use of strings in your application. For example:

In C#:

string employeeName ;
employeeName = "Jessica Jones";

string employeeName2;
employeeName2 = employeeName;
employeeName2 = "Sam Smith";

Debug.WriteLine(employeeName); // Displays "Jessica Jones"
Debug.WriteLine(employeeName2); // Displays "Sam Smith"

In VB:

Dim employeeName As String
employeeName = "Jessica Jones"

Dim employeeName2 As String
employeeName2 = employeeName
employeeName2 = "Sam Smith"

Debug.WriteLine(employeeName) ‘ Displays "Jessica Jones"
Debug.WriteLine(employeeName2) ‘ Displays "Sam Smith"

Even though employeeName2 is assigned to employeeName and then
employeeName2 is changed, employeeName remains unchanged. The
sample code results in the display of Jessica Jones and then Sam

Notice also that the New keyword is not required for strings. Strings
were designed as a special case to make it easier and more natural to work with string variables.

Understanding how to create an object and call its properties and
methods is crucial to any development with .NET.

(Based on an except from "Doing Objects in Visual Basic 2005".)


1 Comment

  1.   Joan — September 19, 2011 @ 10:56 am    Reply

    Yeah that’s what I’m taknlig about baby–nice work!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2014 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs