Click to See Complete Forum and Search --> : How can access variable from different class?


autostrad
October 15th, 2009, 09:00 PM
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.



/**********************************************************/

#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";
}


}


};
}


/*********************************************************************/

#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();

}

};
}

darwen
October 16th, 2009, 10:05 AM
You're trying to run before you can walk. This is a fundamental principle of C++/C#/any object oriented language : member variables and accessing them from one class instance from another.

Can I recommend you getting a C++ book and try reading through that first - or learn C# instead of C++/CLI (because there's lots of tutorials on the net about C#) ?

Darwen.

autostrad
October 17th, 2009, 11:44 PM
Whatever you say; I still listen to you and will wait for someone who knows Visual programming better than me and may be better than you, too, to give some advices. I am walking (practicing C++) since 1997. Now, I am trying to run (practicing C++/CLI). The problem is I am trying to use GUI to rewrite the simple program that I wrote long time ago. Since, I started in learning C++ I read more than 7 books. When you come to visual the thing will start to look different. While concentrating in the hard stuff of visual part you may miss something of the simple primitive rules of C++. That the reason I am trying to see if someone can see what wrong I am doing. I do not want to learn C#, because I used to C++. I still thank you for replying.