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. How can another class access my main Form 's variables?

How can another class access my main Form 's variables?

Scheduled Pinned Locked Moved C#
2 Posts 2 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
    dino2094
    wrote on last edited by
    #1

    My main Form (and only one) gets a packet of data and has to process it, then update textboxes, labels, etc on the form. It a lot of code(but not cpu intensive) so I'd like to put the processing code into another class but I don't know let the class get access to the main form's textboxes, labales, etc when that helper class needs to update the main form. Always gracious for your help.

    L 1 Reply Last reply
    0
    • D dino2094

      My main Form (and only one) gets a packet of data and has to process it, then update textboxes, labels, etc on the form. It a lot of code(but not cpu intensive) so I'd like to put the processing code into another class but I don't know let the class get access to the main form's textboxes, labales, etc when that helper class needs to update the main form. Always gracious for your help.

      L Offline
      L Offline
      Leslie Sanford
      wrote on last edited by
      #2

      Make the helper class a private class of the main form. Have the main form create an instance of the helper class passing itself to the object as its "owner." The private class can access the form and do what it needs to do to update the controls. When it comes time for an update, the form can call an "Update" method on the child object. However, an approach that's just as good in these situations is to keep update code in the main form class only factor it out into its own source code file. Use C#'s partial feature to seperate it out. In my opinion, this helps modularize the code without the need for an extra class. In some situations, you do actually want to factor functionality into a serparte class, but in other cases, like this one, in my opinion, partioning the code while keeping it in the same class is enough.

      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