Access Form from Class
-
Hi, I have a Class named Global and I'm Trying to access Form2 of my App. But sadly I can not. Normally I would use it as
Form2 frm = New Form2();
But I just cannot get Form2 on that Class:confused::confused:
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
Different namespace? Different project? Form2 already compiled?
-
Different namespace? Different project? Form2 already compiled?
Form2 is already Added to the Project. Code file Global.cs and Namespace Globals And in that file I want a common procedure that I can use on any form in my App. Hope this helps.
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
-
Hi, I have a Class named Global and I'm Trying to access Form2 of my App. But sadly I can not. Normally I would use it as
Form2 frm = New Form2();
But I just cannot get Form2 on that Class:confused::confused:
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
Are the Global X| class and the Form2 X| class both in the same namespace? Namespaces or reference errors could do that. Sadly without any further information the exact cause would continue to remain obscure to most non-clairvoyants. Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types. Need more info. Scott
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
-
Are the Global X| class and the Form2 X| class both in the same namespace? Namespaces or reference errors could do that. Sadly without any further information the exact cause would continue to remain obscure to most non-clairvoyants. Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types. Need more info. Scott
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
-
Thanks Scott, I had a spelling mistake in namespace
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
Out of curiosity, which IDE/editor are you using?
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
-
Are the Global X| class and the Form2 X| class both in the same namespace? Namespaces or reference errors could do that. Sadly without any further information the exact cause would continue to remain obscure to most non-clairvoyants. Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types. Need more info. Scott
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand
carbon_golem wrote:
Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types.
Why is this bad?, Because of hackers etc? or... Regards, Gareth.
-
Hi, I have a Class named Global and I'm Trying to access Form2 of my App. But sadly I can not. Normally I would use it as
Form2 frm = New Form2();
But I just cannot get Form2 on that Class:confused::confused:
- Stop thinking in terms of limitations and start thinking in terms of possibilities -
If you're doing this, it's an indication of bad design, violating OOP rules for encapsulation. Why would your "Global" class need to know anything about a form, or the controls on it? Hint: In proper design - the "Global" class shouldn't know or care about anything else outside of itself. If you want to send data and status information back to Form2, the Globals class should expose events or delegates that an instance of Form2 should subscribe to to get notification of these things.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
carbon_golem wrote:
Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types.
Why is this bad?, Because of hackers etc? or... Regards, Gareth.
gareth111 wrote:
Why is this bad?, Because of hackers etc? or...
No, not because of hackers. You don't do this because it's horribly bad design. There might be people comming in behind you that get stuck maintaining your code, and, after seeing this, might want to hunt you down and throw you into a vat of boiling lead.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
If you're doing this, it's an indication of bad design, violating OOP rules for encapsulation. Why would your "Global" class need to know anything about a form, or the controls on it? Hint: In proper design - the "Global" class shouldn't know or care about anything else outside of itself. If you want to send data and status information back to Form2, the Globals class should expose events or delegates that an instance of Form2 should subscribe to to get notification of these things.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
carbon_golem wrote:
Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types.
Why is this bad?, Because of hackers etc? or... Regards, Gareth.
I should have given a reason for that... OOP has a few important best-practices guidelines that usually appears in the beginning of text books that sadly are forgotten by the end of them. Objects should hide data, encapsulate data, and ensure data integrity to name a few. Objects serve a function of providing a structure around important data and work with other Objects to support a few inter-object relationships. (Is-A:association and Has-A:inheritance) Objects should also be coherent meaning if your problem calls for a Person and Pet, you would want to construct Person and Pet classes, and not put them into one 'God Object'. Global objects are not resistant to change either. I hope you're starting to see the reasoning for this. If you have a library with Person, and Pet classes, you don't have to write them again for one, but now the program is coherent when read. Tomes upon tomes have been written on the subject. I have given you a barely-adequate explanation, so I strongly recommend getting a book on object oriented programming, it's not just a bunch of fluff, it will save you effort bigtime. Scott
"Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand