Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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 January 11th, 2005, 01:08 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default Calling base constuctor with alternative argument

Hi All,

I have been looking for some examples of how to call a a classes base constructor using a different argument than the derived class.

I have noted from the examples I have found that when calling the base class like so:
Code:
public MyDerivedClass(int myInt):base(myInt){}
The base class constructor is executed first.

I wish to use a SQLDatareader from the derived class constructor in the base class constructor.
Something like this:
Code:
public MyDerivedClass(int myInt)
{
//Call a method that selects returns a datareader containing the fields for the objects properties 
SLQDataReader reader = this.Select();
if(reader.read())
{
//Set a few variables here
this.MyVariable = reader[""];

//Then I want to load the base class properties with fields from this reader.
base(reader);

}
 }
But I get an error:
"Use of keyword base is not valid in this context."

So my question is: How do I call the base constructor explicitly???

Many thanks for taking the time to read this.




======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old January 11th, 2005, 11:13 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As far as I'm aware the only way to do what you want is to make your derived class have a private constructor. You would then create the object using a public static method.

Something like...
Code:
public class MyDerivedClass : MyBaseClass
{
    private MyDerivedClass(SqlDataReader reader) : base(reader)
    {
        // do your initialization
    }

    public static MyDerivedClass Create(int myInt)
    {  
        SqlDataReader reader = this.Select();

        // do some work

        // return a newly created object
        return new MyDerivedClass(reader);
    }
}

Kep.
 
Old January 12th, 2005, 08:21 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks Kep,
That is just the solution I was after.
I had not though of a private constructor at all.

Although I now realise that my need for such an implementation is due to a design floor.
I really needed to make the the primay field of the base class protected, then I could set it, and call the base classes Select() method directly.

Thanks again

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================





Similar Threads
Thread Thread Starter Forum Replies Last Post
Alternative to Codeplot? pfloyd BOOK: Beginning XML, 4th Ed ISBN: 978-0-470-11487-2 21 June 5th, 2011 03:57 AM
Alternative to BOOKMARKS Shiamak Word VBA 2 October 31st, 2008 04:04 PM
Image Alternative TSEROOGY Javascript 5 December 6th, 2004 10:39 AM
Calling base class assignment function gstroud Visual C++ 8 September 23rd, 2004 02:33 AM
alternative to MemCpy sanjaykabra82 VS.NET 2002/2003 1 September 17th, 2004 12:10 AM





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