Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Accessing varibles across forms.

Accessing varibles across forms.

Scheduled Pinned Locked Moved C#
question
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    draco_iii
    wrote on last edited by
    #1

    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.

    L S O 3 Replies Last reply
    0
    • D draco_iii

      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.

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      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."

      D 1 Reply Last reply
      0
      • D draco_iii

        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.

        S Offline
        S Offline
        Stephane Rodriguez
        wrote on last edited by
        #3

        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).

        D 1 Reply Last reply
        0
        • D draco_iii

          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.

          O Offline
          O Offline
          Omega501
          wrote on last edited by
          #4

          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 form

          Then 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?

          1 Reply Last reply
          0
          • S Stephane Rodriguez

            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).

            D Offline
            D Offline
            draco_iii
            wrote on last edited by
            #5

            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".

            1 Reply Last reply
            0
            • L leppie

              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."

              D Offline
              D Offline
              draco_iii
              wrote on last edited by
              #6

              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.

              L 1 Reply Last reply
              0
              • D draco_iii

                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.

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                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."

                D 1 Reply Last reply
                0
                • L leppie

                  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."

                  D Offline
                  D Offline
                  draco_iii
                  wrote on last edited by
                  #8

                  Thanks. As soon as I got the space out of the name it worked perfectly. There needs to be a an article about making a static varible class. It is the best way I have seen to have global varibles.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups