Connecting Tech Pros Worldwide Forums | Help | Site Map

How can access variable from different class?

Newbie
 
Join Date: Oct 2009
Location: USA
Posts: 7
#1: Oct 16 '09
Below; I have 2 classes (forms), Namely, "PrintOneEmployee" and "DeleteOne". In class "PrintOneEmployee";
I want to declare a variable with name of "fileName" to contain the name of the file "Main\\_1Main.txt".
Later on, when I wanted to access the variable "fileName" in the other class, namely, "DeleteOne". The
compiler show an error that the variable is undeclared. What should I do to access the variable "fileName"
in the the other class "DeleteOne"? I am aware of the scope of the variable inside the brackets,
but I am confused about the scope of the different classes.

/************************************************** ********/
Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2. #using <System.dll> 
  3. #using <System.Windows.Forms.dll>  
  4. #using <System.Drawing.dll>  
  5.  
  6. #include "DeleteOne.h"
  7.  
  8. #include "Time.h"
  9. #using <mscorlib.dll>
  10.  
  11. using namespace System;
  12. using namespace System::IO;
  13. using namespace System::ComponentModel;
  14. using namespace System::Collections;
  15. using namespace System::Windows::Forms;
  16. using namespace System::Data;
  17. using namespace System::Drawing;
  18. using namespace System::Drawing::Printing;  
  19.  
  20. namespace TimeTracking 
  21. {
  22.  
  23.  public ref class PrintOneEmployee : public System::Windows::Forms::Form
  24.  {
  25.  
  26.  
  27.  
  28.     private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
  29.     {   
  30.  
  31.     { 
  32.     streamToPrint = gcnew StreamReader(  "Main\\_1Main.txt" );
  33.  
  34.     String^ fileName = "Main\\_1Main.txt"String^ fileName;
  35.     } 
  36.  
  37.  
  38.     }
  39.  
  40.  
  41.  };
  42. }
  43.  
  44.  
/************************************************** *******************/

Expand|Select|Wrap|Line Numbers
  1. #pragma once
  2.  
  3. #include "Time.h"
  4. //#include "PrintOneEmployee.h"
  5. #using <mscorlib.dll>
  6.  
  7. using namespace System;
  8. using namespace System::IO;
  9. using namespace System::ComponentModel;
  10. using namespace System::Collections;
  11. using namespace System::Windows::Forms;
  12. using namespace System::Data;
  13. using namespace System::Drawing;
  14.  
  15. namespace TimeTracking 
  16. {
  17.  
  18.  public ref class DeleteOne : public System::Windows::Forms::Form
  19.  {
  20.  
  21.  
  22.    private: System::Void OkBotton_Click(System::Object^  sender, System::EventArgs^  e) 
  23.     {
  24.  
  25.      FileInfo^ fi = gcnew FileInfo(FileName);
  26.      fi->Delete();
  27.  
  28.     }
  29.  
  30.  };
  31. }
  32.  
  33.  






--
Thanks
Allen

Needs Regular Fix
 
Join Date: Jul 2008
Posts: 385
#2: Oct 16 '09

re: How can access variable from different class?


1) They are not the same name, it's case-sensitive
2) if there are more than one instance of PrintOneEmployee, how will DeleteOne know which to use?
Then you can write some public getter to access class member.
Reply