How can access variable from different class?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Autostrad
    New Member
    • Oct 2009
    • 9

    How can access variable from different class?

    Below; I have 2 classes (forms), Namely, "PrintOneEmploy ee" and "DeleteOne" . In class "PrintOneEmploy ee";
    I want to declare a variable with name of "fileName" to contain the name of the file "Main\\_1Main.t xt".
    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.

    /*************** *************** *************** *************/
    Code:
    #pragma once
    #using <System.dll> 
    #using <System.Windows.Forms.dll>  
    #using <System.Drawing.dll>  
    
    #include "DeleteOne.h"
    
    #include "Time.h"
    #using <mscorlib.dll>
    
    using namespace System;
    using namespace System::IO;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    using namespace System::Drawing::Printing;  
    
    namespace TimeTracking 
    {
     
     public ref class PrintOneEmployee : public System::Windows::Forms::Form
     {
     
     
    
        private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
        {   
           
        { 
        streamToPrint = gcnew StreamReader(  "Main\\_1Main.txt" );
        
        String^ fileName = "Main\\_1Main.txt"String^ fileName;
        } 
        
          
        }
         
        
     };
    }
    /*************** *************** *************** *************** *********/

    Code:
    #pragma once
    
    #include "Time.h"
    //#include "PrintOneEmployee.h"
    #using <mscorlib.dll>
    
    using namespace System;
    using namespace System::IO;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    
    namespace TimeTracking 
    {
     
     public ref class DeleteOne : public System::Windows::Forms::Form
     {
     
    
       private: System::Void OkBotton_Click(System::Object^  sender, System::EventArgs^  e) 
        {
         
         FileInfo^ fi = gcnew FileInfo(FileName);
         fi->Delete();
         
        }
     
     };
    }






    --
    Thanks
    Allen
  • newb16
    Contributor
    • Jul 2008
    • 687

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

    Comment

    Working...