Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > BOOK: Beginning Visual C# 2005
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 5th, 2006, 10:37 AM
Registered User
Join Date: Oct 2006
Location: , , .
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?
Reply With Quote
  #2 (permalink)  
Old October 6th, 2006, 08:33 AM
Registered User
Join Date: Jun 2006
Location: Amsterdam, Noord-Holland, Netherlands.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts

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.


Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 06:59 AM.

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