• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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
0
KellyJensen
Asked:
KellyJensen
  • 2
  • 2
1 Solution
 
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
 
KellyJensenAuthor Commented:
Thank you very much!! You are Gandalf!!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now