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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:00:23+00:00 2026-06-11T05:00:23+00:00

I occasionally find myself in a situation where I have a variable that will

  • 0

I occasionally find myself in a situation where I have a variable that will only ever be used by one method in a class. Currently I use an instance variable, but it seems like bad design to have this variable visible to the rest of the class. Example just to illustrate a situation where I want this:

private Window _Window;
private void Show()
{
    if (_Window == null)
    {
        _Window = new Window();
        _Window.Closed += delegate { _Window = null; };
        _Window.Show();
    }
    _Window.BringIntoView();
}

The instance variable only exists to prevent more than one window being created at a time, so there’s no reason for the rest of the class to know about it. I’m reminded of C++’s ability to define static variables within a function.

Is there any way to achieve something like this is C#? Or am I stuck with the decision of choosing between bad design and bad encapsulation? (Assuming that this method truly doesn’t warrant a class of its own.)

  • 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-06-11T05:00:24+00:00Added an answer on June 11, 2026 at 5:00 am

    Is there any way to achieve something like this is C#?

    No.

    Or am I stuck with the decision of choosing between bad design and bad encapsulation? (Assuming that this method truly doesn’t warrant a class of its own.)

    Well, I’m not sure you’re actually stuck with either of those. It sounds like this is part of the state of the object, even if none of the other methods need to refer to that aspect of its state. Would it be logically wrong for other code within the same class to refer to this aspect of state? If so, why?

    (Admittedly I’ve occasionally wished for the ability to declare a field “within” a property declaration, forcing the rest of the class to access the state via the property…)

    • 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 used in image processing, and I find myself
I've used jqUploader for a while but I find that it will occasionally fail
I occasionally find myself with several methods in a class that all require the
I occasionally find myself starting and stopping multiple windows services. The only tool I'm
This is something I find myself wanting to do occasionally. Say I have a
We have a .NET Windows Service that occasionally shuts down on one particular server,
In C#, I find myself occasionally wanting to register a method for an event
Im trying to find all records that have similar tags to the currently viewed
I occasionally find that it would be useful to get the printed representation of
Occasionally I will know that there is a .NET Framework function that returns 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.