Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 168917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:32:16+00:00 2026-05-11T12:32:16+00:00

My problem here is that I would like to pass an object to a

  • 0

My problem here is that I would like to pass an object to a derived class, but it must be done before the base class constructor, since the base class will immediately call the derived class’s Start() method that uses the object.

Here’s an excerpt from the base class, (renamed from BarcodeScanner for convenience).

public abstract class MyBase {         public MyBase()     {         if (Initialize())             this.Start();     }      public abstract bool Initialize();     public abstract void Start(); } 

Here’s the derived class that I’m creating.

class MyDerived : MyBase {     private string sampleObject;      public MyDerived (string initObject)     {         sampleObject = initObject;     }      public override bool Initialize()      {          return GetDevice();     }     public override void Start()      {          Console.WriteLine('Processing ' + sampleObject.ToString());      } } 

I doubt you can make C# execute a derived constructor before the base constructor; so I’m really just looking for a solution to pass an object to the derived class before the object is used.

I’ve gotten around this by putting the Initialize/Start if block inside the MyDerived constructor. However, there are other classes deriving from the base class; so I ended up having to repeat this block of Initialize/Start code in every derived class. I’d like to see an alternative to modifying the base class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T12:32:17+00:00Added an answer on May 11, 2026 at 12:32 pm

    What you are trying to do is impossible in C#. A constructor in a base class must be run before the constructor of any derived class otherwise there would be potential for corrupt object state. A child object must be able to assume that its base is fully constructed and available.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a problem similar to,but subtly different from, that described here (Loading assemblies
Here's a coding problem for those that like this kind of thing. Let's see
Here's my idea: Start with a simple object: class dynamicObject(object): pass And to be
I've got a problem here with an MSI deployment that I'm working on (using
I'm having a strange problem here... I have an ASP.NET 3.5 application that has
Here is the code that cause me problem since few hours: TabItem newTab =
Here's my problem: I have an unmanaged dll that I made.I'm calling one of
Here is a hum-dinger of a problem that I have not found an answer
Here's my problem: there's an internal issue tracking system that has a nice summary
Here's the core problem: I have a .NET application that is using COM interop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.