How can access varible from different class/

A

Allen

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

}

};
}
 
M

Mr. Arnold

Allen said:
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.

You create a public class/object where you define the variable with a public
property string FileName getter/setter.

You instantiate the public class/object as new somewhere during application
start-up.

You pass the public object in the class constructor where you want to use
the public object to set data in the public property of the public object or
get the data from the public property of the public object.

Object Oriented Programming or OOP is OOP no mater what language it is
that's using OOP.

<http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx>

A form is a class like any other class and a form can have a constructor too
where you pass things like an object into the class using its constructor so
that the object can be used in the class.


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4512 (20091015) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
A

Allen

Hi Arnold,
I appreciate your help. I believe the problem here is about inheritance.
Both derived classes were inheriting form the base class
"System::Windows::Forms::Form". I do not know the mechanism to access
textBox1->Text. Which is belongs to the base class "private:
System::Windows::Forms::TextBox^ textBox1;". The problem as I see it: If I
have textBoxe1 in 2 derived classes of the same project, how can I
differentiate between them? How can I let the compiler to know that
textBox1 belongs to either class, namely, "PrintOneEmployee" and DeleteOne.
If there was no inheritance I would do something like this:
"DeleteOne::textBox1->Text;" or "PrintOneEmployee::textBox1->Text; ".
 
F

Family Tree Mike

Allen said:
Hi Arnold,
I appreciate your help. I believe the problem here is about
inheritance. Both derived classes were inheriting form the base class
"System::Windows::Forms::Form". I do not know the mechanism to access
textBox1->Text. Which is belongs to the base class "private:
System::Windows::Forms::TextBox^ textBox1;". The problem as I see it:
If I have textBoxe1 in 2 derived classes of the same project, how can I
differentiate between them? How can I let the compiler to know that
textBox1 belongs to either class, namely, "PrintOneEmployee" and
DeleteOne. If there was no inheritance I would do something like this:
"DeleteOne::textBox1->Text;" or "PrintOneEmployee::textBox1->Text; ".

System.Windows.Forms.Form does not contain a textBox1, so I don't see
where the problem with inheritance is coming into play. TextBox happens
to be in the System.Windows.Forms "namespace", which is very different
than being a member of the System.Windows.Forms.Form base class.

Since it sounds like DeleteOne and PrintOneEmployee both derive from
System.Windows.Forms.Form, you have to give each instance a reference to
the other object. Then as you say in your last sentence, you can access
the properties of the other form, from each form.
 
M

Mr. Arnold

Allen said:
Hi Arnold,
I appreciate your help. I believe the problem here is about inheritance.
Both derived classes were inheriting form the base class
"System::Windows::Forms::Form". I do not know the mechanism to access
textBox1->Text. Which is belongs to the base class "private:
System::Windows::Forms::TextBox^ textBox1;". The problem as I see it: If
I have textBoxe1 in 2 derived classes of the same project, how can I
differentiate between them? How can I let the compiler to know that
textBox1 belongs to either class, namely, "PrintOneEmployee" and
DeleteOne. If there was no inheritance I would do something like this:
"DeleteOne::textBox1->Text;" or "PrintOneEmployee::textBox1->Text; ".

I don't think you understand inheritance. Yes, a form inherits from the
System base form and that's it. Each of your forms are objects that were
instantiated at some point, which means that both forms have properties and
methods which derive from the System base form. But they are two objects and
two distinct instances of an object deriving from System base form object.

Textbox1 is a control within in a given instance of a form1 or form2 object.
So, Textbox1 cannot be shared across objects in the way you're thinking.

The simple thing to do which I have already explained is to have a public
class/object that holds the information that can be shared between the two
form objects, passing the object in the form's constructor.

The other thing you can do is pass the form1 (an object) into form2 on
form2's constructor AS System::Windows::Forms::Form, since form1 derives
from System::Windows::Forms::Form, which is (inheritance).

However, if you take option2 passing the form object, then form1 should have
public property getter/setter so that the data can be accessed by another
object (form2).

http://www.devarticles.com/c/a/C-Sharp/Understanding-Properties-in-C-Sharp/




__________ Information from ESET NOD32 Antivirus, version of virus signature database 4519 (20091018) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top