By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,080 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,080 IT Pros & Developers. It's quick & easy.

Reference external class from Windows Form

P: 2
Hopefully this hasn't been asked already - apologies if so

I have a Windows Form running inside C# - built using the form designer

I've built a number of methods inside the form Events which I now want to strip out into External class files (in order to try and make the app a bit more architecturally correct so to speak).

To do this, I have created a new Project which I've added to the Solution.
I've managed to connect the class to the form by building the .cs file and then referencing the class in my form method - example reference:

ClearDetails cls = new ClearDetails();

So, if I have this right, I should be able to add my namespace to my form code, then specify the above code inside the Form Load event, then ultimately, the cls object created above should be available inside any event within the form?

However, this does not appear to be the case - the cls object is only available in the Form Load event?

My Class file contains a Public ClearDetails constructor along with a public method. The class file also uses the System.Windows.Forms namespace. I am using VS 2008 with Framework 3.5

If anyone could help or point me in the right direction, your help would be greatly appreciated

Many Thanks,
Dan
Apr 2 '10 #1

✓ answered by tlhintoq

Its a matter of scope. In easy terms: A variable only exists between the { and }
If you give it life in the Load() {} then it dies at the end of the Load() {}
But if you give it life at the Class() {} then it doesn't die until the end of the class () {}

So move the creation of the variable up a little, but set its value in the load()

Expand|Select|Wrap|Line Numbers
  1.  
  2. namespace blahblah
  3. {
  4.    class  widget
  5.    {
  6.       ClearDetails myDetails; // Scope lifetime of the entire Widget class
  7.  
  8.       Form1_load
  9.       {
  10.            myDetails = new ClearDetails(); // assigned a value
  11.       }
  12.  
  13.    }
  14. }
  15.  
  16.  

Share this Question
Share on Google+
2 Replies


tlhintoq
Expert 2.5K+
P: 3,525
Its a matter of scope. In easy terms: A variable only exists between the { and }
If you give it life in the Load() {} then it dies at the end of the Load() {}
But if you give it life at the Class() {} then it doesn't die until the end of the class () {}

So move the creation of the variable up a little, but set its value in the load()

Expand|Select|Wrap|Line Numbers
  1.  
  2. namespace blahblah
  3. {
  4.    class  widget
  5.    {
  6.       ClearDetails myDetails; // Scope lifetime of the entire Widget class
  7.  
  8.       Form1_load
  9.       {
  10.            myDetails = new ClearDetails(); // assigned a value
  11.       }
  12.  
  13.    }
  14. }
  15.  
  16.  
Apr 2 '10 #2

P: 2
Many thanks tlhintoq

Understand. That all works a treat now
Apr 4 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.