467,211 Members | 1,205 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,211 developers. It's quick & easy.

How can access variable from different class?

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
Oct 16 '09 #1
  • viewed: 2549
Share:
1 Reply
512MB
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.
Oct 16 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

166 posts views Thread by Graham | last post: by
7 posts views Thread by James Fortune | last post: by
6 posts views Thread by Peter Frost | last post: by
4 posts views Thread by lars.uffmann@rwth-aachen.de | last post: by
4 posts views Thread by RedHair | last post: by
9 posts views Thread by LamSoft | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.