Method instance variable

I sometimes find myself in a situation where I have a variable that will be used by only one method in the class. I am currently using an instance variable, but it seems that a bad construct has this variable visible to the rest of the class. An example to illustrate a situation when I want this:

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

An instance variable exists only to prevent the creation of more than one window at a time, so there is no reason for the rest of the class to know about it. I was reminded of the ability of C ++ to define static variables inside a function.

Is there a way to achieve something like C #? Or am I sticking to the decision between bad design and bad encapsulation? (Assuming this method does not really guarantee its own class.)

+4
source share
4 answers

Is there a way to achieve something like C #?

No.

Or am I sticking to the decision between bad design and bad encapsulation? (Assuming this method does not really guarantee its own class.)

Well, I'm not sure that you are actually stuck with this. This appears to be part of the state of the object, even if none of the other methods should reference this aspect of its state. Would it be logical wrong if other code inside the same class referred to this aspect of the state? If so, why?

(Admittedly, I sometimes wanted the declaration to declare a field β€œinside” the property declaration, forcing the rest of the class to access the state through the property ...)

+4
source

Is there a way to achieve something like C #?

No, unfortunately, C # does not have the ability to define a class level variable bound to a method.

As an additional note, Visual Basic does this using the Static modifier . This causes the compiler to create a (decorated) class level variable, but the language will only allow it to be used in the Sub or Function in which it is defined. However, other languages ​​will see it as a member variable.

+1
source

The only approach I know is worse than the mess you are trying to avoid.

 // Constructor public MyClass() { { // _Window is private to this closure Window _Window; Show = () => { if (_Window == null) { _Window = new Window(); _Window.Closed += delegate { _Window = null; }; _Window.Show(); } _Window.BringIntoView(); }; } } // Replaces Show method private Action Show; 

Then use Show.Invoke () instead of Show (). However, there are places where this approach is better: http://www.headspring.com/patrick/public-private-super-private/ . However, in my opinion, the private method should be in C # / Java.

+1
source

The parameter should be to allow the window class to maintain a static reference to the instance of itself. In the window class, you also put the static Display() method, allowing the window class to handle creating and deleting instances. Basically you have to move your code to your Windows class.

 private static DisplayWindow _window; public static void Display() { if (_window == null) { _window = new DisplayWindow(); _window.Closed += delegate { _window = null; }; _window.Show(); } _window.BringIntoView(); } 

In another class

 DisplayWindow.Display(); // No variable required here! 

If you want to prevent the wild creation of windows of this type, make the window constructor closed

 private DisplayWindow() { } 

This causes other classes to call Display() if they want to open this window.

0
source

All Articles