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. Access Form from Class

Access Form from Class

Scheduled Pinned Locked Moved C#
11 Posts 5 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.
  • P parth p

    Hi, I have a Class named Global and I'm Trying to access Form2 of my App. But sadly I can not. Normally I would use it as

    Form2 frm = New Form2();

    But I just cannot get Form2 on that Class:confused::confused:

    - Stop thinking in terms of limitations and start thinking in terms of possibilities -

    B Offline
    B Offline
    buchstaben
    wrote on last edited by
    #2

    Different namespace? Different project? Form2 already compiled?

    P 1 Reply Last reply
    0
    • B buchstaben

      Different namespace? Different project? Form2 already compiled?

      P Offline
      P Offline
      parth p
      wrote on last edited by
      #3

      Form2 is already Added to the Project. Code file Global.cs and Namespace Globals And in that file I want a common procedure that I can use on any form in my App. Hope this helps.

      - Stop thinking in terms of limitations and start thinking in terms of possibilities -

      1 Reply Last reply
      0
      • P parth p

        Hi, I have a Class named Global and I'm Trying to access Form2 of my App. But sadly I can not. Normally I would use it as

        Form2 frm = New Form2();

        But I just cannot get Form2 on that Class:confused::confused:

        - Stop thinking in terms of limitations and start thinking in terms of possibilities -

        C Offline
        C Offline
        carbon_golem
        wrote on last edited by
        #4

        Are the Global X| class and the Form2 X| class both in the same namespace? Namespaces or reference errors could do that. Sadly without any further information the exact cause would continue to remain obscure to most non-clairvoyants. Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types. Need more info. Scott

        "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

        P G 2 Replies Last reply
        0
        • C carbon_golem

          Are the Global X| class and the Form2 X| class both in the same namespace? Namespaces or reference errors could do that. Sadly without any further information the exact cause would continue to remain obscure to most non-clairvoyants. Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types. Need more info. Scott

          "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

          P Offline
          P Offline
          parth p
          wrote on last edited by
          #5

          Thanks Scott, I had a spelling mistake in namespace

          - Stop thinking in terms of limitations and start thinking in terms of possibilities -

          C 1 Reply Last reply
          0
          • P parth p

            Thanks Scott, I had a spelling mistake in namespace

            - Stop thinking in terms of limitations and start thinking in terms of possibilities -

            C Offline
            C Offline
            carbon_golem
            wrote on last edited by
            #6

            Out of curiosity, which IDE/editor are you using?

            "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

            1 Reply Last reply
            0
            • C carbon_golem

              Are the Global X| class and the Form2 X| class both in the same namespace? Namespaces or reference errors could do that. Sadly without any further information the exact cause would continue to remain obscure to most non-clairvoyants. Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types. Need more info. Scott

              "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

              G Offline
              G Offline
              Gareth H
              wrote on last edited by
              #7

              carbon_golem wrote:

              Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types.

              Why is this bad?, Because of hackers etc? or... Regards, Gareth.

              D C 2 Replies Last reply
              0
              • P parth p

                Hi, I have a Class named Global and I'm Trying to access Form2 of my App. But sadly I can not. Normally I would use it as

                Form2 frm = New Form2();

                But I just cannot get Form2 on that Class:confused::confused:

                - Stop thinking in terms of limitations and start thinking in terms of possibilities -

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #8

                If you're doing this, it's an indication of bad design, violating OOP rules for encapsulation. Why would your "Global" class need to know anything about a form, or the controls on it? Hint: In proper design - the "Global" class shouldn't know or care about anything else outside of itself. If you want to send data and status information back to Form2, the Globals class should expose events or delegates that an instance of Form2 should subscribe to to get notification of these things.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                     2006, 2007, 2008

                G 1 Reply Last reply
                0
                • G Gareth H

                  carbon_golem wrote:

                  Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types.

                  Why is this bad?, Because of hackers etc? or... Regards, Gareth.

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #9

                  gareth111 wrote:

                  Why is this bad?, Because of hackers etc? or...

                  No, not because of hackers. You don't do this because it's horribly bad design. There might be people comming in behind you that get stuck maintaining your code, and, after seeing this, might want to hunt you down and throw you into a vat of boiling lead.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008

                  1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    If you're doing this, it's an indication of bad design, violating OOP rules for encapsulation. Why would your "Global" class need to know anything about a form, or the controls on it? Hint: In proper design - the "Global" class shouldn't know or care about anything else outside of itself. If you want to send data and status information back to Form2, the Globals class should expose events or delegates that an instance of Form2 should subscribe to to get notification of these things.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007, 2008

                    G Offline
                    G Offline
                    Gareth H
                    wrote on last edited by
                    #10

                    Thanks for that Dave. Regards, Gareth.

                    1 Reply Last reply
                    0
                    • G Gareth H

                      carbon_golem wrote:

                      Another piece of possibly unwanted advice from me would be to avoid at all costs any type of "God Object" global type poison in your code, and please use appropriately named types.

                      Why is this bad?, Because of hackers etc? or... Regards, Gareth.

                      C Offline
                      C Offline
                      carbon_golem
                      wrote on last edited by
                      #11

                      I should have given a reason for that... OOP has a few important best-practices guidelines that usually appears in the beginning of text books that sadly are forgotten by the end of them. Objects should hide data, encapsulate data, and ensure data integrity to name a few. Objects serve a function of providing a structure around important data and work with other Objects to support a few inter-object relationships. (Is-A:association and Has-A:inheritance) Objects should also be coherent meaning if your problem calls for a Person and Pet, you would want to construct Person and Pet classes, and not put them into one 'God Object'. Global objects are not resistant to change either. I hope you're starting to see the reasoning for this. If you have a library with Person, and Pet classes, you don't have to write them again for one, but now the program is coherent when read. Tomes upon tomes have been written on the subject. I have given you a barely-adequate explanation, so I strongly recommend getting a book on object oriented programming, it's not just a bunch of fluff, it will save you effort bigtime. Scott

                      "Run for your life from any man who tells you that money is evil. That sentence is the leper's bell of an approaching looter." --Ayn Rand

                      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