Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Professional C# 4.0 and .NET 4
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional C# 4.0 and .NET 4
This is the forum to discuss the Wrox book Professional C# 4.0 and .NET 4 by Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner; ISBN: 9780470502259
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C# 4.0 and .NET 4 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
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 24th, 2010, 10:03 AM
Registered User
 
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Why use base class as identifier?

On page 98 of the book in the example about multiple constructors in inheritance there is the following piece of code:

GenericCustomer customer = new Nevermore60Customer("Arabel Jones")

Why would you do this as opposed to:

Nevermore60Customer customer = new Nevermore60Customer("Arabel Jones")

I was much confused by this.
Reply With Quote
  #2 (permalink)  
Old September 24th, 2010, 04:57 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

First of all, the base class is *NOT* being used as an "identifier".

The only identifier in there (either version) is "customer".

As to why declare the variable as the base class: Since I don't have the book, I can't tell for sure, but *probably* because the author(s) are going to demonstrate polymorphism. That is, they are going to have some other code that takes an instance of the base class--not the derived class--so why declare the variable as an instance of the derived class??

Oh, more than likely the code will work either way. That is, if there is a function that takes GenericCustomer as an argument, it would of course take a variable declared as Nevermore60Customer as the argument. But unless you *need* some characteristic of Nevermore60Customer, it really makes no difference.

In "real life" code, you would probably be more likely to see code that looks like this:
Code:
GenericCustomer customer = functionThatMightReturnAnyClassDerivedFromGenericCustomer("Arabel");
So I think the author(s) are just illustrating the point that you can always assign an instance of a derived class to a variable declared to be of the type of the base class.
Reply With Quote
Reply


Thread Tools
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
DataGrid and the WDA Base Class sirmilt BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 3 June 23rd, 2009 06:49 AM
Base Class for Code-Behind Problem groupmatch BOOK: ASP.NET Website Programming Problem-Design-Solution 3 September 8th, 2004 05:06 AM
classes :: base class is inaccessible .... Kaliste C# 3 July 16th, 2004 04:56 AM
base class inheritance krunch97 VB.NET 2002/2003 Basics 6 April 23rd, 2004 11:48 AM
Help with DBObject base class projectedNexus BOOK: ASP.NET Website Programming Problem-Design-Solution 1 August 12th, 2003 06:36 PM



All times are GMT -4. The time now is 05:49 AM.


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