Arrays Of Objects


One important benefit of being able to have references refer to derived class instances is that you can form arrays of object references, where the different objects in the array might be of different types. This is analogous to the situation in Visual Basic where you could form arrays of interface references and not care about the fact that these interface references might be implemented by completely different classes of objects.

In order to see how C# deals with arrays, rewrite the test harness code for the Employee and Manager classes so that it forms an array of object references. You can download the revised code, called EmployeeManagerWithArrays, from the Wrox Press Web site (www.wrox.com). The new code looks like this:

 public MainForm() { InitializeComponent(); Employee Britney = new Employee("Britney Spears", 20000.00M); Employee Elton = new Manager("Elton John", 50000.00M); Manager Ginger = new Manager("Geri Halliwell", 50000.00M, 20000.00M); Employee [] Employees = new Employee[3]; Employees[0] = Britney; Employees[1] = Elton; Employees[2] = Ginger; for (int I=0 ; I<3 ; I++) { this.listBox1.Items.Add(Employees[I].Name); this.listBox1.Items.Add(Employees[I].ToString()); this.listBox1.Items.Add(""); } } 

You simply call up the Name property and the ToString() method of each element of the array. Figure B-4 shows the results of running this code.

image from book
Figure B-4

From Figure B-4 you can see that C# uses square brackets for dealing with arrays. This means that, unlike in Visual Basic, there is no danger of any confusion about whether you're talking about an array or a method or function call. The syntax for declaring an array looks like this:

 Employee [] Employees = new Employee[3]; 

As you can see, you declare an array of variables of a certain type by putting square brackets after the name of the type. An array in C# always counts as a reference object (even if its elements are simple types like int or double) so there are actually two stages: declaring the reference and instantiating the array. To make this clearer you could have split up the previous line of code like this:

 Employee [] Employees; Employees = new Employee[3];  

There is no difference between what you're doing here and how you instantiate objects, except that you are using square brackets to indicate that this is an array. Also note that the size of the array is established when you instantiate the object — the reference itself doesn't contain details of the size of the array — only its dimension. The dimension is specified by commas in the array declaration. For example, if you want to declare a two-dimensional, 3x4 array of doubles, you write this:

 double [,] DoubleArray = new double[3,4]; 

When you have the array, you simply assign values to its elements in the usual way.

Important

Note that one difference between C# and Visual Basic is that arrays in C# always start at element 0.

In Visual Basic you have the option to change this behavior to element 1 using the Option Base statement. You can also specify lower boundaries for any array. But this feature doesn't really add any benefits, and it can impact performance, because it means that whenever you access an element in an array in Visual Basic, the code has to do some extra checking to find out what of the lower bound of that array is for this collection. C# does not support changing the base of an array in this way.

In the previous code, once you have initialized the elements of the array, you just loop through them. If the "strange-looking" syntax of the for loop worries you, hang in there — you'll come back to it shortly.

Note that because the array has been declared as an array of Employee, you can access only those members of each object defined for the Employee class. If you wanted to access the Bonus property of any object in the array, you first would have to cast the corresponding reference to a Manager reference, which would mean checking whether the object is a Manager object. That is not difficult to do but is beyond the scope of this appendix.

Although you are using Employee references, you do always pick up the correct version of ToString(). If the object you're referring to is a Manager object, then, when you call ToString(), the version of ToString() defined in the Manager class is the one that is executed for that object. That is the beauty of overriding methods in C#. You can replace some method in the derived class, and know that no matter through which reference type this object is accessed, you will always run the correct method for that object.




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

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