473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to bring a value from a class?

18
hello, sorry to bothering all.

May I know what is the coding to bring a value from a class's constructor?

Below is the class.
Expand|Select|Wrap|Line Numbers
  1. public class ABC
  2.     {
  3.         public String aa;
  4.         public String bb;
  5.         public Int32 cc;
  6.  
  7.         public ABC(String Aa, String Bb, Int32 Cc)
  8.         {
  9.             this.aa = Aa;
  10.             this.bb = Bb;
  11.             this.cc = Cc;
  12.          }
  13.     }  

Let's say i wan bring the value of ABC.bb from the class to a window form. How to pass the value from the class??

Please help. Thanks in advance. :)
By, Aeris.
Mar 19 '10 #1

✓ answered by tlhintoq

How do I get my Form2 to react to something on my Form1?
How do I make my Form1 control something on my Form2?
Although you can have Form1 directly access items on Form2 it isn't the recommended way to go. It ties the two forms tightly to each other. If a change is made in Form2 such as removing one of the controls, then code in Form1 breaks.
It is better to Form1 raise an event, and have Form2 contain its own code for how to react to this. This places responsibility for Form2 within Form2, and Form1 within Form1.
It keeps Form1 blissfully ignorant of Form2 - and a logging component - and a progress component, and a dozen other little black boxes that can be subscribed to events in Form1, all without Form1 being made responsible for directly affecting controls other than itself.
Events tutorial (including Form to Form which is the same as class to class)
This tutorial for a cash register does exactly that: It makes a virtual numeric keyboard.
Bad: Directly accessing controls of one class/form from another.
Expand|Select|Wrap|Line Numbers
  1. bool IsOn = Form2.button1.IsChecked;


Good: Use a property to get such information
Expand|Select|Wrap|Line Numbers
  1. //Form1
  2. bool IsOn = Form2.IsOn;
Expand|Select|Wrap|Line Numbers
  1. //Form 2
  2. public bool IsOn
  3. {
  4.    get { return button1.Checked; }
  5.    set { button1.Checked = value; }
  6. }
It's a subtle but important difference as your applications become more complex. Using properties means your target class/form (Form2) can be changed and updated a thousand different ways yet won't negatively impact the classes that are reading from it. If you change the name of a control for example: No break in all your other classes. If your target form evolves where it needs to do 10 things when it is turned on, then the responsibility stays within Form2, and that burden on not put on Form1 and a dozen other forms that might be using Form2. The goal is to compartimentalize the work so that Form2 is responsiblity SOLELY for Form2. From1 should only have to say "Turn on" or "Turn Off"

