473,468 Members | 1,389 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to access the values assigned to the members of Class 1 in Class 2 ?

21 New Member
Hi everybody...I am new to OOP! so facing some difficulties and need help.
My Problem is...

I have a form and two classes (Class1 And Class2) when i enter the data in to the form the values are assigned to Class1 Members what i want is the values assigned to class1 members accessible in class2. I am using Visual Studio 2008

Here is the code

Code for Form:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace Simple_Dal_Test
  5. {
  6.     public partial class Form1 : Form
  7.     {
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.  
  13.         private void label2_Click(object sender, EventArgs e)
  14.         {
  15.  
  16.         }
  17.  
  18.         private void Form1_Load(object sender, EventArgs e)
  19.         {
  20.  
  21.         }
  22.  
  23.         private void button1_Click(object sender, EventArgs e)
  24.         {
  25.  
  26.             MyClass1 MyName = new MyClass1();
  27.             MyName.Name = textBox1.Text;
  28.  
  29.             MyClass1 MyPh = new MyClass1();
  30.             MyPh.Ph = textBox2.Text;
  31.  
  32.         }
  33.     }
  34. }
  35.  
Code For Class1:

Expand|Select|Wrap|Line Numbers
  1. namespace Simple_Dal_Test
  2. {
  3.     public class MyClass1
  4.     {
  5.         private string _Name;
  6.         private string _Ph;
  7.  
  8.         public string Name
  9.         {
  10.             get { return _Name; }
  11.             set { _Name = value; }
  12.         }
  13.  
  14.         public string Ph
  15.         {
  16.             get { return _Ph; }
  17.             set { _Ph = value; }
  18.         }
  19.  
  20.     }
  21.  
  22. }
Code For Class2:

Expand|Select|Wrap|Line Numbers
  1. namespace Simple_Dal_Test
  2. {
  3.  
  4.     public class MyClass2
  5.     {
  6.  
  7.         MyClass1 MyN = new MyClass1();
  8.  
  9.  
  10.         MyClass1 MyP = new MyClass1();
  11.  
  12.     }
  13. }
Sep 3 '09 #1
1 1593
tlhintoq
3,525 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
  1.         private void button1_Click(object sender, EventArgs e)
  2.         {
  3.  
  4.             MyClass1 MyName = new MyClass1();
  5.             MyName.Name = textBox1.Text;
  6.  
  7.             MyClass1 MyPh = new MyClass1();
  8.             MyPh.Ph = textBox2.Text;
  9.  
  10.         }
In this case your MyClass1 only has a scope of this one method. It was made at the beginning of the method and will cease to exist when the method ends.

What you need to do is create the classes 'higher up the food chain' so they have a broader scope of existence. For example: Your form1 could create a public Class1 and a public Class2. They could then be referenced through the form1

Expand|Select|Wrap|Line Numbers
  1. // From inside Class2
  2. String PhoneNumber = Form1.MyClass1.PhoneNumberProperty;

Next tip. Don't do this:
Expand|Select|Wrap|Line Numbers
  1. MyPh.Ph = textBox2.Text;
This requires that every other class know the exact name of every control of every other class and form. It sucks and is impossible to maintain. Its a bad habit. Don't start and you won't have to break yourself of it.

Take an extra 3 minutes and build properties in the forms for these things.
Expand|Select|Wrap|Line Numbers
  1. // In class1
  2. string PhoneNumber
  3. {
  4.    get  { return textbox1.text; }
  5.    set  { textbox1.text = value; }
  6. }
  7.  
Now you can get this value by just getting the PhoneNumber property

Expand|Select|Wrap|Line Numbers
  1. // From class87
  2. MyClass1.PhoneNumber = "867-5309";
  3.  
Later you can redesign the entire form and put in range checking without every class that references this property getting broken.

Expand|Select|Wrap|Line Numbers
  1. // In class1
  2. string PhoneNumber
  3. {
  4.    //  Textbox1 has been renamed, but other classes didn't need to know
  5.    get  { return tbPhoneNumber.text; }
  6.    set  {
  7.               tbPhoneNumber.text = value;
  8.               If (value == "867-5609)
  9.               {
  10.                MessageBox.Show("You got the number off the wall", "Hey Janey");
  11.               }
  12.               if (value.StartsWith("555")
  13.               {
  14.                MessageBox.Show("That prefix is only used in movies.");
  15.               }
  16.  
  17.           }
  18. }
  19.  
Sep 3 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: diadia | last post by:
when i write two classes as follow . compiler tell me that i can't access private number from other class but if types of two classes are the same it don't have error message ? why? class...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
3
by: Chua Wen Ching | last post by:
Hi there, I had seen examples for classes, but i had no idea how to implement the same thing in struct. I am quite mix up! Which one is correct? Scenario: WForm.cs - the one that calls...
7
by: cFleury | last post by:
I have an ASP.NET web form that is retaining values over different stations on the network, how can I stop this behavior ?. I didn’t do anything special to cause it. Thanks C.Fleury
6
by: Plamen Doykov | last post by:
Hi all I have converted a simple project from ASP.NET 1 to 2.0 with the latest prerelease of Visual Studio 2005. The problem is I can't access internal members from the code behind. It gives:...
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
7
by: Andy B | last post by:
I have a class I am creating for data access. I need to access controls from inside the class that are on a particular page. How do I do this? or is creating an instance of the page class and using...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.