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. Managed C++/CLI
  4. How to implement a global variable if C++/CLI doesn't support it at all?

How to implement a global variable if C++/CLI doesn't support it at all?

Scheduled Pinned Locked Moved Managed C++/CLI
c++helptutorialquestionlearning
3 Posts 2 Posters 11 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.
  • M Offline
    M Offline
    Member_15150778
    wrote on last edited by
    #1

    I'm trying to build a really simple class with member variables and member functions for learning. I am trying to achieve the following code inside btn_User1_Click, however since user1 is not globally declared, it couldn't find the variable. At the same time, if I tried to declare this myUser^ user1 = gcnew user1 (1002); outside the function , it gives an error syntax error missing ';' before '^'

    myUser.h
    #pragma once
    ref class myUser
    {

    public:
    myUser(unsigned int user_id) :id(user_id) {

    ...

    }
    

    private:
    unsigned int id{ 0 };

    public:

    System::Void increment\_UserID() {
    

    id = id + 1;

    }
    

    };

    MyForm.cpp

    namespace FACEDControllerGUI {

    System::Void MyForm::MyForm\_Load(System::Object^ sender, System::EventArgs^ e) {
    
        myUser^ user1 = gcnew user1 (1002);
        myUser^ user2 = gcnew user1 (1005);
    
    }
    
    System::Void MyForm::btn\_User1\_Click(System::Object^ sender, System::EventArgs^ e) {
    	user1->increment\_UserID();
    }
    
    System::Void MyForm::btn\_User2\_Click(System::Object^ sender, System::EventArgs^ e) {
    	user2->increment\_UserID();
    }
    

    } // end namespace

    L 1 Reply Last reply
    0
    • M Member_15150778

      I'm trying to build a really simple class with member variables and member functions for learning. I am trying to achieve the following code inside btn_User1_Click, however since user1 is not globally declared, it couldn't find the variable. At the same time, if I tried to declare this myUser^ user1 = gcnew user1 (1002); outside the function , it gives an error syntax error missing ';' before '^'

      myUser.h
      #pragma once
      ref class myUser
      {

      public:
      myUser(unsigned int user_id) :id(user_id) {

      ...

      }
      

      private:
      unsigned int id{ 0 };

      public:

      System::Void increment\_UserID() {
      

      id = id + 1;

      }
      

      };

      MyForm.cpp

      namespace FACEDControllerGUI {

      System::Void MyForm::MyForm\_Load(System::Object^ sender, System::EventArgs^ e) {
      
          myUser^ user1 = gcnew user1 (1002);
          myUser^ user2 = gcnew user1 (1005);
      
      }
      
      System::Void MyForm::btn\_User1\_Click(System::Object^ sender, System::EventArgs^ e) {
      	user1->increment\_UserID();
      }
      
      System::Void MyForm::btn\_User2\_Click(System::Object^ sender, System::EventArgs^ e) {
      	user2->increment\_UserID();
      }
      

      } // end namespace

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

      You are creating user1 and user2 inside the MyForm_Load method, so as soon as that method ends the two variables disappear. You should create them in the class definition of MyForm so they are available to all methods of the class.

      M 1 Reply Last reply
      0
      • L Lost User

        You are creating user1 and user2 inside the MyForm_Load method, so as soon as that method ends the two variables disappear. You should create them in the class definition of MyForm so they are available to all methods of the class.

        M Offline
        M Offline
        Member_15150778
        wrote on last edited by
        #3

        I have mentioned this following: At the same time, if I tried to declare this myUser^ user1 = gcnew user1 (1002); outside the function , it gives an error syntax error missing ';' before '^' Fortunately, I have solved my problem. It is due to #include function of the class is placed in the .cpp file, instead of the .h file

        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