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. Global Variables ...wtf

Global Variables ...wtf

Scheduled Pinned Locked Moved C#
questionwinforms
8 Posts 6 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.
  • A Offline
    A Offline
    antoine orchus tech
    wrote on last edited by
    #1

    Hi You have two Windows Forms from one project. One calls the other and closes itself. But before the first Windows Form closes itself, it must send some data to the second Windows Form so that when it is created, it formats itself accordingly to the data passed. Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? Thanks Antoine This by our hands that dream, "I shall find a way or make one!" great quote heh? ;)

    S M F L 4 Replies Last reply
    0
    • A antoine orchus tech

      Hi You have two Windows Forms from one project. One calls the other and closes itself. But before the first Windows Form closes itself, it must send some data to the second Windows Form so that when it is created, it formats itself accordingly to the data passed. Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? Thanks Antoine This by our hands that dream, "I shall find a way or make one!" great quote heh? ;)

      S Offline
      S Offline
      S O S
      wrote on last edited by
      #2

      Make the second form's constructor have some parameters that receive it?

      1 Reply Last reply
      0
      • A antoine orchus tech

        Hi You have two Windows Forms from one project. One calls the other and closes itself. But before the first Windows Form closes itself, it must send some data to the second Windows Form so that when it is created, it formats itself accordingly to the data passed. Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? Thanks Antoine This by our hands that dream, "I shall find a way or make one!" great quote heh? ;)

        M Offline
        M Offline
        Marc Clifton
        wrote on last edited by
        #3

        You may need an independant storage mechanism. Essentially a container for global data. Alternatively, the native API call supports passing user-defined data to the window create command, I believe. Besides, there isn't anything wrong with global data--it just needs to be managed. Finally, I'm not exactly too sure what the problem is--can't you just pass the data as part of the constructor call to the other form? Marc STL, a liability factory - Anonymously
        A doable project is one that is small enough to be done quickly and big enough to be interesting - Ken Orr
        Latest AAL Article My blog

        1 Reply Last reply
        0
        • A antoine orchus tech

          Hi You have two Windows Forms from one project. One calls the other and closes itself. But before the first Windows Form closes itself, it must send some data to the second Windows Form so that when it is created, it formats itself accordingly to the data passed. Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? Thanks Antoine This by our hands that dream, "I shall find a way or make one!" great quote heh? ;)

          F Offline
          F Offline
          Furty
          wrote on last edited by
          #4

          Orlanda Ramos wrote: Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? As pssing properties in the constructor could be perceived as creating a dependency, the other way to do this would be to provide public or internal accessors for the properties you want to pass in form 2. public class Form2 : Form { private int myInt; private string myString; public int MyInt { set { myInt = value; } } public string MyString { set { myString = value; } } public Form2() { } } public class Form1 : Form { public Form1() { } private void OpenForm2(int myInt, string myString) { Form2 form2 = new Form2(); form2.MyInt = myInt; form2.MyString = myString; form2.Show(); } }

          J A 2 Replies Last reply
          0
          • F Furty

            Orlanda Ramos wrote: Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? As pssing properties in the constructor could be perceived as creating a dependency, the other way to do this would be to provide public or internal accessors for the properties you want to pass in form 2. public class Form2 : Form { private int myInt; private string myString; public int MyInt { set { myInt = value; } } public string MyString { set { myString = value; } } public Form2() { } } public class Form1 : Form { public Form1() { } private void OpenForm2(int myInt, string myString) { Form2 form2 = new Form2(); form2.MyInt = myInt; form2.MyString = myString; form2.Show(); } }

            J Offline
            J Offline
            James T Johnson
            wrote on last edited by
            #5

            This is the way I would do it, just because it is more flexible. If you use either method I would say that you aren't creating a dependency between the two forms, but a dependency on the data required for Form2 to work correctly. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him

            1 Reply Last reply
            0
            • A antoine orchus tech

              Hi You have two Windows Forms from one project. One calls the other and closes itself. But before the first Windows Form closes itself, it must send some data to the second Windows Form so that when it is created, it formats itself accordingly to the data passed. Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? Thanks Antoine This by our hands that dream, "I shall find a way or make one!" great quote heh? ;)

              L Offline
              L Offline
              LovelyXiaoXinXin
              wrote on last edited by
              #6

              You can build a class which is called GlobalVars NameSpace GlobalVars { public class GlobalVars { public static string stringVars; public class GlobalVars() {} } }

              F 1 Reply Last reply
              0
              • L LovelyXiaoXinXin

                You can build a class which is called GlobalVars NameSpace GlobalVars { public class GlobalVars { public static string stringVars; public class GlobalVars() {} } }

                F Offline
                F Offline
                Furty
                wrote on last edited by
                #7

                whoops, in this example the constructor will never be called automatically - to make this a fully static class the constructor line should be: static GlobalVars() not public class GlobalVars() The advantage of using a static constructor is that it allows you to dynamically set values/execute code before any properties are sent to the requesting class. Note that you cannot control exactly when a static constructor is initiated, but you can rest assured that it will happen before any values are passed in or out of your static class.

                1 Reply Last reply
                0
                • F Furty

                  Orlanda Ramos wrote: Now, pray tell, what is the best, non-obstrusive way of doing that without creating ugly dependencies between those two? As pssing properties in the constructor could be perceived as creating a dependency, the other way to do this would be to provide public or internal accessors for the properties you want to pass in form 2. public class Form2 : Form { private int myInt; private string myString; public int MyInt { set { myInt = value; } } public string MyString { set { myString = value; } } public Form2() { } } public class Form1 : Form { public Form1() { } private void OpenForm2(int myInt, string myString) { Form2 form2 = new Form2(); form2.MyInt = myInt; form2.MyString = myString; form2.Show(); } }

                  A Offline
                  A Offline
                  antoine orchus tech
                  wrote on last edited by
                  #8

                  That wraps it up preaty niely ;-) Thanks everyone, Furty! Antoine This by our hands that dream, "I shall find a way or make one!" great quote heh? ;)

                  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