PC Review


Reply
Thread Tools Rate Thread

How can access a variable from one class anduse in another

 
 
Allen
Guest
Posts: n/a
 
      10th Sep 2009

I am making a program with 3 forms(classes). 1)Form1, 2) Employee, and 3)
EmployeeInOu. I am using MS VS 2008.

They all inherit from “System::Windows::Forms::Form”.
My problem is I want to access variable “temp”, which belongs to EmployeeID
class, in a different class which is EmployeeInOu.
Below are some skeleton codes to show the 3 classes. I hope someone can
help.

//Class(1):
public __gc class Form1 : public System::Windows::Forms::From {
public:
Form1(void)
private: System::Void EmpMenuItem_Click(System::Object * sender,
System::EventArgs * e)
{
//Create
EmployeeID* box = new EmployeeID();

//Fill
box->EmpNum = S""; //set is used

//Show
if (box->ShowDialog() == DialogResult::OK)
{
box->EmpNum; //get is used

}
}
};

//Class (2):
public __gc class EmployeeID : public System::Windows::Forms::Form
{
public:

private: System::Windows::Forms::TextBox * empNumBox;

public:
__property void set_EmpNum(String* p){empNumBox->Text = p;}
__property String* get_EmpNum() {return empNumBox->Text;}
private: System::Void OKBtn_Click(System::Object * sender,
System::EventArgs * e)
{
//Create
EmployeeInOut* box1 = new EmployeeInOut();
//Fill
int temp = System::Int32::Parse(EmpNum);// I want to use “temp”

}
};

