Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2005
This is the forum to discuss the Wrox book Beginning Visual C# 2005 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780764578472
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 5th, 2006, 10:37 AM
Registered User
 
Join Date: Oct 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Quick Instantiation Question

This came to my attention on page 274:

            Animal[] animalArray = new Animal[2];
            Cow myCow1 = new Cow("Deirdre");
            animalArray[0] = myCow1;
            animalArray[1] = new Chicken("Ken");

My question is why do you need to prefix myCow1 with Cow, but you do not need to prefix animalArray[1] with Chicken.

In this line: Cow myCow1 = new Cow("Deirdre");

The first Cow is the type (like int, string, etc.), and the second Cow() is the constructor, correct?

So why does the Chicken not need the type declaration?
 
Old October 6th, 2006, 08:33 AM
Registered User
 
Join Date: Jun 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Dynamik,
You do not need to prefix Animal[1] with Chicken since Chicken is an object derived from the Animal abstract class. This is a very powerful feature that allows you to populate arrays with objects without necessarily knowing their type in advance. Both Cow and Chicken are derived from the Animal abstract class. The Animal[] array can only hold Animal objects or objects derived from them.

Both ways of adding an Animal derived object into an Animal array are valid (yet result in slightly different results).

The example is showing that there are two ways to do that.

1. Adding an already instantiated object (or instantiating the object first, then adding it to the array). This was the case of Cow myCow1 = new Cow("Deirdre"); This statment instantiated a Cow object with the myCow1 as a reference to it.

2. The second method instantiates the Chicken object and adds it to the array in one step " animalArray[1] = new Chicken("Ken"); ". The cost of saving the separate instantiation step is that you do not get a reference to the chicken object (such as myChicken1 for example).


I hope this helps.

Regards,
Madkour






Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick Question! arcadium Java GUI 1 August 5th, 2008 02:16 AM
another quick question andyhague XSLT 2 March 21st, 2006 12:13 PM
Quick Question Ben Horne Forum and Wrox.com Feedback 1 March 22nd, 2004 03:29 PM
Quick If Then Question hcweb Classic ASP Basics 2 March 13th, 2004 02:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.