Expand|Select|Wrap|Line Numbers
  1. Form2
  2. public bool IsOn
  3. {
  4.    get { return btnMeaningfulName.Checked; }
  5.    set {
  6.             btnMeaningfulName.Checked = value;
  7.             panelDashboard.Visible = value;
  8.             labelStatus = value == true ? "On" : "Off";
  9.             btnRunNow.Enabled = value;
  10. }

4 2415
tlhintoq
3,525 Expert 2GB
The form would reference the instance of the class.

ABC myNewABC = new ABC("Alpha", "Bravo", "Charlie")

you would then reference it through the new instance

myNewABC.aa
Mar 19 '10 #2
aeris
18
Thanks for the reply.

But I actually having two forms.

From the first form, let's say I had reference the instance,
ABC myNewABC = new ABC("Alpha", "Bravo", "Charlie")

The Alpha, bravo and charlie are been inserted into the class.

And then, how to pass the value from the class to the Second Form?
Mar 19 '10 #3
aeris
18
@tlhintoq
Thanks for the reply.

But I actually having two forms.

From the first form, let's say I had reference the instance,
ABC myNewABC = new ABC("Alpha", "Bravo", "Charlie")

The Alpha, bravo and charlie are been inserted into the class.

And then, how to pass the value from the class to the Second Form?
Mar 19 '10 #4
tlhintoq
3,525 Expert 2GB
How do I get my Form2 to react to something on my Form1?
How do I make my Form1 control something on my Form2?
Although you can have Form1 directly access items on Form2 it isn't the recommended way to go. It ties the two forms tightly to each other. If a change is made in Form2 such as removing one of the controls, then code in Form1 breaks.
It is better to Form1 raise an event, and have Form2 contain its own code for how to react to this. This places responsibility for Form2 within Form2, and Form1 within Form1.
It keeps Form1 blissfully ignorant of Form2 - and a logging component - and a progress component, and a dozen other little black boxes that can be subscribed to events in Form1, all without Form1 being made responsible for directly affecting controls other than itself.
Events tutorial (including Form to Form which is the same as class to class)
This tutorial for a cash register does exactly that: It makes a virtual numeric keyboard.
Bad: Directly accessing controls of one class/form from another.
Expand|Select|Wrap|Line Numbers
  1. bool IsOn = Form2.button1.IsChecked;


Good: Use a property to get such information
Expand|Select|Wrap|Line Numbers
  1. //Form1
  2. bool IsOn = Form2.IsOn;
Expand|Select|Wrap|Line Numbers
  1. //Form 2
  2. public bool IsOn
  3. {
  4.    get { return button1.Checked; }
  5.    set { button1.Checked = value; }
  6. }
It's a subtle but important difference as your applications become more complex. Using properties means your target class/form (Form2) can be changed and updated a thousand different ways yet won't negatively impact the classes that are reading from it. If you change the name of a control for example: No break in all your other classes. If your target form evolves where it needs to do 10 things when it is turned on, then the responsibility stays within Form2, and that burden on not put on Form1 and a dozen other forms that might be using Form2. The goal is to compartimentalize the work so that Form2 is responsiblity SOLELY for Form2. From1 should only have to say "Turn on" or "Turn Off"

Expand|Select|Wrap|Line Numbers
  1. Form2
  2. public bool IsOn
  3. {
  4.    get { return btnMeaningfulName.Checked; }
  5.    set {
  6.             btnMeaningfulName.Checked = value;
  7.             panelDashboard.Visible = value;
  8.             labelStatus = value == true ? "On" : "Off";
  9.             btnRunNow.Enabled = value;
  10. }
Mar 19 '10 #5

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

Similar topics

1
by: Programmer | last post by:
Hi All Here is my problem I'm using a SQLDataAdapter and DataSet I use the method FillSchema(myDataset, SchemaType.Source) The problem is that when i Check the default Values of the Dataset...
2
by: Tyson | last post by:
I have got this little piece of code that fires on exit of a text box. It run's my query perfectly based of my form input. But I don't know how to bring result in my query back to a text box on my...
1
by: Matt M | last post by:
Hey... I'm trying to run a crystal report (8.5) from a c# windows forms application class. I can't use the report viewer as the application will be running these reports from a command line...
4
by: Davids | last post by:
in my small hangman web project I have a Hangman class which has some methods and properties (int GuessAttempts, string GuessedLetters etc..) which change as the game goes on... Best of all would...
6
by: Paolo Pignatelli | last post by:
I have an aspx code behind page that goes something like this in the HTML view: <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp;...
9
by: DraguVaso | last post by:
Hi, I want my application to bring another application to the Front. I thought best way to do this was by the Process-model: Dim c As Process = Process.GetCurrentProcess() Dim p As Process...
1
by: Scott | last post by:
I have created a windows form with vb.net and have done all the calculations to work out all the value that I have. I have a button at the bottom that I want to bring up a summary screen in a new...
8
by: Radx | last post by:
Here in my web application, I have a data entry page with serval controls. Some of the controls have autopostback is set true. But the problem is when two or more people are entering data at the...
0
by: Hasin Hayder | last post by:
You know that in ruby/prototype you can traverse thru each element of array like this Array.each(function(){/*function body*/}). It has also some methods like without(), inspect(),...
3
by: dave | last post by:
Hello, I'm currently on the class section of my self-taught journey and have a question about classes: is it possible to bring a object created inside the class definitions outside the class so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.