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. need reference to parent object

need reference to parent object

Scheduled Pinned Locked Moved C#
c++helptutorialquestion
3 Posts 3 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.
  • B Offline
    B Offline
    BungoMan85
    wrote on last edited by
    #1

    I have a Windows Form that creates an instance of a class of my own, and within that object I need to call a function that is part of the Windows Form. The problem is I don't know how to give my class a reference to my Windows Form. If this was C++ I'd just pass a pointer through the constructor... but this isn't C++... Anyone know the proper way to get a reference to a parent object so its methods can be called? Bungo!

    A J 2 Replies Last reply
    0
    • B BungoMan85

      I have a Windows Form that creates an instance of a class of my own, and within that object I need to call a function that is part of the Windows Form. The problem is I don't know how to give my class a reference to my Windows Form. If this was C++ I'd just pass a pointer through the constructor... but this isn't C++... Anyone know the proper way to get a reference to a parent object so its methods can be called? Bungo!

      A Offline
      A Offline
      Alomgir Miah
      wrote on last edited by
      #2

      You can always pass the reference to the parent object and call methods of it. The reference is just like pointers in c++. Live Life King Size Alomgir Miah

      1 Reply Last reply
      0
      • B BungoMan85

        I have a Windows Form that creates an instance of a class of my own, and within that object I need to call a function that is part of the Windows Form. The problem is I don't know how to give my class a reference to my Windows Form. If this was C++ I'd just pass a pointer through the constructor... but this isn't C++... Anyone know the proper way to get a reference to a parent object so its methods can be called? Bungo!

        J Offline
        J Offline
        Josh Lindenmuth
        wrote on last edited by
        #3

        You could just pass the form to the class during instantiation using 'this' keyword. For example, assume your form is called MyForm and has a function called MyFormFunction that you want to call from MyClass: during instantiation in MyForm: MyClass newClass = new MyClass(this); in MyClass: private MyForm _externalForm; //declare as class level private variable //ctor public MyClass(MyForm passedFormReference) { _externalForm = passedFormReference; } public MyFormFunction() { _externalForm.MyFormFunction(); } Hope this helps, Regards, ----- Josh Lindenmuth jlindenmuth at paycepayroll dot com Payce Payroll - Smart for Business

        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