Sample Program Using an Array Object


Let's write a short program that uses an array object. The form we want to use is shown in Figure 14.11.

Figure 14.11. Form for the array object program.

graphics/14fig11.jpg

The txtList text box has its Multiline property set to True and its Scrollbars property set to Vertical . The three buttons are named btnCalc , btnShow , and btnExit . btnShow has its Visible property set to False , because we don't want it available to the user when the program first starts running.

The first four lines of the program are shown in the following code fragment:

 Public Class frmArrayObjects   Inherits System.Windows.Forms.Form  Dim MyList As Integer() = New Integer(10) {}  Dim MyCopy As Integer() 

The code starts by defining the MyList integer array with module scope. Note that the reference definition and the storage request for the array object are combined into a single statement. This means that MyList will have an rvalue that points to the storage location in memory after Visual Basic .NET finishes processing this statement.

The next statement defines another reference variable named MyCopy , but Visual Basic .NET does not create storage for a new array object. Recall that this means that MyCopy is created as a reference variable that has an lvalue , but its rvalue is set to Nothing .

Now let's look closely at the btnCalc Click event code. This is shown in Listing 14.5.

Listing 14.5 Code for the btnCalc Click Event
 Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As _              System.EventArgs) Handles btnCalc.Click  Dim i As Integer  txtList.Clear()  For i = 0 To 10   MyList(i) = i   ' Just fill in some values  Next i  MyCopy = MyList   ' Magic!  For i = 0 To 10   txtList.Text &= MyCopy(i) & vbCrLf  Next  btnShow.Visible = True End Sub 

First we clear out the txtList text box and then use a For loop to fill in some values for the MyList() array. Each element simply receives the value of i as we progress through the loop.

The next statement assigns MyList into MyCopy . What does this assignment statement actually do? When you think about it, assignment statements move the rvalue of one operand of the assignment operator into the rvalue of the other operand. That is, assignment operations are expressions that resolve to rvalue-into-rvalue for its operands. (Honestly think about this before you read further!)

Suppose that MyList is stored at memory location 70,000 and the array object is stored at 80,000. Let's further assume that MyCopy is stored at memory address 90,000. The image of these two reference variables prior to the assignment statement is shown in Figure 14.12.

Figure 14.12. Memory image for MyList and MyCopy .

graphics/14fig12.gif

Notice that the rvalue of MyCopy is Nothing because we didn't create a new object for it.

Now consider the assignment statement of MyList into MyCopy . Because the assignment expression copies the rvalue of MyList into the rvalue of MyCopy , the assignment statement:

 MyCopy = MyList    ' Magic! 

causes the rvalue for MyCopy to be changed from Nothing to 80,000.

Think about what this assignment means. Both reference variables now refer to the same object in memory! Therefore, anything we do to MyCopy is actually impacting MyList . The rest of the code in the procedure simply displays the MyList array values and makes the Show button visible. The state of the program at this point is shown in Figure 14.13.

Figure 14.13. Output of ArrayObjects program before pressing the Show button.

graphics/14fig13.jpg

The purpose of the Show button is to illustrate that MyCopy and MyList actually reference the same array object in memory. The code for the Show button in Listing 14.6 proves this fact.

Listing 14.6 The Code for the btnShow Click Event
 Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As _              System.EventArgs) Handles btnShow.Click  Dim i As Integer  MyCopy(5) = 52443  ' Set a new value for copy  txtList.Clear()   ' Clear the old data  For i = 0 To 10   ' Now show ORIGINAL list!   txtList.Text &= MyList(i) & vbCrLf  Next End Sub 

After defining the For loop variable i , the procedure assigns a number into an element of the MyCopy array object. Note that we have done nothing with the original MyList data. The old data in the txtList text box is cleared out by a call to the Clear() method. (The data being cleared out is the original MyList data.)

Now, pay attention! The For loop redisplays the MyList data. Note we're showing the MyList data; not the MyCopy data. Figure 14.14 shows the output of the program after this For loop finishes.

Figure 14.14. Program output after pressing the Show button.

graphics/14fig14.jpg

Notice how the output for MyList(5) is changed even though the assignment statement for 52443 was performed on MyCopy(5) . The reason this works this way is because both reference variables point to the same area in memory for the array object. Therefore, MyCopy points to the MyList array object, not a copy of it. While MyCopy and MyList may have different names , they both reference the same object in memory. This is why the change to MyCopy changes the value in MyList .

Reference variables are extremely useful things. For example, suppose that we want a function to manipulate the data in an array. Further assume that the array has several hundred thousand elements in it. Which is more efficient: copying several hundred thousand values onto the stack and calling the function, or copying a single 4-byte reference variable onto the stack and calling the function? The choice is a no-brainer.



Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net