Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Step 27. Understanding Object References

If you are new to Object Oriented Programming (OOP) this Step should interest you.

When I first started programming with objects things didn‘t always go smoothly. Although I had a fair understanding of classes (at least simple ones), often when I tried to use objects made from these classes my program would behave unexpectedly and frequently crashed with "Null Reference Exception (object reference not set to an instance of an object)". I also had difficulty knowing when to use, or not to use, the New keyword. After a while I realised that the cause of my problems was because I did not properly understand how objects are stored in memory. When I learnt more about this my programming improved.

Here’s a simple quiz.
Consider this code snippet involving 2 integer variables x and y:

Code:

Dim x, y As Integer

y = x

x = 3

y = 7

Print(x)

Question: what is printed? Obviously the answer is 3.
Now let’s introduce a simple class Var with an integer Value property:

Code:

Class Var

Public Value As Integer

End Class

Now consider this code snippet involving 2 object variables x and y of type Var

Code:

Dim x, y As Var

x = New Var

y = x

x.Value = 3

y.Value = 7

Print(x.Value)

Again, what is printed? If you answered 3, read on...

The correct answer is 7. The reason is because x and y refer to the same object.
Let’s look at this a bit closer using the Giraffe class from the previous step:

Code:

Class Giraffe

Public Height As Single = 4.3 'Height in metres

Public Spots As Integer = 470 'Number of spots

End Class

We declare a variable of type Giraffe with the statement:

Dim George As Giraffe

We can now assign a new Giraffe object to this variable with:

George = New Giraffe

Alternatively, these two statements could be replaced with:

Dim George As New Giraffe

This declares the variable and assigns the new Giraffe object in one statement.

Whichever way we do it we can say that George now contains a Giraffe object - but this is an illusion! George does not contain a Giraffe object at all - it contain a reference to a Giraffe object.

To better understand this let’s go through this again in greater detail, looking at what happens "beneath the surface".

The statement :

Dim George As Giraffe

creates a variable named George which is initially empty (it contains Nothing).

Code:

__________

George |__________|

When the statement:

George = New Giraffe

executes, the New keyword causes a new Giraffe object to be created, not in the variable, but in another part of the system’s memory known as "The Heap".

Code:

The variable The Giraffe object (in the heap)

__________ ______________

George | 3fa85e02 | 3fa85e02 | 4.3 | 470 |

Height Spots

The address where the new object is stored is placed into the variable, Thus the variable is said to contain a reference to the object, i.e. something which tells the process where the object is actually located. The variable does not contain the object’s data.



In this example I have shown the object at a fictitious address 3fa85e02. We don’t know (or care) what this address actually is - only the process knows. Note that the object is not named George. It is simply "the Giraffe object at 3fa85e02".

If we declare another variable:

Dim Gerald As Giraffe

and then make the assignment:

Gerald = George

we are not copying the object. We are simply copying the object reference in George into Gerald. We now have also:

Code:

__________

Gerald | 3fa85e02 |

which is a reference to the same object. In other words, George and Gerald refer to the same Giraffe! So if we change George’s height and print both heights before and after with:

Code:

Print(George.Height & " " & Gerald.Height)

George.Height = 5.7

Print(George.Height & " " & Gerald.Height)

We print:

4.3 4.3
5.7 5.7

You can see that changing a property of George also changes the same property of Gerald. This confirms the theory.

It’s worth mentioning that the "built-in" data types (Integer, Single, Double, Date, Boolean, etc.) don’t behave like objects constructed from classes as discussed above. For these types the value is actually contained in the variable, hence they are called value types, while objects constructed from classes are called reference types.


Arrays of Objects

Because it is constructed from a .NET-supplied class, an array is itself a reference type of object. Let’s look at again at setting up an array of Giraffe objects (as in Step 26) in greater detail.

The declaration:

Dim Field As Giraffe()

simply creates a variable, named Field, capable of holding a reference to an array of Giraffe objects. Initially this variable is empty (it contains Nothing). All we have is this:

Code:

__________

Field |__________|

Now if we execute the statement:

ReDim Field(3)

the processor creates a 4-element array in the heap and assigns its starting address to the Field variable. Note that each array element is initially empty (contains Nothing):

Code:

The Array variable The Array object (in the heap)

__________ __________

Field | 3fa9b428 | 3fa9b428 |__________| 0

|__________| 1

|__________| 2

|__________| 3

If we load the array with Giraffe objects using the loop:

Code:

For i = 0 To UBound(Field)

Field(i) = New Giraffe

Next

we create 4 new Giraffe objects (also in the heap). The address of each object is assigned to the corresponding array element. We now have:

Code:

The array variable The Array object (in the heap) The Giraffe objects (in the heap)

__________ __________ ______________

Field | 3fa9b428 | 3fa9b428 | 3fb02c40 | 0 3fb02c40 | 4.3 | 470 |

| 3fb02c48 | 1 3fb02c48 | 4.3 | 470 |

| 3fb02c50 | 2 3fb02c50 | 4.3 | 470 |

| 3fb02c58 | 3 3fb02c58 | 4.3 | 470 |

Height Spots

Again each array element contains a reference to a Giraffe object, rather than the actual object.

This gives us a more detailed picture of what actually happens when we set up an array of objects. If, for example, the Giraffe class had another property which was object, an additional set of objects would be created and referenced in the heap. This referencing process could go on forever!

If we assign the array to another variable with:

Dim Paddock As Giraffe() = Field

All we are doing is creating another reference (in Paddock) to the same array. We are not copying the array.

This Step illustrates what objects references are. The main points to remember are:

  • An object variable initially contains Nothing until an object is assigned to it.
  • An object (or more correctly the data associated with an object) is not stored in the object variable. The variable simply contains a link to where the object’s data is.

If you understand this I’m sure you will become better OOPs.


 


Date: 2014-12-21; view: 1211


<== previous page | next page ==>
Processing data from 2 async calls. | Step 28. Placing Multiple Bets with placeBets.
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.006 sec.)