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. Windows Forms
  4. c# Winforms with classes.

c# Winforms with classes.

Scheduled Pinned Locked Moved Windows Forms
csharpwinformshelptutorial
6 Posts 4 Posters 4 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.
  • C Offline
    C Offline
    Cliffordagius
    wrote on last edited by
    #1

    Hi, I have a winforms project with multiple forms that are children of a parent form using the MDI format. Now my main form which is the parent of all the others has a class instance created in the code to do some comms work. My issue is thatI want to be able to access some of the methods and properties of this instance from my other child forms to for example change some settings etc. Can someone point me in the right direction please. example of the code in Main.cs myClass mainClass = new myClass(); In admin.cs I want to access the PacketSize property of the myClass instance that is created in the Main.cs Can this be done please say yes... Cliff.

    L L B 4 Replies Last reply
    0
    • C Cliffordagius

      Hi, I have a winforms project with multiple forms that are children of a parent form using the MDI format. Now my main form which is the parent of all the others has a class instance created in the code to do some comms work. My issue is thatI want to be able to access some of the methods and properties of this instance from my other child forms to for example change some settings etc. Can someone point me in the right direction please. example of the code in Main.cs myClass mainClass = new myClass(); In admin.cs I want to access the PacketSize property of the myClass instance that is created in the Main.cs Can this be done please say yes... Cliff.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Cliffordagius wrote:

      Can this be done please say yes...

      Yes. Have your class implement a custom interface, and cast the form to it's correct interface when you need to access the properties specific to the interface.

      Bastard Programmer from Hell :suss:

      1 Reply Last reply
      0
      • C Cliffordagius

        Hi, I have a winforms project with multiple forms that are children of a parent form using the MDI format. Now my main form which is the parent of all the others has a class instance created in the code to do some comms work. My issue is thatI want to be able to access some of the methods and properties of this instance from my other child forms to for example change some settings etc. Can someone point me in the right direction please. example of the code in Main.cs myClass mainClass = new myClass(); In admin.cs I want to access the PacketSize property of the myClass instance that is created in the Main.cs Can this be done please say yes... Cliff.

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        I say: yes Furthermore I suggest you choose, buy and study an introductory book on C#, it will teach you the basics of the language and relevant OO principles. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        1 Reply Last reply
        0
        • C Cliffordagius

          Hi, I have a winforms project with multiple forms that are children of a parent form using the MDI format. Now my main form which is the parent of all the others has a class instance created in the code to do some comms work. My issue is thatI want to be able to access some of the methods and properties of this instance from my other child forms to for example change some settings etc. Can someone point me in the right direction please. example of the code in Main.cs myClass mainClass = new myClass(); In admin.cs I want to access the PacketSize property of the myClass instance that is created in the Main.cs Can this be done please say yes... Cliff.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Declare you mainClass instance at the form level and expose the instance as a property.

          internal MyClass MyClass {
          get {
          return myClass;
          }
          }

          And from the child forms you can access this class like this:

          MainForm mainForm = this.MdiParent as MainForm;
          if (mainForm != null) {
          var packetSize = mainForm.MyClass.PacketSize;
          }

          B 1 Reply Last reply
          0
          • C Cliffordagius

            Hi, I have a winforms project with multiple forms that are children of a parent form using the MDI format. Now my main form which is the parent of all the others has a class instance created in the code to do some comms work. My issue is thatI want to be able to access some of the methods and properties of this instance from my other child forms to for example change some settings etc. Can someone point me in the right direction please. example of the code in Main.cs myClass mainClass = new myClass(); In admin.cs I want to access the PacketSize property of the myClass instance that is created in the Main.cs Can this be done please say yes... Cliff.

            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            Note: having the information about exactly where 'mainClass, and admin.cs instances are created would, imho, focus this question. This type of question involving exposing fields, methods, etc. across Form boundaries is one of the most frequently asked and answered ones on the C# QA forums. There are a variety of techniques you can use. 1. injection : have a public variable in admin.cs that holds the instance of mainClass: and when mainClass is created/instantiated: set that public variable in the instance of admin.cs. Note the requirements here: some entity (your main form's code ?) will have to have access to instances of both admin.cs and mainClass. 2. expose the instance of mainClass via a public static property in whatever Form it is created in; using a private 'setter, and a public 'getter is a common practice. 3. create a static class to function in the same way as #2, but, then, why do this, if you only need to expose one instance of one object ? However, if mainClass is instantiated in one semantic entity, and admin.cs is instantiated in another, then this strategy may be necessary. 4. raising events: not relevant here, I think. My own preference is to keep the "scope" of what is exposed as minimal as possible (which is I why I shudder in horror every time I see .NET code that injects the instance of an entire Form into another). I see this as being consistent with the principle of "separation of concerns." In this case, if the only data "admin.cs" must have is the 'PacketSize data from mainClass: then consider just exposing a static property, or variable of whatever Type PacketSize is in whatever Form creates the instance of mainClasss, and setting it immediately after mainClass is instantiated. Then admin.cs can access the property by something like mainClass.PacketSize. If PacketSize is something dynamic that's changing, then use a public static method in mainClass.cs, perhaps to access the current value from the instance of admin.cs. You'll note I have not mentioned interfaces here: I have a bias against using them in cases where the real use of them is only to expose limited access to data via casting to the interface. edit ... see response to Shameel's code above for an example of what this post talks about in code ... best, Bill

            "It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle

            1 Reply Last reply
            0
            • L Lost User

              Declare you mainClass instance at the form level and expose the instance as a property.

              internal MyClass MyClass {
              get {
              return myClass;
              }
              }

              And from the child forms you can access this class like this:

              MainForm mainForm = this.MdiParent as MainForm;
              if (mainForm != null) {
              var packetSize = mainForm.MyClass.PacketSize;
              }

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              Picky-picky: the use of identical names, for both Property Type, and Property Name, in the 'internal Property definition above ... uhh ... makes me nervous :) Yes, it will compile, but I think that then becomes ambiguous code that degrades future maintainability. Consider: we define 'MyClass this way:

              public class MyClass
              {
              // default 'ctor
              public MyClass() {}

              // 'ctor where PacketSize is passed in as a parameter
              public MyClass(int pSize)
              {
                  PacketSize = pSize;
              }
              
              public int PacketSize { get; set; }
              

              }

              Then, in the "main form:"

              private static MyClass myClassInstance1;

              public static int PacketSizeInMyClass1
              {
              get
              {
              return myClassInstance1.PacketSize;
              }
              }

              AdminForm adminForm = new AdminForm();

              private void MainForm_Load(object sender, EventArgs e)
              {
              myClassInstance1 = new MyClass(256);

              adminForm.Show();
              }

              Now, in the instance of 'AdminForm, we can directly access the PacketSize value in the instance of MyClass:

              int pSize = MainForm.PacketSizeInMyClass1;

              This is only one variation of many possible approaches to narrowing the scope of what is exposed, and eliminating complex look-ups (as in your example, where you have to re-create the instance of MainForm by de-referencing this.MDIParent every time you want to access the value of PacketSize). And, each scenario's requirements may demand different approaches: if, using the scenario above there is one and only one instance of MyClass ever created: then that class can be made static, and the code simplified futher. In the example here, we've gone to perhaps awkward lengths to leave MyClass a dynamic class that could have more than one instance created (although, obviously, we haven't shown any other instantiation and its use).

              "It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle

              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