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 7408983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:00:19+00:00 2026-05-29T06:00:19+00:00

I just tried to implement Singleton pattern to WinForms ,so that only one instance

  • 0

I just tried to implement Singleton pattern to WinForms ,so that only one instance of a form stays in the Application life ,but i face a difficulty

I want to throw exception if the instance of singleton exists and return same instance reference at the same time.

SingletonForm.cs

public class SingletonForm : BaseFormcs
{
    private static SingletonForm _instance;
    //To stop new keyword from instantiation 
    private SingletonForm()
    { }
    public static SingletonForm GetInstance()
    {
        if (_instance == null)
            return _instance = new SingletonForm();

        else
        {
            throw new Exception("Form already exists"); // execution returns from here
            return _instance; // Warning : Unreachable code detected
            //I also want to return instance reference.
        }

    }
}
  • 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-29T06:00:19+00:00Added an answer on May 29, 2026 at 6:00 am

    You either throw an Exception, or return an instance. In the case of a Singleton, no Exception should be thrown, just return the instance if it exists.

    The Singleton pattern shouldn’t stop you (or even warn you) from calling GetInstance many times. It should just return the same instance that was created the first time around.

    i just thought may be sometime i may need to use both at the same time
    thats why I asked

    Throwing an exception returns immediately from the function, because it means that an unexpected error occurs. In another situation, you might want to throw an exception, but only if some condition is true (e.g. argument validation failed). Otherwise, you return a value. Here’s an example:

    public int SomeFunction(String someArgument)
    {
        if (someArgument == null) throw new ArgumentNullException("someArgument");
        int retVal = 0;
        //Some code here
        return retVal;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that is essentially working until I tried to implement an
Just tried to execute a small Lua script, but unfortunately I'm doing something wrong.
Just tried some small graphics application of mine on Windows 7, and I'm getting
I just tried the tooltip like this Easy image preview ... But i cant
I have just tried using the NSArchiver but for some reason I am getting
I just tried to implement Fermat's little theorem in JavaScript. I tried it both
I've tried all sorts of design approaches to solve this problem, but I just
I wound up converting a table to just divs... But, in doing that, I
I try to implement a singleton pattern with the capability of passing arguments to
I've tried a few times to understand what a Singleton is. Perhaps I'm just

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.