biepbiep
biepbiep

Reputation: 217

Static vs Instance members in MainWindow

I understand the concept of Static and Instance but I am confused which I should use when I have a class of which only 1 instance will ever exist which is the instance which is called at the start of my application (=Application.Current.MainWindow)

I want to keep a list which I need thoughout different classes the whole time in my program. Should I make it static because there won't ever exist 2 instances of MainWindow? Or should I make it non-static since it sounds right saying that it belongs to the instance?

If I choose to make it non-static this will also mean that I always need to reference to it in other classes with "(MainWindow)Application.Current.MainWindow" which is annoying

Would it be "bad" to make the list static here?

Upvotes: 2

Views: 152

Answers (4)

Gayot Fow
Gayot Fow

Reputation: 8802

If you use an instance pattern, it paves the way for Dependency Injection and Module Discovery, which in turn facilitates unit testing of the components that use the service.

The instance pattern also provides for late binding, which can be very useful if the class needs something that is not known until run time. Lastly, the instance pattern enables 'mutability'.

If your application is built up by the interaction of various services, then a static pattern may be more appropriate.

Performance and memory footprint differences between the two are negligible.

Upvotes: 1

mungflesh
mungflesh

Reputation: 786

I think it would be bad to make it static but if your program is small and concise, you are unlikely to witness side effects such that you would come to the same conclusion.

This list which has items that might need sharing with other classes, could be contained in a separate class to the main window, and an interface implemented by this class can expose the required functionality, with respect to list access or manipulation.

You might like to try reading about "dependency injection". You could let the IOC container control the lifetime of the list-encapsulating class and only expose the interface to other application classes.

Upvotes: 0

Daniel Schealler
Daniel Schealler

Reputation: 396

Hard to say without seeing your code or understanding your work culture.

IMHO: So long as your assumption about 'only one instance' holds true then you'll be able to get away with static properties and methods. That strikes me as a bit dubious.

If you were a junior or intermediate working with me then I would prefer for you to practice the habit of using instance properties, methods and events, and reserve the use of static members for when they're called for.

Just my 2c though. Opinions on this will vary.

Servy's comment above gets my endorsement, btw.

Upvotes: 0

Caleb
Caleb

Reputation: 1088

It doesn't seem like a static would be that much of an issue. If it seems odd to you though, you could make a "Singleton" of your MainWindow that the rest of the program uses.

See the docs: http://msdn.microsoft.com/en-us/library/ff650316.aspx

Upvotes: 1

Related Questions