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

  • SEARCH
  • Home
  • 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 4611642
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:17:43+00:00 2026-05-22T01:17:43+00:00

I have a parent type and there are some children types inheriting from it.

  • 0

I have a parent type and there are some children types inheriting from it.

I want to make sure there is only one instance of the parent and also for all the children types. Parent type:

private static int _instanceCount = 0;

public ParentClass()
{
    protected ParentClass() // constructor
    {
        _instanceCount++;

    if (_instanceCount > 1)
        throw new exception("Only one instance is allowed.");
    }
}

Sample child class:

private static int _instanceCount = 0;

public ChildClass() : ParentClass
{
    public ChildClass() : base() // constructor
    {
        _instanceCount++;

        if (_instanceCount > 1)
            throw new exception("Only one instance is allowed.");
    }
}

The solution works for children types but when they call the base class constructor I cannot distinguish if the base constructor is called from other types or not so the solution fails.

How can I achieve this?

  • 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. Editorial Team
    Editorial Team
    2026-05-22T01:17:44+00:00Added an answer on May 22, 2026 at 1:17 am

    You should be able to tell if you’re being called from a subclass like this:

    if( this.GetType().Equals(typeof(ParentClass)) )
    {
        //we know we're not being called by a sub-class.
    }
    

    Of course, you could just skip the step of incrementing the count in child classes and only do it in the parent as well…and there are threading issues.

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

Sidebar

Related Questions

I have two entities types: RunContainer parent entity type Run child entity type Run
If you have Parent has_many :children Child Is there any reason a foreign key
I'm building an architecture with inheritable generics and parent-children relations. I have one major
Is there some way to have a generic type with a parameterless constructor that
I have an event handler, but I want to make sure that it will
I have a parent pom whose packaging type is jar . Then I have
I have a parent object which has a one to many relationship with an
Aside from keeping some kind of structured mark-up, is there any justifiable reason to
I have a class Parent . I want to define a __new__ for Parent
I have two different classes deriving from one base class. They each have a

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.