|
-
Aug 27th, 2007, 06:04 PM
#1
Thread Starter
Frenzied Member
[2.0] Form.ActiveForm
Hey all,
I have a class which contains a private method. The private method must have access to the main form. For example, I could do this:
Code:
frmSnake.ActiveForm.PropertyName
But ActiveForm in my opinion is very bad programming to use in this situation. Sometimes the code fails at runtime and it just isn't a reliable thing to do in my opinion.
So I don't want to use frmSnake.ActiveForm and the method is private so I cannot really pass any Form variables directly to it..
Any ideas on this one? Here's some pics to help you envision my problem:
-
Aug 27th, 2007, 06:29 PM
#2
Re: [2.0] Form.ActiveForm
Firstly, you wouldn't use the ActiveForm property unless you specifically wanted a reference to the active form, whatever form that might be. If you specifically want a reference to your startup form then that's just not appropriate. Using the ActiveForm property is good programming if you want a reference to the active form, other wise it's not.
Secondly, the ActiveForm property is static, so you call it on the Form class, not on an instance of the class.
If you can't directly pass a reference to the main form to the places it's needed then you will need to use a global variable. You could use a static class, which is the C# equivalent of a VB module, or you could use a Singleton class. Either way you'd be setting and getting the main form reference via a static property so you'd have access to it from anywhere.
-
Aug 27th, 2007, 06:34 PM
#3
Thread Starter
Frenzied Member
Re: [2.0] Form.ActiveForm
The whole idea sounds a bit messy. I only need to be able to add controls to the forum. There must be a better solution than creating a static class or singleton JUST for calling the Controls property of the main form.
Hopefully a better solution?
-
Aug 27th, 2007, 07:38 PM
#4
Re: [2.0] Form.ActiveForm
I fail to see what's bad about the solution provided such that a better one need be sought. The "proper" way to get access to your main form elsewhere is to pass a reference to it where it's needed. THAT is the better solution. If you can't or won't do that then the alternative is as I described. VB has the My.Application.ApplicationContext.MainForm property; C# does not. Unless you have some magic that I don't know about then you will use the solution provided.
-
Aug 27th, 2007, 08:26 PM
#5
Thread Starter
Frenzied Member
Re: [2.0] Form.ActiveForm
-
Aug 27th, 2007, 09:33 PM
#6
Re: [2.0] Form.ActiveForm
 Originally Posted by jmcilhinney
Unless you have some magic that I don't know about then you will use the solution provided.
You will use that solution because that's the only solution available, not just because I said so.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|