Windows Form Application Variable Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kief12
    New Member
    • Jul 2007
    • 13

    Windows Form Application Variable Question

    I have a class called User. I want to make a User object called myUser that can be access able to both windows forms that i have create. I have main.cpp which calls the creation of the first form
    Code:
    Application::Run(gcnew Form1());
    but i can't seem to access the Form1 object it creates. I also tried making a User object in the forms but i got this error
    Code:
     	private: User myUser;
    Code:
      error C4368: cannot define 'myUser' as a member of managed 'myProject::w_Name': mixed types are not supported
    How do i define variables in the visual c++ forms and execute functions outside of their headers.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This looks like C#.

    Moving this to the .NET forum.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Originally posted by weaknessforcats
      This looks like C#.

      Moving this to the .NET forum.
      It's C++, but probably .NET constructs

      Comment

      • kief12
        New Member
        • Jul 2007
        • 13

        #4
        Originally posted by Plater
        It's C++, but probably .NET constructs
        Yeah i am using c++ this is my first time using a gui for c++ so i am lost and confused on what to use. I follow a guide that had me using the windows form application in visual c++. Which brought me here

        Comment

        • TRScheel
          Recognized Expert Contributor
          • Apr 2007
          • 638

          #5
          Originally posted by kief12
          I have a class called User. I want to make a User object called myUser that can be access able to both windows forms that i have create. I have main.cpp which calls the creation of the first form
          Code:
          Application::Run(gcnew Form1());
          but i can't seem to access the Form1 object it creates. I also tried making a User object in the forms but i got this error
          Code:
           	private: User myUser;
          Code:
            error C4368: cannot define 'myUser' as a member of managed 'myProject::w_Name': mixed types are not supported
          How do i define variables in the visual c++ forms and execute functions outside of their headers.

          Try

          [code=cpp]
          private:
          User ^myUser;
          [/code]

          Comment

          • kief12
            New Member
            • Jul 2007
            • 13

            #6
            Originally posted by TRScheel
            Try

            [code=cpp]
            private:
            User ^myUser;
            [/code]
            Private: User *myUSer;

            Worked thank you

            If i wanted to access myUser in the main window of my application from the main function how would i do it?

            Comment

            • TRScheel
              Recognized Expert Contributor
              • Apr 2007
              • 638

              #7
              Originally posted by kief12
              Private: User *myUSer;

              Worked thank you

              If i wanted to access myUser in the main window of my application from the main function how would i do it?

              That is declaring a native pointer, where as with ^ it is within the garbage collection.

              Comment

              • TRScheel
                Recognized Expert Contributor
                • Apr 2007
                • 638

                #8
                One of my favorite books has this table describing this

                Operator:
                Stack / Native Heap
                Managed Heap

                Address:
                &
                %

                Pointer declaration:
                *
                ^

                Dereference:
                *
                *

                Example:
                Static/Native Heap
                value struct V{ int i; };
                V v;
                V * pV = &v; v.i;
                pV->i;

                Managed Heap
                ref struct R{ int i; };
                R ^r1 = gcnew R();
                R ^r2 = %(*r1);
                R r;
                R ^r3 = %r;
                r1->i;
                (*r2).i;

                Comment

                • kief12
                  New Member
                  • Jul 2007
                  • 13

                  #9
                  Originally posted by TRScheel
                  That is declaring a native pointer, where as with ^ it is within the garbage collection.
                  Code:
                  w_Name.h(36) : error C3699: '^' : cannot use this indirection on type 'User'
                          compiler replacing '^' with '*' to continue parsing'
                  Will not let me use ^

                  Comment

                  • TRScheel
                    Recognized Expert Contributor
                    • Apr 2007
                    • 638

                    #10
                    Originally posted by kief12
                    Code:
                    w_Name.h(36) : error C3699: '^' : cannot use this indirection on type 'User'
                            compiler replacing '^' with '*' to continue parsing'
                    Will not let me use ^
                    How is User declared?

                    Comment

                    • TRScheel
                      Recognized Expert Contributor
                      • Apr 2007
                      • 638

                      #11
                      Originally posted by TRScheel
                      How is User declared?
                      [CODE=cpp]public class User { .. };[/CODE]

                      will not work.

                      You need either

                      [CODE=cpp]public value class User { ... };[/CODE]

                      or

                      [CODE=cpp]public ref class User { .. };[/CODE]

                      Comment

                      • kief12
                        New Member
                        • Jul 2007
                        • 13

                        #12
                        if the main form window is created using Application::Ru n(gcnew Form1()); Who would i run the function lets say myRandomFunctio n() in Form1.h from a different window or the
                        Code:
                        int main(array<System::String ^> ^args)

                        Comment

                        • TRScheel
                          Recognized Expert Contributor
                          • Apr 2007
                          • 638

                          #13
                          Originally posted by kief12
                          if the main form window is created using Application::Ru n(gcnew Form1()); Who would i run the function lets say myRandomFunctio n() in Form1.h from a different window or the
                          Code:
                          int main(array<System::String ^> ^args)

                          [code=cpp]
                          public ref class Form1
                          {
                          private:
                          ...
                          public:
                          Form1();
                          myRandomFunctio n();
                          };
                          [/code]

                          Declaring it like that should make it accessible else where.

                          Comment

                          • sawpit
                            New Member
                            • Aug 2007
                            • 2

                            #14
                            Originally posted by TRScheel
                            [code=cpp]
                            public ref class Form1
                            {
                            private:
                            ...
                            public:
                            Form1();
                            myRandomFunctio n();
                            };
                            [/code]

                            Declaring it like that should make it accessible else where.
                            A newbee here. Can you give an example of the code that would call myRandomFunctio n()? How do you get a reference to Form1 elsewhere, given that the form is gcnew'd directly to Run()? Thanks.

                            Comment

                            • TRScheel
                              Recognized Expert Contributor
                              • Apr 2007
                              • 638

                              #15
                              Originally posted by sawpit
                              A newbee here. Can you give an example of the code that would call myRandomFunctio n()? How do you get a reference to Form1 elsewhere, given that the form is gcnew'd directly to Run()? Thanks.
                              Dont gcnew it in the Run function, thats the only way that I know of. Although, someone might be able to extract it from the Application object.

                              Comment

                              Working...