Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET 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 Search this Thread Display Modes
  #1 (permalink)  
Old May 16th, 2007, 04:13 AM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default Cascading Constructors

Hi All,
I wish to have a class call the constuctor from the class it inherits in within its own constuctor.
I have understand the constuct
public class myClass():Base(){}
But I wish to use a paramter for the class that is not determined untill the child constructor is executed.
Can anyone advise me of how this is done.

This is an example of what Im after but it doesnot seem to recognise the base class constuctors.

Code:
class Class1
    {
        int _c1id;
        int _field1;
        int _field2;
        public Class1() { }
        public Class1(int c1id) 
        { 
            _c1id = c1id;

            //Get Data From DB
            _field1 = 456;
            _field2 = 789;
        }
    }
    class Class2 : Class1
    {
        int _c2id;
        public Class2(int c2id) 
        {
            _c2id = c2id;
            //Get C1 ID from DB
            _c1id = 1;//FROM DB
            base(_c1id);
        }
    }



======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
Reply With Quote
  #2 (permalink)  
Old May 16th, 2007, 07:26 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You can pass constructor arguments to the super class construct:
Code:
public class SubClass : SuperClass{

   public SubClass(int someInt) : base(someInt){
      //do stuff
   }

}
-Peter
Reply With Quote
  #3 (permalink)  
Old May 16th, 2007, 07:23 PM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Hi Peter,
Thanks for that.
I thought that will execute the base constructor first.
I need the base contructors paramter to be obtained by the subclass.
Its not the same variable.
Code:
public class SubClass : SuperClass{

   public SubClass(int someInt) : base(anotherInt){
      //do stuff
      anotherInt = 31;//Obtained from DB lookup
   }

}
Should I just create a protected method to populate the super class variables?



======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
Reply With Quote
  #4 (permalink)  
Old May 16th, 2007, 10:31 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You've discovered one of the pitfalls of putting too much smarts into a constructor. All super class constructor's are called before the subclass ones.

You would probably be better off making a method outside of the constructor that loads the database info so you can control it more.

-Peter
Reply With Quote
  #5 (permalink)  
Old May 18th, 2007, 03:57 AM
Friend of Wrox
Points: 2,376, Level: 20
Points: 2,376, Level: 20 Points: 2,376, Level: 20 Points: 2,376, Level: 20
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: , , Australia.
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Thanks Peter, I had thought as much but had hoped for some wierd and wonderful work around.
thanks all the same.

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


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
Constructors madhuriguda C# 2 April 29th, 2008 08:11 AM
Constructors - instantiation brysora C# 2005 1 September 3rd, 2007 08:45 PM
Constructors return void? AndrewH Java Basics 4 February 13th, 2007 05:59 AM
COPY CONSTRUCTORS gbilios C++ Programming 4 April 30th, 2004 02:51 AM
Static Constructors Ibn_Aziz C# 3 December 25th, 2003 04:07 AM



All times are GMT -4. The time now is 06:52 PM.


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