Scope rules for class

     I've created a class called "RDAindividual" with get and set properties. I read data from my database, and create a new object "user". What is confusing me is that inside the "while", I can retrieve my data using the get property, but outside the while I cannot...... I get compile errors. I don't understand, I thought that dynamic variables created with "new" were saved in the heap and not garbage collected, or maybe I'm just using the scope resolution operator wrong. What is wrong?

              while(reader->Read())
            {
                  RDAindividual *user = new RDAindividual(
                        Double::Parse(reader->Item[S"item1"]->ToString()),
                  Double::Parse(reader->Item[S"item2"]->ToString()),
                  Double::Parse(reader->Item[S"item3"]->ToString());

                   MessageBox::Show(user->get_RDAItem2().ToString(), S"Test1");  // Inside "while" this works

            } // end while
            reader->Close();
                MessageBox::Show(RDAindividual::user->get_RDAItem2().ToString(), S"Test2"); // Outside "while" get compile  
                                                                                                                                     //  errors
KellyJensenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
Local variable defined inside of {} block is valid only in this block. Exactly like variable defined in the function is valid only in this function. Change this code by the following way:

          RDAindividual *user = null;

          while(reader->Read())
          {
               user = new RDAindividual(
                        Double::Parse(reader->Item[S"item1"]->ToString()),
               Double::Parse(reader->Item[S"item2"]->ToString()),
               Double::Parse(reader->Item[S"item3"]->ToString());

               MessageBox::Show(user->get_RDAItem2().ToString(), S"Test1");  // Inside "while" this works

          } // end while
          reader->Close();

          if ( user != null )    // precaution: recordset may be empty
              MessageBox::Show(user->get_RDAItem2().ToString(), S"Test2");
0
KellyJensenAuthor Commented:
Thank you, I understand better now. I would really like to know how to make my " *user " pointer global, so that I have access to it throughout my program, not just in the form I am currently using. I have a "Global.h" file where I have some integers defined, and then then use "extern int variable_name;" to tell the compiler that the integers are defined elsewhere. Would it be possible to do the same with an object such as " *user "?
0
AlexFMCommented:
Yes, you can do this. Global variable should be declared in cpp file, not inside of function:

// SomeFile.cpp
#inclide "RDAindividual.h>    // ensure that type is known to compiler

RDAindividual *user;

You can put line:

extern RDAindividual *user;

to Global.h. In this case global variable user may be used in any place where Global.h is included.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KellyJensenAuthor Commented:
Thank you very much!! You are Gandalf!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.