Accessing varibles across forms.
-
I have a varible that is declared in the main application form as public. How can I get at this varible from another form. I have tried accessing the forms varible via Form1.varible and this.ParentForm.varible and so forth. All to no avail and I need to have access to this varible from anywhere in the program. Any suggesttion would be greatly appreciated.
-
I have a varible that is declared in the main application form as public. How can I get at this varible from another form. I have tried accessing the forms varible via Form1.varible and this.ParentForm.varible and so forth. All to no avail and I need to have access to this varible from anywhere in the program. Any suggesttion would be greatly appreciated.
DId you cast it (the form) to your form before you tried to access it? draco_iii wrote: All to no avail and I need to have access to this varible from anywhere in the program. I suggest making a "static" class then to hold these variables. MyDUMeter: a .NET DUMeter clone
"Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of." -
I have a varible that is declared in the main application form as public. How can I get at this varible from another form. I have tried accessing the forms varible via Form1.varible and this.ParentForm.varible and so forth. All to no avail and I need to have access to this varible from anywhere in the program. Any suggesttion would be greatly appreciated.
this.ParentForm
returns a System.Windows.Forms.Form class type, not your derived class. In fact,this.ParentForm
is mostly used for UI logic. For your application logic, it's better to either let the two forms know about each other existence or, better, share a property bag class (better in terms of decoupling). -
I have a varible that is declared in the main application form as public. How can I get at this varible from another form. I have tried accessing the forms varible via Form1.varible and this.ParentForm.varible and so forth. All to no avail and I need to have access to this varible from anywhere in the program. Any suggesttion would be greatly appreciated.
I'm not sure if this is the correct way of doing things, but I have been able to access properties and function in parent/child forms like this.
// in ParentForm parent
ChildForm child = new ChildForm();
child.Owner = this;
// access custom property or function in the child form
((ChildForm)child).PublicProperty = MyVal;
// show the child formThen you can access the parents public properties like this
// in ChildForm child
((ParentForm)Owner).PublicProperty = MyVal;
((ParentForm)Owner).PublicFunction();I know that this works, but is it the correct way of doing things? And if there is a better way of this, any example code on this?
-
this.ParentForm
returns a System.Windows.Forms.Form class type, not your derived class. In fact,this.ParentForm
is mostly used for UI logic. For your application logic, it's better to either let the two forms know about each other existence or, better, share a property bag class (better in terms of decoupling).How do you go about letting them know about each others existance or at least letting the child forms see full existance of the main running form? I need to be able keep one set of the varibles not multiple instances so that they can be written to or read from by any of the child forms. Doing so, would allow less overhead and insure that all of the child forms are working from the same set of "settings".
-
DId you cast it (the form) to your form before you tried to access it? draco_iii wrote: All to no avail and I need to have access to this varible from anywhere in the program. I suggest making a "static" class then to hold these variables. MyDUMeter: a .NET DUMeter clone
"Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of."Made the static class for the variable it look like this
using System; using Perpetual_Settings; namespace Brazen_Mail { /// /// Summary description for Setting. /// public class Setting { public static Perpetual_Settings.PerpetualSettings test = new PerpetualSettings(); static Setting() { test = new PerpetualSettings(); } } }
But when running the code I get this error.
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in system.windows.forms.dll
Additional information: File or assembly name Perpetual Settings, or one of its dependencies, was not found.
I can see the Properties and Methods from the class when trying access them via VS, but it craps out when I try and run the program.
-
Made the static class for the variable it look like this
using System; using Perpetual_Settings; namespace Brazen_Mail { /// /// Summary description for Setting. /// public class Setting { public static Perpetual_Settings.PerpetualSettings test = new PerpetualSettings(); static Setting() { test = new PerpetualSettings(); } } }
But when running the code I get this error.
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in system.windows.forms.dll
Additional information: File or assembly name Perpetual Settings, or one of its dependencies, was not found.
I can see the Properties and Methods from the class when trying access them via VS, but it craps out when I try and run the program.
Spaces in general is a BAD idea spawned from some VB coders on crack. NEVER use spaces. I assume your dll is called "Perpetual Settings.dll". Spaces are used to space code elements, not making pretty names. MyDUMeter: a .NET DUMeter clone
"Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of." -
Spaces in general is a BAD idea spawned from some VB coders on crack. NEVER use spaces. I assume your dll is called "Perpetual Settings.dll". Spaces are used to space code elements, not making pretty names. MyDUMeter: a .NET DUMeter clone
"Thats like saying "hahahaha he doesnt know the difference between a cyberneticradioactivenuclothermolopticdimswitch and a biocontainingspherogramotron", but with words you have really never heard of."