Releis
Releis

Reputation: 27

c# WinForms accessing instance from another class

I'm facing this problem (i'll make it short and easy) In form1 i need to create an instance of Login form and be able to access it in the class abc and use it's(login's) method.However, it appears to be inaccessible.

I hope i said all the relevant in an understandable.

Thanks in advance

Upvotes: 0

Views: 163

Answers (2)

Eric J.
Eric J.

Reputation: 150238

Fundamentally to do exactly what you ask, you need to provide an instance of Login to abc, for example by passing it in through the abc constructor.

You may wish to consider refactoring some of the functionality currently in Login form to make it useful in all places it's needed.

I suggest having a look at the MVC pattern. While WPF, Silverlight and ASP.Net MVC provide strong support for that pattern, you can certainly apply it in a WinForms scenario as well.

Using a pattern like MVC will make your code far easier to evolve and maintain over time.

Upvotes: 1

Jeff Watkins
Jeff Watkins

Reputation: 6357

If you're calling actual functionality belonging to another form explicitly, it is possibly time to look at your design. You should probably have a class or assembly dealing with authentication which is decoupled from any form. Use it in both your login form and class ABC.

Upvotes: 1

Related Questions