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. windows application

windows application

Scheduled Pinned Locked Moved C#
question
5 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.
  • J Offline
    J Offline
    jashimu
    wrote on last edited by
    #1

    Hi Everyone, I have an application that has at least two forms and a Class. how do I access variable between two forms without initializing all varialbes each time I access the class. this is how I access functions form the Class In Form1 I do this: ClassA myClass = new ClassA(); myClass.Function_A(); // do something with it. IN Form2 ClassA myClass2 = new ClassA(); myClass2.Function_B(); // do something with it. how do i prevent ClassA funtions from reinitializing all over again. I want to treat the ClassA's funtions and variables like Global variable. Thanks

    "Show me the way I will get there"

    M R L 3 Replies Last reply
    0
    • J jashimu

      Hi Everyone, I have an application that has at least two forms and a Class. how do I access variable between two forms without initializing all varialbes each time I access the class. this is how I access functions form the Class In Form1 I do this: ClassA myClass = new ClassA(); myClass.Function_A(); // do something with it. IN Form2 ClassA myClass2 = new ClassA(); myClass2.Function_B(); // do something with it. how do i prevent ClassA funtions from reinitializing all over again. I want to treat the ClassA's funtions and variables like Global variable. Thanks

      "Show me the way I will get there"

      M Offline
      M Offline
      Manas Bhardwaj
      wrote on last edited by
      #2

      This [^]one is a nice example

      Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

      1 Reply Last reply
      0
      • J jashimu

        Hi Everyone, I have an application that has at least two forms and a Class. how do I access variable between two forms without initializing all varialbes each time I access the class. this is how I access functions form the Class In Form1 I do this: ClassA myClass = new ClassA(); myClass.Function_A(); // do something with it. IN Form2 ClassA myClass2 = new ClassA(); myClass2.Function_B(); // do something with it. how do i prevent ClassA funtions from reinitializing all over again. I want to treat the ClassA's funtions and variables like Global variable. Thanks

        "Show me the way I will get there"

        R Offline
        R Offline
        realJSOP
        wrote on last edited by
        #3

        You could add a constructor to the form class that accepts a parameter that is another copy of the form. Then, write a clone method that copies the data from the speficied instance into the form you're creating. IMHO, this is a silly approach to pretty much anything, but it will do what you want. If you're actually just interested in keeping just one instance of the form around and using it whenever you want, hide it when you're done with it, and show it again when you need it.

        .45 ACP - because shooting twice is just silly
        -----
        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

        modified on Wednesday, June 9, 2010 9:03 AM

        1 Reply Last reply
        0
        • J jashimu

          Hi Everyone, I have an application that has at least two forms and a Class. how do I access variable between two forms without initializing all varialbes each time I access the class. this is how I access functions form the Class In Form1 I do this: ClassA myClass = new ClassA(); myClass.Function_A(); // do something with it. IN Form2 ClassA myClass2 = new ClassA(); myClass2.Function_B(); // do something with it. how do i prevent ClassA funtions from reinitializing all over again. I want to treat the ClassA's funtions and variables like Global variable. Thanks

          "Show me the way I will get there"

          L Offline
          L Offline
          LookSharp
          wrote on last edited by
          #4

          Similar to John Simmons's answer, but let each form's constructor take a ClassA. Now, create the ClassA before creating either form, and pass it to the form before showing it. The important fact is that neither form 'owns' (or instantiates) the shared instance of ClassA, rather, some larger scope owns it and makes it available to both forms that want to use it. An alternative to passing the ClassA into the constructor is to make a ClassA property on each form, and assign the property before showing the form. This is a very common pattern when presenting dialogs whose purpose is to modify data that is used elsewhere. Here's a snippet:

          public void DoFormStuff(ClassA ca)
          	{
          	using (MyForm frm = new MyForm())
          		{
          		frm.ClassA = ca;
          		frm.ShowDialog(this);
          		}
          	}
          
          J 1 Reply Last reply
          0
          • L LookSharp

            Similar to John Simmons's answer, but let each form's constructor take a ClassA. Now, create the ClassA before creating either form, and pass it to the form before showing it. The important fact is that neither form 'owns' (or instantiates) the shared instance of ClassA, rather, some larger scope owns it and makes it available to both forms that want to use it. An alternative to passing the ClassA into the constructor is to make a ClassA property on each form, and assign the property before showing the form. This is a very common pattern when presenting dialogs whose purpose is to modify data that is used elsewhere. Here's a snippet:

            public void DoFormStuff(ClassA ca)
            	{
            	using (MyForm frm = new MyForm())
            		{
            		frm.ClassA = ca;
            		frm.ShowDialog(this);
            		}
            	}
            
            J Offline
            J Offline
            jashimu
            wrote on last edited by
            #5

            HI LookSharp, I am not sure how to use it exactly, let me try first then I will ask you later. Thanks for your help.

            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