Author Topic: Forms and Class declared variables  (Read 2736 times)

0 Members and 1 Guest are viewing this topic.

T.Willey

  • Needs a day job
  • Posts: 5251
Forms and Class declared variables
« on: September 29, 2009, 03:30:01 PM »
I was wondering why Class declared variables are not available to the Form code.

Code: [Select]

public partial class Test : Form {

private Dictionary<key, value> test;

public Form () {

InitializeComponent();

}

public void LoadForm ( object sender, System.EventArgs e ) {

...... here test is nothing ....

}

public void CallingSection () {

..... here I set test to something ...
AcadApp.ShowModalDialog( new Form() );
.... here test is still what I set it to before the form call ....

}

}

I thought if you defined a class variable, it would be accessible from anywhere in the class?  In other subs it is accessible for setting or getting, but if I put the declaration in the form section, then I can't get it anywhere else, and if I assigned it anywhere else in the code, it is not defined within the form section.

Am I just not understanding this concept correctly?

Thanks in advance ( even if it's RTFM; just help me with where to start ).
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

Glenn R

  • Guest
Re: Forms and Class declared variables
« Reply #1 on: September 29, 2009, 04:16:04 PM »
From a very brief scan, you need to init the 'Test' class which is where the 'test' variable is located ie. a constructor for the 'Test' class. A reference member variable of a class is always initialised to null unless you declare it otherwise.

this.test = new Whatevertestis();

Late here and eyes hanging out my head...and stop using VB terms for C# Tim < spank > ;)

Have you purchased Andrew Troelsen's book on C# - Pro C# 2008 and the .NET 3.5 Platform? It is the bible as far as I'm concerned. Read it, then read it again. The different editions over the years have taught me a lot wrt to C#.

Cheers,
Glenn.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Forms and Class declared variables
« Reply #2 on: September 29, 2009, 04:24:16 PM »
From a very brief scan, you need to init the 'Test' class which is where the 'test' variable is located ie. a constructor for the 'Test' class. A reference member variable of a class is always initialised to null unless you declare it otherwise.

this.test = new Whatevertestis();

Thanks Glenn.  I think I get most of what you said, as it's been a while since I did anything in C#.

Late here and eyes hanging out my head...and stop using VB terms for C# Tim < spank > ;)

No comment.......  :-)

Have you purchased Andrew Troelsen's book on C# - Pro C# 2008 and the .NET 3.5 Platform? It is the bible as far as I'm concerned. Read it, then read it again. The different editions over the years have taught me a lot wrt to C#.

Cheers,
Glenn.

I have 2005 .Net 2.0.  Since we are still using 1.1 as company standard, I don't want to know too more than I can use.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

sinc

  • Guest
Re: Forms and Class declared variables
« Reply #3 on: September 29, 2009, 04:40:27 PM »
I have 2005 .Net 2.0.  Since we are still using 1.1 as company standard, I don't want to know too more than I can use.

Why .NET 1.1?  That's pretty ancient, and a whole slew of things are much easier even in .NET 2.0 (let alone .NET 3.5).

The .NET 3.5 framework is free from Microsoft, so it's easy enough to put it on pretty much any Windows machine (unless you are writing stuff for computers that pre-date Windows XP...?)

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Forms and Class declared variables
« Reply #4 on: September 29, 2009, 04:52:23 PM »
I have 2005 .Net 2.0.  Since we are still using 1.1 as company standard, I don't want to know too more than I can use.

Why .NET 1.1?  That's pretty ancient, and a whole slew of things are much easier even in .NET 2.0 (let alone .NET 3.5).

The .NET 3.5 framework is free from Microsoft, so it's easy enough to put it on pretty much any Windows machine (unless you are writing stuff for computers that pre-date Windows XP...?)


I'm not a paid programmer, just a cad monkey, so they use what they want to use, and right now it is 1.1.  We are still on '06 for the moment, but will be switching to '09 soon, so I figure we will use a more up to date framework, but until then I'm stuck pretty much with 1.1.  So to answer your question; I don't know why 1.1.   :-)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Forms and Class declared variables
« Reply #5 on: September 30, 2009, 07:18:37 PM »
I tried putting something in the Initialize portion of the class, but it would only assign it the first time it was called, and I would need it to be updated per call to the code, but I did find a way to make it work.  I had to use the keyword static within the declaration.

Code: [Select]

public partial class Test : Form {

[color=red]private static Dictionary<key, value> test;[/color]

public Form () {

InitializeComponent();

}

public void LoadForm ( object sender, System.EventArgs e ) {

...... here test is nothing ....

}

public void CallingSection () {

..... here I set test to something ...
AcadApp.ShowModalDialog( new Form() );
.... here test is still what I set it to before the form call ....

}

}
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.