//Class (3):
ublic __gc class EmployeeInOut : public System::Windows::Forms::Form
{
public:
EmployeeInOut(void)
{


private: System::Void OutBtn_Click(System::Object * sender,
//OUT
System::EventArgs * e) //OUT
{
//Create
EmployeeInOut* box1 = new EmployeeInOut();
//Fill
switch (temp)//---- I want to access temp from the 2nd class to use in(let
say switch) the 3rd class
{

}




 
Reply With Quote
 
 
 
 
Jeff Johnson
Guest
Posts: n/a
 
      10th Sep 2009
"Allen" <(E-Mail Removed)> wrote in message
news:E1661619-B65D-4751-B473-(E-Mail Removed)...

> Below are some skeleton codes to show the 3 classes. I hope someone can
> help.


Perhaps if you post to a C++ group instead of a C# group, someone will....


 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      10th Sep 2009
"Jeff Johnson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

>> Below are some skeleton codes to show the 3 classes. I hope someone can
>> help.

>
> Perhaps if you post to a C++ group instead of a C# group, someone will....


AAAGGGGHHH!!! Sorry about that! I could have sworn I was reading the C#
group....

Anyways, what you need to do is expose your variable from the EmployeeID
class, preferrably as a property.


 
Reply With Quote
 
Allen
Guest
Posts: n/a
 
      11th Sep 2009
I had one aready, if you look at the second class.

__property void set_EmpNum(String* p){empNumBox->Text = p;}
__property String* get_EmpNum() {return empNumBox->Text;}

I do not know if it's OK.


--
Thanks
Allen
"Jeff Johnson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Jeff Johnson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>> Below are some skeleton codes to show the 3 classes. I hope someone can
>>> help.

>>
>> Perhaps if you post to a C++ group instead of a C# group, someone
>> will....

>
> AAAGGGGHHH!!! Sorry about that! I could have sworn I was reading the C#
> group....
>
> Anyways, what you need to do is expose your variable from the EmployeeID
> class, preferrably as a property.
>


 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      11th Sep 2009
"Allen" <(E-Mail Removed)> wrote in message
news:B86340FA-1364-48A5-B43F-(E-Mail Removed)...

>I had one aready, if you look at the second class.
>
> __property void set_EmpNum(String* p){empNumBox->Text = p;}
> __property String* get_EmpNum() {return empNumBox->Text;}
>
> I do not know if it's OK.


Well, that's a string, and you converted that string to an int when you made
your temp variable, so I assumed you wanted to expose the value as an int.
(Some error trapping would be good, as well.)

If you just want it as a string, then the property you already have is fine.
The EmployeeInOut form will need some sort of reference to the EmployeeID
form, by the way. If EmployeeInOut is some kind of "sub-form" of EmployeeID
then perhaps you could pass the parent instance to an overload of
EmployeeInOut's constructor.


 
Reply With Quote
 
Morten Wennevik [C# MVP]
Guest
Posts: n/a
 
      14th Sep 2009
Hi Allen,

It is a bit hard to grasp what you are actually trying to do. To let
EmployeeInOut know of the temp number you can just pass it along in the
constructor. If what you really want to do is have EmployeeInOut change the
temp number you should instead use the DialogResult and set/get a property on
the EmployeeInOut.

Then again, a better solution alltogether is to have an Employee class you
can send among the forms, and pass along this object to the EmployeeInOut
class which can then read and update as needed. The Employee class would
contain the employee ID name, number, address and anything else you would
associate with an employee.


Employee employee = new Employee();
EmployeeInOut box1 = new EmployeeInOut(employee);


--
Happy Coding!
Morten Wennevik [C# MVP]


"Allen" wrote:

>
> I am making a program with 3 forms(classes). 1)Form1, 2) Employee, and 3)
> EmployeeInOu. I am using MS VS 2008.
>
> They all inherit from “System::Windows::Forms::Form”.
> My problem is I want to access variable “temp”, which belongs to EmployeeID
> class, in a different class which is EmployeeInOu.
> Below are some skeleton codes to show the 3 classes. I hope someone can
> help.
>
> //Class(1):
> public __gc class Form1 : public System::Windows::Forms::From {
> public:
> Form1(void)
> private: System::Void EmpMenuItem_Click(System::Object * sender,
> System::EventArgs * e)
> {
> //Create
> EmployeeID* box = new EmployeeID();
>
> //Fill
> box->EmpNum = S""; //set is used
>
> //Show
> if (box->ShowDialog() == DialogResult::OK)
> {
> box->EmpNum; //get is used
>
> }
> }
> };
>
> //Class (2):
> public __gc class EmployeeID : public System::Windows::Forms::Form
> {
> public:
>
> private: System::Windows::Forms::TextBox * empNumBox;
>
> public:
> __property void set_EmpNum(String* p){empNumBox->Text = p;}
> __property String* get_EmpNum() {return empNumBox->Text;}
> private: System::Void OKBtn_Click(System::Object * sender,
> System::EventArgs * e)
> {
> //Create
> EmployeeInOut* box1 = new EmployeeInOut();
> //Fill
> int temp = System::Int32::Parse(EmpNum);// I want to use “temp”
>
> }
> };
>
> //Class (3):
> ublic __gc class EmployeeInOut : public System::Windows::Forms::Form
> {
> public:
> EmployeeInOut(void)
> {
>
>
> private: System::Void OutBtn_Click(System::Object * sender,
> //OUT
> System::EventArgs * e) //OUT
> {
> //Create
> EmployeeInOut* box1 = new EmployeeInOut();
> //Fill
> switch (temp)//---- I want to access temp from the 2nd class to use in(let
> say switch) the 3rd class
> {
>
> }
>
>
>
>

 
Reply With Quote
 
Allen
Guest
Posts: n/a
 
      15th Sep 2009
I am sorry I made a mistake. The 3 forms (classes). 1)Form1, 2) EmployeeID,
and 3). In other words I said 2) Emploee, but I supposed to say 2)
EmployeeID

