PC Review


Reply
Thread Tools Rate Thread

How can access varible from different class/

 
 
Allen
Guest
Posts: n/a
 
      16th Oct 2009
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:ata;
using namespace System:rawing;
using namespace System:rawing::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:ata;
using namespace System:rawing;

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

 
Reply With Quote
 
 
 
 
Mr. Arnold
Guest
Posts: n/a
 
      16th Oct 2009

"Allen" <(E-Mail Removed)> wrote in message
news:E55D804B-E9EB-4A24-B7B2-(E-Mail Removed)...
> 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



 
Reply With Quote
 
Allen
Guest
Posts: n/a
 
      18th Oct 2009
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; ".

--
Thanks
Allen
"Mr. Arnold" <MR. (E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "Allen" <(E-Mail Removed)> wrote in message
> news:E55D804B-E9EB-4A24-B7B2-(E-Mail Removed)...
>> 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
>
>
>


 
Reply With Quote
 
Family Tree Mike
Guest
Posts: n/a
 
      18th Oct 2009
Allen wrote:
> 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.

--
Mike
 
Reply With Quote
 
Mr. Arnold
Guest
Posts: n/a
 
      18th Oct 2009

"Allen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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-Sha...es-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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Access outer class members from a nested class Matthew Ireland Microsoft C# .NET 4 15th Oct 2009 06:00 AM
derived class can not access base class protected member? =?Utf-8?B?R2Vvcmdl?= Microsoft VC .NET 15 26th Oct 2007 10:23 AM
Restricting access of class variable to another single class? Danny Tuppeny Microsoft C# .NET 6 1st Dec 2005 08:35 AM
Restricting access of class variable to another single class? Danny Tuppeny Microsoft Dot NET 4 1st Dec 2005 08:34 AM
Access DataGrid declared in derived class from base class?? Jonas Microsoft ASP .NET 1 12th Aug 2003 05:13 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:05 PM.