title: Passing classes/objects/data? between two forms
body: <p>I am currently working on a C++ project which requires me to pass data from one form to the other. In this case, the user will create an account on CreateAccount Form (<code>Create_Account.h</code>) with a username and password. This username and password will be stored as private attributes of a object (lets call it <code>user</code>) and he will proceed to a Login Form (<code>Login.h</code>). However, I cannot get the system to verify the entered password on this form with the earlier one. I have sort of figure out that I have to pass the object(<code>user</code>) to this login form so that I can verify the username and password entered with the private attributes of the object(<code>user</code>). However, I'm not very sure of the syntax on how to pass in objects to forms. (I'm only familiar with passing objects through functions)  </p>
  
  <p>I am new to this language and will really appreciate help on it. </p>
  
  <p>Below is the code which I have written.</p>
  
  <pre><code>**//CreateAccount.h**
  
  #pragma once
  #include "user2.h"
  #include &lt;stdlib.h&gt;
  #include "ExpenseTracker.h"
  #include "User_Login.h"
  #include &lt;string&gt;
  #include &lt;msclr/marshal_cppstd.h&gt;
  
  namespace EzXpns2 {
  
      using namespace System;
      using namespace System::ComponentModel;
      using namespace System::Collections;
      using namespace System::Windows::Forms;
      using namespace System::Data;
      using namespace System::Drawing;
  
      /// &lt;summary&gt;
      /// Summary for Create_Account
      /// &lt;/summary&gt;
      public ref class Create_Account : public System::Windows::Forms::Form
      {
      public:
          Create_Account(void)
          {
              InitializeComponent();
              //
              //TODO: Add the constructor code here
              //
          }
  
      protected:
          /// &lt;summary&gt;
          /// Clean up any resources being used.
          /// &lt;/summary&gt;
          ~Create_Account()
          {
              if (components)
              {
                  delete components;
              }
          }
      private: System::Windows::Forms::Button^  btnCreateNewAccount;
      protected: 
  
      protected: 
      private: System::Windows::Forms::TextBox^  txtBxPassword;
      private: System::Windows::Forms::Label^  lblPassword;
      private: System::Windows::Forms::TextBox^  txtBxUsername;
      private: System::Windows::Forms::Label^  lblUsername;
      private: System::Windows::Forms::TextBox^  txtBxReenterPassword;
  
      private: System::Windows::Forms::Label^  label1;
      //private: System::Windows::Forms:User_Login^ userlogin; 
      //private User_Login^ UserLogin;
  
  
      private:
          /// &lt;summary&gt;
          /// Required designer variable.
          /// &lt;/summary&gt;
          System::ComponentModel::Container ^components;
  
  #pragma region Windows Form Designer generated code
          /// &lt;summary&gt;
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          void InitializeComponent(void)
          {
              this-&gt;btnCreateNewAccount = (gcnew System::Windows::Forms::Button());
              this-&gt;txtBxPassword = (gcnew System::Windows::Forms::TextBox());
              this-&gt;lblPassword = (gcnew System::Windows::Forms::Label());
              this-&gt;txtBxUsername = (gcnew System::Windows::Forms::TextBox());
              this-&gt;lblUsername = (gcnew System::Windows::Forms::Label());
              this-&gt;txtBxReenterPassword = (gcnew System::Windows::Forms::TextBox());
              this-&gt;label1 = (gcnew System::Windows::Forms::Label());
              this-&gt;SuspendLayout();
              // 
              // btnCreateNewAccount
              // 
              this-&gt;btnCreateNewAccount-&gt;Location = System::Drawing::Point(147, 147);
              this-&gt;btnCreateNewAccount-&gt;Name = L"btnCreateNewAccount";
              this-&gt;btnCreateNewAccount-&gt;Size = System::Drawing::Size(126, 23);
              this-&gt;btnCreateNewAccount-&gt;TabIndex = 3;
              this-&gt;btnCreateNewAccount-&gt;Text = L"Create New Account";
              this-&gt;btnCreateNewAccount-&gt;UseVisualStyleBackColor = true;
              this-&gt;btnCreateNewAccount-&gt;Click += gcnew System::EventHandler(this, &amp;Create_Account::btnCreateNewAccount_Click);
              // 
              // txtBxPassword
              // 
              this-&gt;txtBxPassword-&gt;Location = System::Drawing::Point(147, 68);
              this-&gt;txtBxPassword-&gt;Name = L"txtBxPassword";
              this-&gt;txtBxPassword-&gt;Size = System::Drawing::Size(126, 20);
              this-&gt;txtBxPassword-&gt;TabIndex = 1;
              this-&gt;txtBxPassword-&gt;UseSystemPasswordChar = true;
              // 
              // lblPassword
              // 
              this-&gt;lblPassword-&gt;AutoSize = true;
              this-&gt;lblPassword-&gt;Location = System::Drawing::Point(26, 73);
              this-&gt;lblPassword-&gt;Name = L"lblPassword";
              this-&gt;lblPassword-&gt;Size = System::Drawing::Size(56, 13);
              this-&gt;lblPassword-&gt;TabIndex = 12;
              this-&gt;lblPassword-&gt;Text = L"Password:";
              // 
              // txtBxUsername
              // 
              this-&gt;txtBxUsername-&gt;Location = System::Drawing::Point(149, 27);
              this-&gt;txtBxUsername-&gt;Name = L"txtBxUsername";
              this-&gt;txtBxUsername-&gt;Size = System::Drawing::Size(126, 20);
              this-&gt;txtBxUsername-&gt;TabIndex = 0;
              // 
              // lblUsername
              // 
              this-&gt;lblUsername-&gt;AutoSize = true;
              this-&gt;lblUsername-&gt;Location = System::Drawing::Point(24, 29);
              this-&gt;lblUsername-&gt;Name = L"lblUsername";
              this-&gt;lblUsername-&gt;Size = System::Drawing::Size(86, 13);
              this-&gt;lblUsername-&gt;TabIndex = 10;
              this-&gt;lblUsername-&gt;Text = L"Enter Username:";
              // 
              // txtBxReenterPassword
              // 
              this-&gt;txtBxReenterPassword-&gt;Location = System::Drawing::Point(147, 100);
              this-&gt;txtBxReenterPassword-&gt;Name = L"txtBxReenterPassword";
              this-&gt;txtBxReenterPassword-&gt;Size = System::Drawing::Size(126, 20);
              this-&gt;txtBxReenterPassword-&gt;TabIndex = 2;
              this-&gt;txtBxReenterPassword-&gt;UseSystemPasswordChar = true;
              // 
              // label1
              // 
              this-&gt;label1-&gt;AutoSize = true;
              this-&gt;label1-&gt;Location = System::Drawing::Point(26, 105);
              this-&gt;label1-&gt;Name = L"label1";
              this-&gt;label1-&gt;Size = System::Drawing::Size(100, 13);
              this-&gt;label1-&gt;TabIndex = 15;
              this-&gt;label1-&gt;Text = L"Re-enter Password:";
              // 
              // Create_Account
              // 
              this-&gt;AutoScaleDimensions = System::Drawing::SizeF(6, 13);
              this-&gt;AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
              this-&gt;ClientSize = System::Drawing::Size(309, 188);
              this-&gt;Controls-&gt;Add(this-&gt;txtBxReenterPassword);
              this-&gt;Controls-&gt;Add(this-&gt;label1);
              this-&gt;Controls-&gt;Add(this-&gt;btnCreateNewAccount);
              this-&gt;Controls-&gt;Add(this-&gt;txtBxPassword);
              this-&gt;Controls-&gt;Add(this-&gt;lblPassword);
              this-&gt;Controls-&gt;Add(this-&gt;txtBxUsername);
              this-&gt;Controls-&gt;Add(this-&gt;lblUsername);
              this-&gt;Name = L"Create_Account";
              this-&gt;Text = L"Welcome to EzXpns -Create Account";
              this-&gt;ResumeLayout(false);
              this-&gt;PerformLayout();
  
          }
  #pragma endregion
  
  private: System::Void btnCreateNewAccount_Click(System::Object^  sender, System::EventArgs^  e) {
  
                   ExpenseTracker *myTracker  = new ExpenseTracker();
                   string strUsername, strPassword, strReenterPassword; 
                   bool checkPassword = false;
  
                   System::String^ str1 = txtBxUsername-&gt;Text;
                   strUsername = marshal_as&lt;string&gt;(str1);
                   //convert system string to string
  
                   System::String^ str2 = txtBxPassword-&gt;Text;    
                   strPassword = marshal_as&lt;string&gt;(str2);
  
                   System::String^ str3 = txtBxReenterPassword-&gt;Text; 
                   strReenterPassword = marshal_as&lt;string&gt;(str3);
  
                   if (strPassword  == strReenterPassword) 
                       checkPassword = true;
  
                   if (checkPassword)
                   {
                       User *newUser = new User(strUsername, strReenterPassword);
                       (*myTracker).addUser(newUser);
                       this-&gt;Hide();
                       User_Login^ testDialog = gcnew User_Login;
                       testDialog-&gt;Show();
                   }
                   else
                      MessageBox::Show("Please re-enter your password.\n\nThe password you entered do not match.\nPlease try again.",
           "Create User Failure", MessageBoxButtons::OK, MessageBoxIcon::Warning);
           }
  };
  }
  
  //UserLogin.h
  #pragma once
  #include "user2.h"
  #include &lt;stdlib.h&gt;
  #include "ExpenseTracker.h"
  #include "Create_Account.h"
  #include "Login.h"
  #include &lt;string&gt;
  #include &lt;msclr/marshal_cppstd.h&gt;
  
  namespace EzXpns2 {
  
      using namespace System;
      using namespace System::ComponentModel;
      using namespace System::Collections;
      using namespace System::Windows::Forms;
      using namespace System::Data;
      using namespace System::Drawing;
      using namespace msclr::interop;
  
      /// &lt;summary&gt;
      /// Summary for User_Login
      /// &lt;/summary&gt;
      public ref class User_Login : public System::Windows::Forms::Form
      {
      public:
          User_Login(void)//ExpenseTracker *myPtrTracker)
          {
              InitializeComponent();
              //
              //TODO: Add the constructor code here
              //
              //CreateAccountForm = CreateAcct;
              //ExpenseTracker *myTracker = myPtrTracker;
          }
  
      protected:
          /// &lt;summary&gt;
          /// Clean up any resources being used.
          /// &lt;/summary&gt;
          ~User_Login()
          {
              if (components)
              {
                  delete components;
              }
          }
      private: System::Windows::Forms::Button^  btnLogin;
      protected: 
      private: System::Windows::Forms::TextBox^  txtBxPassword;
      private: System::Windows::Forms::Label^  lblPassword;
      private: System::Windows::Forms::TextBox^  txtBxUsername;
      private: System::Windows::Forms::Label^  lblUsername;
      private: System::Windows::Forms::Form^ CreateAccountForm;
  
  
      private:
          /// &lt;summary&gt;
          /// Required designer variable.
          /// &lt;/summary&gt;
          System::ComponentModel::Container ^components;
  
  #pragma region Windows Form Designer generated code
          /// &lt;summary&gt;
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          void InitializeComponent(void)
          {
              this-&gt;btnLogin = (gcnew System::Windows::Forms::Button());
              this-&gt;txtBxPassword = (gcnew System::Windows::Forms::TextBox());
              this-&gt;lblPassword = (gcnew System::Windows::Forms::Label());
              this-&gt;txtBxUsername = (gcnew System::Windows::Forms::TextBox());
              this-&gt;lblUsername = (gcnew System::Windows::Forms::Label());
              this-&gt;SuspendLayout();
              // 
              // btnLogin
              // 
              this-&gt;btnLogin-&gt;Location = System::Drawing::Point(80, 110);
              this-&gt;btnLogin-&gt;Name = L"btnLogin";
              this-&gt;btnLogin-&gt;Size = System::Drawing::Size(126, 23);
              this-&gt;btnLogin-&gt;TabIndex = 9;
              this-&gt;btnLogin-&gt;Text = L"Login";
              this-&gt;btnLogin-&gt;UseVisualStyleBackColor = true;
              this-&gt;btnLogin-&gt;Click += gcnew System::EventHandler(this, &amp;User_Login::btnLogin_Click);
              // 
              // txtBxPassword
              // 
              this-&gt;txtBxPassword-&gt;Location = System::Drawing::Point(80, 65);
              this-&gt;txtBxPassword-&gt;Name = L"txtBxPassword";
              this-&gt;txtBxPassword-&gt;Size = System::Drawing::Size(126, 20);
              this-&gt;txtBxPassword-&gt;TabIndex = 8;
              this-&gt;txtBxPassword-&gt;UseSystemPasswordChar = true;
              this-&gt;txtBxPassword-&gt;TextChanged += gcnew System::EventHandler(this, &amp;User_Login::txtBxPassword_TextChanged);
              // 
              // lblPassword
              // 
              this-&gt;lblPassword-&gt;AutoSize = true;
              this-&gt;lblPassword-&gt;Location = System::Drawing::Point(20, 68);
              this-&gt;lblPassword-&gt;Name = L"lblPassword";
              this-&gt;lblPassword-&gt;Size = System::Drawing::Size(56, 13);
              this-&gt;lblPassword-&gt;TabIndex = 7;
              this-&gt;lblPassword-&gt;Text = L"Password:";
              this-&gt;lblPassword-&gt;Click += gcnew System::EventHandler(this, &amp;User_Login::lblPassword_Click);
              // 
              // txtBxUsername
              // 
              this-&gt;txtBxUsername-&gt;Location = System::Drawing::Point(82, 24);
              this-&gt;txtBxUsername-&gt;Name = L"txtBxUsername";
              this-&gt;txtBxUsername-&gt;Size = System::Drawing::Size(126, 20);
              this-&gt;txtBxUsername-&gt;TabIndex = 6;
              this-&gt;txtBxUsername-&gt;TextChanged += gcnew System::EventHandler(this, &amp;User_Login::txtBxUsername_TextChanged);
              // 
              // lblUsername
              // 
              this-&gt;lblUsername-&gt;AutoSize = true;
              this-&gt;lblUsername-&gt;Location = System::Drawing::Point(18, 24);
              this-&gt;lblUsername-&gt;Name = L"lblUsername";
              this-&gt;lblUsername-&gt;Size = System::Drawing::Size(58, 13);
              this-&gt;lblUsername-&gt;TabIndex = 5;
              this-&gt;lblUsername-&gt;Text = L"Username:";
              this-&gt;lblUsername-&gt;Click += gcnew System::EventHandler(this, &amp;User_Login::lblUsername_Click);
              // 
              // User_Login
              // 
              this-&gt;AutoScaleDimensions = System::Drawing::SizeF(6, 13);
              this-&gt;AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
              this-&gt;ClientSize = System::Drawing::Size(231, 155);
              this-&gt;Controls-&gt;Add(this-&gt;btnLogin);
              this-&gt;Controls-&gt;Add(this-&gt;txtBxPassword);
              this-&gt;Controls-&gt;Add(this-&gt;lblPassword);
              this-&gt;Controls-&gt;Add(this-&gt;txtBxUsername);
              this-&gt;Controls-&gt;Add(this-&gt;lblUsername);
              this-&gt;Name = L"User_Login";
              this-&gt;Text = L"Welcome to EzXpns";
              this-&gt;Load += gcnew System::EventHandler(this, &amp;User_Login::User_Login_Load);
              this-&gt;ResumeLayout(false);
              this-&gt;PerformLayout();
  
          }
  #pragma endregion
      private: System::Void btnLogin_Click(System::Object^  sender, System::EventArgs^  e) {
  
                   //User myUser;
                   //ExpenseTracker myTracker;
  
                   System::String^ str1 = txtBxUsername-&gt;Text;
                   string strUsername = marshal_as&lt;string&gt;(str1);
                   //convert system string to string
  
                   System::String^ str2 = txtBxPassword-&gt;Text;    
                   string strPassword = marshal_as&lt;string&gt;(str2);
                   bool success = false;
                   success = (*myTracker).login(strUsername, strPassword);
                   if (success)
                   {
                          this-&gt;Hide();
                          Login^ testDialog = gcnew Login;
                          testDialog-&gt;Show();
                   }
                   else
                   {
                        MessageBox::Show("Please re-enter your password.\n\nThe password you entered is incorrect.\nPlease try again (make sure your caps lock is off).",
           "Login Failure", MessageBoxButtons::OK, MessageBoxIcon::Warning);
                   }
               }
  private: System::Void lblUsername_Click(System::Object^  sender, System::EventArgs^  e) {
               }
  private: System::Void lblPassword_Click(System::Object^  sender, System::EventArgs^  e) {
               }
  private: System::Void txtBxPassword_TextChanged(System::Object^  sender, System::EventArgs^  e) {
               }
  private: System::Void txtBxUsername_TextChanged(System::Object^  sender, System::EventArgs^  e) {
               }
  private: System::Void User_Login_Load(System::Object^  sender, System::EventArgs^  e) {
  
           }
  };
  }
  
  //ExpenseTracker.h 
  #ifndef _EXPENSETRACKER_H
  #define _EXPENSETRACKER_H
  
  #include &lt;string&gt;
  #include &lt;vector&gt;
  //#include &lt;iostream&gt;
  #include "user2.h"
  
  using namespace std;
  
  class ExpenseTracker
  {
  private:
      vector&lt;User*&gt; allMyUsers;
  
  public:
      ExpenseTracker(); //empty constructor
      void addUser(User*);
      int findUser(string);
      bool login(string, string);
  };
  
  #endif;
  </code></pre>