From (int temp = System::Int32::Parse(EmpNum);// I want to use “temp”) ,
you can see all I wanted is to use "temp". As you may notice "temp" is
only the "empNum" (employee number). I have to convert empNum to Int32 so
that I can use it in a switch in the same class (EmployeeID). Now I want to
use temp or empNum in another switch but in a different class
(EmployeeInOut).

--
Thanks
Allen
"Morten Wennevik [C# MVP]" <(E-Mail Removed)> wrote in message
news:C4BF979B-CB0B-4680-824D-(E-Mail Removed)...
> Hi Allen,
>
> It is a bit hard to grasp what you are actually trying to do. To let
> EmployeeInOut know of the temp number you can just pass it along in the
> constructor. If what you really want to do is have EmployeeInOut change
> the
> temp number you should instead use the DialogResult and set/get a property
> on
> the EmployeeInOut.
>
> Then again, a better solution alltogether is to have an Employee class you
> can send among the forms, and pass along this object to the EmployeeInOut
> class which can then read and update as needed. The Employee class would
> contain the employee ID name, number, address and anything else you would
> associate with an employee.
>
>
> Employee employee = new Employee();
> EmployeeInOut box1 = new EmployeeInOut(employee);
>
>
> --
> Happy Coding!
> Morten Wennevik [C# MVP]
>
>
> "Allen" wrote:
>
>>
>> I am making a program with 3 forms(classes). 1)Form1, 2) Employee, and
>> 3)
>> EmployeeInOu. I am using MS VS 2008.
>>
>> They all inherit from “System::Windows::Forms::Form”.
>> My problem is I want to access variable “temp”, which belongs to
>> EmployeeID
>> class, in a different class which is EmployeeInOu.
>> Below are some skeleton codes to show the 3 classes. I hope someone can
>> help.
>>
>> //Class(1):
>> public __gc class Form1 : public System::Windows::Forms::From {
>> public:
>> Form1(void)
>> private: System::Void EmpMenuItem_Click(System::Object * sender,
>> System::EventArgs * e)
>> {
>> //Create
>> EmployeeID* box = new EmployeeID();
>>
>> //Fill
>> box->EmpNum = S""; //set is used
>>
>> //Show
>> if (box->ShowDialog() == DialogResult::OK)
>> {
>> box->EmpNum; //get is used
>>
>> }
>> }
>> };
>>
>> //Class (2):
>> public __gc class EmployeeID : public System::Windows::Forms::Form
>> {
>> public:
>>
>> private: System::Windows::Forms::TextBox * empNumBox;
>>
>> public:
>> __property void set_EmpNum(String* p){empNumBox->Text = p;}
>> __property String* get_EmpNum() {return empNumBox->Text;}
>> private: System::Void OKBtn_Click(System::Object * sender,
>> System::EventArgs * e)
>> {
>> //Create
>> EmployeeInOut* box1 = new EmployeeInOut();
>> //Fill
>> int temp = System::Int32::Parse(EmpNum);// I want to use “temp”
>>
>> }
>> };
>>
>> //Class (3):
>> ublic __gc class EmployeeInOut : public System::Windows::Forms::Form
>> {
>> public:
>> EmployeeInOut(void)
>> {
>>
>>
>> private: System::Void OutBtn_Click(System::Object * sender,
>> //OUT
>> System::EventArgs * e) //OUT
>> {
>> //Create
>> EmployeeInOut* box1 = new EmployeeInOut();
>> //Fill
>> switch (temp)//---- I want to access temp from the 2nd class to use
>> in(let
>> say switch) the 3rd class
>> {
>>
>> }
>>
>>
>>
>>


 
Reply With Quote
 
Morten Wennevik [C# MVP]
Guest
Posts: n/a
 
      16th Sep 2009
Hi Allen,

The Employee class in my code is not a Form, just a class to hold employee
data.

if all you want is to pass temp along to EmployeeInOut just create a
constructor in EmployeeInOut accepting an integer.


EmployeeInOut box1 = new EmployeeInOut(123);


class EmployeeInOut : Form
{
int externalNumber;
public EmployeeInOut(int number)
{
externalNumber = number;
}

void SomeMethod()
{
switch(externalNumber)
{
}
// externalNumber will be the number you send in the constructor
}
}



--
Happy Coding!
Morten Wennevik [C# MVP]


"Allen" wrote:

> I am sorry I made a mistake. The 3 forms (classes). 1)Form1, 2) EmployeeID,
> and 3). In other words I said 2) Emploee, but I supposed to say 2)
> EmployeeID
>
> From (int temp = System::Int32::Parse(EmpNum);// I want to use “temp”) ,
> you can see all I wanted is to use "temp". As you may notice "temp" is
> only the "empNum" (employee number). I have to convert empNum to Int32 so
> that I can use it in a switch in the same class (EmployeeID). Now I want to
> use temp or empNum in another switch but in a different class
> (EmployeeInOut).
>
> --
> Thanks
> Allen
> "Morten Wennevik [C# MVP]" <(E-Mail Removed)> wrote in message
> news:C4BF979B-CB0B-4680-824D-(E-Mail Removed)...
> > Hi Allen,
> >
> > It is a bit hard to grasp what you are actually trying to do. To let
> > EmployeeInOut know of the temp number you can just pass it along in the
> > constructor. If what you really want to do is have EmployeeInOut change
> > the
> > temp number you should instead use the DialogResult and set/get a property
> > on
> > the EmployeeInOut.
> >
> > Then again, a better solution alltogether is to have an Employee class you
> > can send among the forms, and pass along this object to the EmployeeInOut
> > class which can then read and update as needed. The Employee class would
> > contain the employee ID name, number, address and anything else you would
> > associate with an employee.
> >
> >
> > Employee employee = new Employee();
> > EmployeeInOut box1 = new EmployeeInOut(employee);
> >
> >
> > --
> > Happy Coding!
> > Morten Wennevik [C# MVP]
> >
> >
> > "Allen" wrote:
> >
> >>
> >> I am making a program with 3 forms(classes). 1)Form1, 2) Employee, and
> >> 3)
> >> EmployeeInOu. I am using MS VS 2008.
> >>
> >> They all inherit from “System::Windows::Forms::Form”.
> >> My problem is I want to access variable “temp”, which belongs to
> >> EmployeeID
> >> class, in a different class which is EmployeeInOu.
> >> Below are some skeleton codes to show the 3 classes. I hope someone can
> >> help.
> >>
> >> //Class(1):
> >> public __gc class Form1 : public System::Windows::Forms::From {
> >> public:
> >> Form1(void)
> >> private: System::Void EmpMenuItem_Click(System::Object * sender,
> >> System::EventArgs * e)
> >> {
> >> //Create
> >> EmployeeID* box = new EmployeeID();
> >>
> >> //Fill
> >> box->EmpNum = S""; //set is used
> >>
> >> //Show
> >> if (box->ShowDialog() == DialogResult::OK)
> >> {
> >> box->EmpNum; //get is used
> >>
> >> }
> >> }
> >> };
> >>
> >> //Class (2):
> >> public __gc class EmployeeID : public System::Windows::Forms::Form
> >> {
> >> public:
> >>
> >> private: System::Windows::Forms::TextBox * empNumBox;
> >>
> >> public:
> >> __property void set_EmpNum(String* p){empNumBox->Text = p;}
> >> __property String* get_EmpNum() {return empNumBox->Text;}
> >> private: System::Void OKBtn_Click(System::Object * sender,
> >> System::EventArgs * e)
> >> {
> >> //Create
> >> EmployeeInOut* box1 = new EmployeeInOut();
> >> //Fill
> >> int temp = System::Int32::Parse(EmpNum);// I want to use “temp”
> >>
> >> }
> >> };
> >>
> >> //Class (3):
> >> ublic __gc class EmployeeInOut : public System::Windows::Forms::Form
> >> {
> >> public:
> >> EmployeeInOut(void)
> >> {
> >>
> >>
> >> private: System::Void OutBtn_Click(System::Object * sender,
> >> //OUT
> >> System::EventArgs * e) //OUT
> >> {
> >> //Create
> >> EmployeeInOut* box1 = new EmployeeInOut();
> >> //Fill
> >> switch (temp)//---- I want to access temp from the 2nd class to use
> >> in(let
> >> say switch) the 3rd class
> >> {
> >>
> >> }
> >>
> >>
> >>
> >>

>

 
Reply With Quote
 
Allen
Guest
Posts: n/a
 
      18th Sep 2009
I am sorry; I understand you very well, but I did not make it clear form the
beginning that. I have no control over the integer you suggested.
the user is going to enter "empNumBox" from a text box. In some point I
need to use the amount entered which is of type
"System::Windows::Forms::TextBox^". I would like to use the amount entered
in a switch to get the right employee. Unfortunately, that switch will
accept only integers.


protected:

private: System::Windows::Forms::TextBox^ empNumBox;
..
..
..

switch (empNumBox
{
case 1:
{
..
..
}
break;
case 2:
{
..
..
}
break;
case 3:
{
..
..

}
break;
default: MessageBox::Show(" This is an invalid employee number.\n Please try
again! ",L" Error ");
}


I may have to use property like this:

property TextBox^ EmpNum
{
//String^ get(){return empNumBox; }
TextBox^ get(){return empNumBox; }
void set(TextBox^ p) {empNumBox = p ;}
}






--
Thanks
Allen
"Morten Wennevik [C# MVP]" <(E-Mail Removed)> wrote in message
news:5A200DA1-58D6-4001-853F-(E-Mail Removed)...
> Hi Allen,
>
> The Employee class in my code is not a Form, just a class to hold employee
> data.
>
> if all you want is to pass temp along to EmployeeInOut just create a
> constructor in EmployeeInOut accepting an integer.
>
>
> EmployeeInOut box1 = new EmployeeInOut(123);
>
>
> class EmployeeInOut : Form
> {
> int externalNumber;
> public EmployeeInOut(int number)
> {
> externalNumber = number;
> }
>
> void SomeMethod()
> {
> switch(externalNumber)
> {
> }
> // externalNumber will be the number you send in the constructor
> }
> }
>
>
>
> --
> Happy Coding!
> Morten Wennevik [C# MVP]
>
>
> "Allen" wrote:
>
>> I am sorry I made a mistake. The 3 forms (classes). 1)Form1, 2)
>> EmployeeID,
>> and 3). In other words I said 2) Emploee, but I supposed to say 2)
>> EmployeeID
>>
>> From (int temp = System::Int32::Parse(EmpNum);// I want to use “temp”) ,
>> you can see all I wanted is to use "temp". As you may notice "temp" is
>> only the "empNum" (employee number). I have to convert empNum to Int32
>> so
>> that I can use it in a switch in the same class (EmployeeID). Now I want
>> to
>> use temp or empNum in another switch but in a different class
>> (EmployeeInOut).
>>
>> --
>> Thanks
>> Allen
>> "Morten Wennevik [C# MVP]" <(E-Mail Removed)> wrote in message
>> news:C4BF979B-CB0B-4680-824D-(E-Mail Removed)...
>> > Hi Allen,
>> >
>> > It is a bit hard to grasp what you are actually trying to do. To let
>> > EmployeeInOut know of the temp number you can just pass it along in the
>> > constructor. If what you really want to do is have EmployeeInOut
>> > change
>> > the
>> > temp number you should instead use the DialogResult and set/get a
>> > property
>> > on
>> > the EmployeeInOut.
>> >
>> > Then again, a better solution alltogether is to have an Employee class
>> > you
>> > can send among the forms, and pass along this object to the
>> > EmployeeInOut
>> > class which can then read and update as needed. The Employee class
>> > would
>> > contain the employee ID name, number, address and anything else you
>> > would
>> > associate with an employee.
>> >
>> >
>> > Employee employee = new Employee();
>> > EmployeeInOut box1 = new EmployeeInOut(employee);
>> >
>> >
>> > --
>> > Happy Coding!
>> > Morten Wennevik [C# MVP]
>> >
>> >
>> > "Allen" wrote:
>> >
>> >>
>> >> I am making a program with 3 forms(classes). 1)Form1, 2) Employee,
>> >> and
>> >> 3)
>> >> EmployeeInOu. I am using MS VS 2008.
>> >>
>> >> They all inherit from “System::Windows::Forms::Form”.
>> >> My problem is I want to access variable “temp”, which belongs to
>> >> EmployeeID
>> >> class, in a different class which is EmployeeInOu.
>> >> Below are some skeleton codes to show the 3 classes. I hope someone
>> >> can
>> >> help.
>> >>
>> >> //Class(1):
>> >> public __gc class Form1 : public System::Windows::Forms::From {
>> >> public:
>> >> Form1(void)
>> >> private: System::Void EmpMenuItem_Click(System::Object * sender,
>> >> System::EventArgs * e)
>> >> {
>> >> //Create
>> >> EmployeeID* box = new EmployeeID();
>> >>
>> >> //Fill
>> >> box->EmpNum = S""; //set is used
>> >>
>> >> //Show
>> >> if (box->ShowDialog() == DialogResult::OK)
>> >> {
>> >> box->EmpNum; //get is used
>> >>
>> >> }
>> >> }
>> >> };
>> >>
>> >> //Class (2):
>> >> public __gc class EmployeeID : public System::Windows::Forms::Form
>> >> {
>> >> public:
>> >>
>> >> private: System::Windows::Forms::TextBox * empNumBox;
>> >>
>> >> public:
>> >> __property void set_EmpNum(String* p){empNumBox->Text = p;}
>> >> __property String* get_EmpNum() {return empNumBox->Text;}
>> >> private: System::Void OKBtn_Click(System::Object * sender,
>> >> System::EventArgs * e)
>> >> {
>> >> //Create
>> >> EmployeeInOut* box1 = new EmployeeInOut();
>> >> //Fill
>> >> int temp = System::Int32::Parse(EmpNum);// I want to use “temp”
>> >>
>> >> }
>> >> };
>> >>
>> >> //Class (3):
>> >> ublic __gc class EmployeeInOut : public System::Windows::Forms::Form
>> >> {
>> >> public:
>> >> EmployeeInOut(void)
>> >> {
>> >>
>> >>
>> >> private: System::Void OutBtn_Click(System::Object * sender,
>> >> //OUT
>> >> System::EventArgs * e) //OUT
>> >> {
>> >> //Create
>> >> EmployeeInOut* box1 = new EmployeeInOut();
>> >> //Fill
>> >> switch (temp)//---- I want to access temp from the 2nd class to use
>> >> in(let
>> >> say switch) the 3rd class
>> >> {
>> >>
>> >> }
>> >>
>> >>
>> >>
>> >>

>>


 
Reply With Quote
 
Morten Wennevik [C# MVP]
Guest
Posts: n/a
 
      18th Sep 2009
Allen, please correct me if I have misunderstood.

In one form you have a TextBox. A user writes a number in this TextBox and
clicks a button that will open the EmployeeInOut Form. You want the
EmployeeInOut Form to receive this number.

You can do that by passing the number in a constructor

EmployeeInOut box1 = new EmployeeInOut(123);

or

EmployeeInOut box1 = new EmployeeInOut(Int32.Parse(textBox1.Text));

or

int temp = int.Parse(textBox1.Text)
EmployeeInOut box1 = new EmployeeInOut(temp);

In addition you need to create a constructor in EmployeeInOut that accepts
and stores the number received.

There is another way. Create a property in EmployeeInOut the parent form
can access.

EmployeeInOut box1 = new EmployeeInOut();
box1.EmpNum= temp; //

....

class EmployeeInOut : Form
{
public int EmpNum{ get; set; }
}

A third option, if you really want to you can access the Parent property of
a Form and read fields and properties directly. This is frowned upon, though.

private void OutBtn_Click(object sender, EventArgs e)
{
EmployeeID parent = this.Parent as EmployeeID
switch (parent.EmpNum)
{
}
}


--
Happy Coding!
Morten Wennevik [C# MVP]


"Allen" wrote:

> I am sorry; I understand you very well, but I did not make it clear form the
> beginning that. I have no control over the integer you suggested.
> the user is going to enter "empNumBox" from a text box. In some point I
> need to use the amount entered which is of type
> "System::Windows::Forms::TextBox^". I would like to use the amount entered
> in a switch to get the right employee. Unfortunately, that switch will
> accept only integers.
>
>
> protected:
>
> private: System::Windows::Forms::TextBox^ empNumBox;
> .
> .
> .
>
> switch (empNumBox
> {
> case 1:
> {
> .
> .
> }
> break;
> case 2:
> {
> .
> .
> }
> break;
> case 3:
> {
> .
> .
>
> }
> break;
> default: MessageBox::Show(" This is an invalid employee number.\n Please try
> again! ",L" Error ");
> }
>
>
> I may have to use property like this:
>
> property TextBox^ EmpNum
> {
> //String^ get(){return empNumBox; }
> TextBox^ get(){return empNumBox; }
> void set(TextBox^ p) {empNumBox = p ;}
> }
>
>
>
>
>
>
> --
> Thanks
> Allen
> "Morten Wennevik [C# MVP]" <(E-Mail Removed)> wrote in message
> news:5A200DA1-58D6-4001-853F-(E-Mail Removed)...
> > Hi Allen,
> >
> > The Employee class in my code is not a Form, just a class to hold employee
> > data.
> >
> > if all you want is to pass temp along to EmployeeInOut just create a
> > constructor in EmployeeInOut accepting an integer.
> >
> >
> > EmployeeInOut box1 = new EmployeeInOut(123);
> >
> >
> > class EmployeeInOut : Form
> > {
> > int externalNumber;
> > public EmployeeInOut(int number)
> > {
> > externalNumber = number;
> > }
> >
> > void SomeMethod()
> > {
> > switch(externalNumber)
> > {
> > }
> > // externalNumber will be the number you send in the constructor
> > }
> > }
> >
> >
> >
> > --
> > Happy Coding!
> > Morten Wennevik [C# MVP]
> >
> >
> > "Allen" wrote:
> >
> >> I am sorry I made a mistake. The 3 forms (classes). 1)Form1, 2)
> >> EmployeeID,
> >> and 3). In other words I said 2) Emploee, but I supposed to say 2)
> >> EmployeeID
> >>
> >> From (int temp = System::Int32::Parse(EmpNum);// I want to use “temp”) ,
> >> you can see all I wanted is to use "temp". As you may notice "temp" is
> >> only the "empNum" (employee number). I have to convert empNum to Int32
> >> so
> >> that I can use it in a switch in the same class (EmployeeID). Now I want
> >> to
> >> use temp or empNum in another switch but in a different class
> >> (EmployeeInOut).
> >>
> >> --
> >> Thanks
> >> Allen
> >> "Morten Wennevik [C# MVP]" <(E-Mail Removed)> wrote in message
> >> news:C4BF979B-CB0B-4680-824D-(E-Mail Removed)...
> >> > Hi Allen,
> >> >
> >> > It is a bit hard to grasp what you are actually trying to do. To let
> >> > EmployeeInOut know of the temp number you can just pass it along in the
> >> > constructor. If what you really want to do is have EmployeeInOut
> >> > change
> >> > the
> >> > temp number you should instead use the DialogResult and set/get a
> >> > property
> >> > on
> >> > the EmployeeInOut.
> >> >
> >> > Then again, a better solution alltogether is to have an Employee class
> >> > you
> >> > can send among the forms, and pass along this object to the
> >> > EmployeeInOut
> >> > class which can then read and update as needed. The Employee class
> >> > would
> >> > contain the employee ID name, number, address and anything else you
> >> > would
> >> > associate with an employee.
> >> >
> >> >
> >> > Employee employee = new Employee();
> >> > EmployeeInOut box1 = new EmployeeInOut(employee);
> >> >
> >> >
> >> > --
> >> > Happy Coding!
> >> > Morten Wennevik [C# MVP]
> >> >
> >> >
> >> > "Allen" wrote:
> >> >
> >> >>
> >> >> I am making a program with 3 forms(classes). 1)Form1, 2) Employee,
> >> >> and
> >> >> 3)
> >> >> EmployeeInOu. I am using MS VS 2008.
> >> >>
> >> >> They all inherit from “System::Windows::Forms::Form”.
> >> >> My problem is I want to access variable “temp”, which belongs to
> >> >> EmployeeID
> >> >> class, in a different class which is EmployeeInOu.
> >> >> Below are some skeleton codes to show the 3 classes. I hope someone
> >> >> can
> >> >> help.
> >> >>
> >> >> //Class(1):
> >> >> public __gc class Form1 : public System::Windows::Forms::From {
> >> >> public:
> >> >> Form1(void)
> >> >> private: System::Void EmpMenuItem_Click(System::Object * sender,
> >> >> System::EventArgs * e)
> >> >> {
> >> >> //Create
> >> >> EmployeeID* box = new EmployeeID();
> >> >>
> >> >> //Fill
> >> >> box->EmpNum = S""; //set is used
> >> >>
> >> >> //Show
> >> >> if (box->ShowDialog() == DialogResult::OK)
> >> >> {
> >> >> box->EmpNum; //get is used
> >> >>
> >> >> }
> >> >> }
> >> >> };
> >> >>
> >> >> //Class (2):
> >> >> public __gc class EmployeeID : public System::Windows::Forms::Form
> >> >> {
> >> >> public:
> >> >>
> >> >> private: System::Windows::Forms::TextBox * empNumBox;
> >> >>
> >> >> public:
> >> >> __property void set_EmpNum(String* p){empNumBox->Text = p;}
> >> >> __property String* get_EmpNum() {return empNumBox->Text;}
> >> >> private: System::Void OKBtn_Click(System::Object * sender,
> >> >> System::EventArgs * e)
> >> >> {
> >> >> //Create
> >> >> EmployeeInOut* box1 = new EmployeeInOut();
> >> >> //Fill
> >> >> int temp = System::Int32::Parse(EmpNum);// I want to use “temp”
> >> >>
> >> >> }
> >> >> };
> >> >>
> >> >> //Class (3):
> >> >> ublic __gc class EmployeeInOut : public System::Windows::Forms::Form
> >> >> {
> >> >> public:
> >> >> EmployeeInOut(void)
> >> >> {
> >> >>
> >> >>
> >> >> private: System::Void OutBtn_Click(System::Object * sender,
> >> >> //OUT
> >> >> System::EventArgs * e) //OUT
> >> >> {
> >> >> //Create
> >> >> EmployeeInOut* box1 = new EmployeeInOut();
> >> >> //Fill
> >> >> switch (temp)//---- I want to access temp from the 2nd class to use
> >> >> in(let
> >> >> say switch) the 3rd class
> >> >> {
> >> >>
> >> >> }
> >> >>
> >> >>
> >> >>
> >> >>
> >>

>

 
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
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 Framework 4 1st Dec 2005 08:34 AM
Restricting access of class variable to another single class? Danny Tuppeny Microsoft Dot NET 4 1st Dec 2005 08:34 AM
Unable to access a variable from another class - please help! Al Murphy Microsoft C# .NET 4 8th Feb 2005 01:33 PM
Access to color variable in a class Anonymous Microsoft C# .NET 3 10th Feb 2004 11:33 AM


Features
 

Advertising
 

Newsgroups
 


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