Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Beginning Visual C# 2010
|
BOOK: Beginning Visual C# 2010
This is the forum to discuss the Wrox book Beginning Visual C# 2010 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, ; ISBN: 9780470502266
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2010 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 August 26th, 2011, 12:04 PM
Registered User
 
Join Date: Aug 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default p248: Calling a private constructor using a derived class

At the bottom of page 248, there is code for a default constructor, which is private. However, it is appended with

Code:
 : this("Default Name")
because it is stated that the constructor could be called if a new class is derived from this class.

I can't quite understand how this private constructor could in fact be called by a derived class. Could someone post an example of how a class could be derived that would use this constructor?

Thanks.
 
Old August 26th, 2011, 03:28 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

Hi,

A private constructor cannot be called from a derived class, only from within the class itself (e.g. a static member). A protected constructor can be called from a derived class.

Hope this helps.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
 
Old August 31st, 2011, 10:50 AM
Registered User
 
Join Date: Aug 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Christian.

Does this mean that the 'How It Works' at the bottom of that page (248) is wrong?
 
Old September 9th, 2011, 05:10 PM
Registered User
 
Join Date: Jan 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

that code is not related to derived class! christian's answer is related to derived class but the question is not!
in 248 page of beginning visual c# 2010 is a code that related to override constructor in one class!
dude, when we have more than one constructor in one class, can call another constructor by :this([argument of another constructor])
like this code:
Code:
class test
{
private int A;
public test(int a)
{
this.A=a;
}

public test():this(10)
{
}

}
in this code we have a class that have two constructor, first have one argument, 2nd have not any argument but we need that when the programmer create a object of our class A variable have some value, if programmer doesn't set that value,*by calling 2nd constructor) when test() has called, the test(10) is called by this(10).
good luck both!





Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a parent class method from a child class (Zend Cert) crmpicco Beginning PHP 2 July 30th, 2010 04:22 AM
private constructor in C# n.nsivakumar C# 1 August 16th, 2006 07:08 AM
Not a ReportDocument derived class chokk Crystal Reports 0 May 10th, 2006 08:21 AM
calling private method from another class jack_3 C# 3 December 16th, 2005 07:56 PM
Not a ReportDocument derived class error AnotherUser BOOK: Professional Crystal Reports for VS.NET 0 March 9th, 2004 01:41 PM





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