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. Understanding application level variables and functions compared to C++

Understanding application level variables and functions compared to C++

Scheduled Pinned Locked Moved C#
csharpc++helptutorialquestion
6 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.
  • D Offline
    D Offline
    dazinith
    wrote on last edited by
    #1

    i am a newbie at c#, but have a good amount of time with vc++ development.. can someone help me with some questions? in vc++ my application could have variables and functions like:

    CCompanyStore* myCompanyStore;
    void ShowAboutDialog();

    and i would call them throughout the program like:

    CMyApp* pApp = (CMyApp*)AfxGetApp();
    pApp->ShowAboutDialog();

    i would use these types of functions for replacing views in my mainframe and such as well.. but i dont understand how to call a function from my base form without doing something stupid like calling this.Parent.Parent.SetStatusText("hello");.. is there an easy way to get a pointer or a handle onto the main form i have derived from System.Windows.Forms.Form to call its functions or get/set its variables? thanks in advance for any pointers! EDIT: just to clarify, i want to be able to call functions from anywhere in the program to set the status bar text, or to enable/disable a toolbar button, or to replace the form displayed in the background, etc.

    J S 2 Replies Last reply
    0
    • D dazinith

      i am a newbie at c#, but have a good amount of time with vc++ development.. can someone help me with some questions? in vc++ my application could have variables and functions like:

      CCompanyStore* myCompanyStore;
      void ShowAboutDialog();

      and i would call them throughout the program like:

      CMyApp* pApp = (CMyApp*)AfxGetApp();
      pApp->ShowAboutDialog();

      i would use these types of functions for replacing views in my mainframe and such as well.. but i dont understand how to call a function from my base form without doing something stupid like calling this.Parent.Parent.SetStatusText("hello");.. is there an easy way to get a pointer or a handle onto the main form i have derived from System.Windows.Forms.Form to call its functions or get/set its variables? thanks in advance for any pointers! EDIT: just to clarify, i want to be able to call functions from anywhere in the program to set the status bar text, or to enable/disable a toolbar button, or to replace the form displayed in the background, etc.

      J Offline
      J Offline
      Jim Stewart
      wrote on last edited by
      #2

      Remember that everything in C# is defined in a class. So, check out the static[^] keyword. I believe that is what you want. Here[^] is another reference.

      α.γεεκ

      Fortune passes everywhere.
      Duke Leto Atreides

      D 1 Reply Last reply
      0
      • D dazinith

        i am a newbie at c#, but have a good amount of time with vc++ development.. can someone help me with some questions? in vc++ my application could have variables and functions like:

        CCompanyStore* myCompanyStore;
        void ShowAboutDialog();

        and i would call them throughout the program like:

        CMyApp* pApp = (CMyApp*)AfxGetApp();
        pApp->ShowAboutDialog();

        i would use these types of functions for replacing views in my mainframe and such as well.. but i dont understand how to call a function from my base form without doing something stupid like calling this.Parent.Parent.SetStatusText("hello");.. is there an easy way to get a pointer or a handle onto the main form i have derived from System.Windows.Forms.Form to call its functions or get/set its variables? thanks in advance for any pointers! EDIT: just to clarify, i want to be able to call functions from anywhere in the program to set the status bar text, or to enable/disable a toolbar button, or to replace the form displayed in the background, etc.

        S Offline
        S Offline
        Stephane Rodriguez
        wrote on last edited by
        #3

        The equivalent of AfxGetApp is the .NET Application class, which holds the ApplicationContext and the MainForm. Unfortunately, the ApplicationContext member is not visible from your own code (it's protected).

        D 1 Reply Last reply
        0
        • S Stephane Rodriguez

          The equivalent of AfxGetApp is the .NET Application class, which holds the ApplicationContext and the MainForm. Unfortunately, the ApplicationContext member is not visible from your own code (it's protected).

          D Offline
          D Offline
          dazinith
          wrote on last edited by
          #4

          so is there another way to get a handle on the main form? still a newb.. cut me some slack :P -dz

          S 1 Reply Last reply
          0
          • J Jim Stewart

            Remember that everything in C# is defined in a class. So, check out the static[^] keyword. I believe that is what you want. Here[^] is another reference.

            α.γεεκ

            Fortune passes everywhere.
            Duke Leto Atreides

            D Offline
            D Offline
            dazinith
            wrote on last edited by
            #5

            i guess the static variables would work for maintaining application variables, but i still dont understand how i would access the variable of that class type if it is a member variable of my main form.. basicly if i have a function which sets what is being displayed in my main form i would want to be able to call that function from another form.. still a newb.. cut me some slack :P -dz

            1 Reply Last reply
            0
            • D dazinith

              so is there another way to get a handle on the main form? still a newb.. cut me some slack :P -dz

              S Offline
              S Offline
              Stephane Rodriguez
              wrote on last edited by
              #6

              dazinith wrote: so is there another way to get a handle on the main form? First of all, a form handle is a different thing. All forms expose the underlying WIN32 window handle through the this.Handle property, but that's a different matter. Beside navigating the parent hierarchy (as you pointed out in your original post), the most straight forward way to let a main form method be called from a child form is to pass a this reference to it when the child form is created. Passing this passes a reference to it, not a copy.

              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