473,498 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to insert value into the class?

18 New Member
Sorry to bothering you all. Can someone give me a hand here? How to insert value from a form into a class?

I have a form, called EnterKeyWindow.cs.

Expand|Select|Wrap|Line Numbers
  1.         public CarrierFile carrierFile;
  2.  
  3.         public EnterKeyWindow()
  4.         {
  5.     InitializeComponent();
  6.     this.carrierFile = new CarrierFile(imageSourceLocation,String.Empty, 0, 0);
  7.          }
  8.  
  9.         public EnterKeyWindow(CarrierFile carrierFile):this()
  10.         {
  11.     this.carrierFile = carrierFile;
  12.         }
  13.  
  14.         private void EnterKeyWindow_Load(object sender, EventArgs e)
  15.         {
  16.             FillCarrierFile();
  17.         }
  18.  
  19.         private void FillCarrierFile()
  20.         {            
  21.             carrierFile.SourceFileName = imageSourceLocation;
  22.             carrierFile.DestinationFileName = textBox1.Text;
  23.             carrierFile.CountBytesToHide = 0;
  24.         }
Then I have another .cs, called CarrierFile.cs

Expand|Select|Wrap|Line Numbers
  1.  public class CarrierFile
  2.     {
  3.         public String SourceFileName;
  4.         public String DestinationFileName;
  5.         public Int32 CountBytesToHide;
  6.  
  7.         public CarrierFile(String sourceFileName, String destinationFileName, Int32 countBytesToHide, int CountBitsToHidePerCarrierUnit)
  8.         {
  9.             this.SourceFileName = sourceFileName;
  10.             this.DestinationFileName = destinationFileName;
  11.             this.CountBytesToHide = countBytesToHide;
  12.         }
But then, it can run, but the value actually fail to insert into CarrierFile.cs. May I know what is the code to insert the value into the class??

Thanks in advance,
By
Aeris.
Mar 21 '10 #1
6 1600
tlhintoq
3,525 Recognized Expert Specialist
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. }
Now when Form1 tells Form2 to turn on, Form2 will check a box, make an entire panel visible, change its Status label to say 'On' and enable a Run Now button.
Mar 21 '10 #2
GaryTexmo
1,501 Recognized Expert Top Contributor
I don't understand what you mean by the value fails to insert into CarrierFile.cs... you mean assigning the value to one of the properties doesn't do anything?

Which property isn't getting set, and what's the sequence of calls that are happening here? For example, which constructor happens to be called in your program flow right now?

*Edit: Ohhh! I think I might see the problem. You've got your constructor that takes a CarrierFile object as a paramater also calling the parameterless constructor. So you assign the local carrierFile member, then immediately destroy it by creating a new object.

This ...
Expand|Select|Wrap|Line Numbers
  1.         public EnterKeyWindow(CarrierFile carrierFile):this()
  2.         {
  3.     this.carrierFile = carrierFile;
  4.         }
... should be changed to this ...
Expand|Select|Wrap|Line Numbers
  1.         public EnterKeyWindow(CarrierFile carrierFile)
  2.         {
  3.     this.carrierFile = carrierFile;
  4.         }
Mar 21 '10 #3
aeris
18 New Member
Ya i mean failed to assigning the value to the SourceFileName in the CarrierFile.cs class.

I don't know how to assign a value to the class from a Form.
Mar 21 '10 #4
GaryTexmo
1,501 Recognized Expert Top Contributor
Please see my edit, that might help you out. Also, T's post should be a great help for you in general, read through it. He's also got some great articles in the insights section that you should check out :)
Mar 21 '10 #5
aeris
18 New Member
It is still the same eventhough i remove the ":this() ".
I don't know what is wrong with my codes... I've been fixing it long ago, but still can not assign the value to the class...

Some one please help...
Mar 21 '10 #6
tlhintoq
3,525 Recognized Expert Specialist
Ok... a critique on the code and a possible source of the problem.

This is asking for confusion
Expand|Select|Wrap|Line Numbers
  1. public EnterKeyWindow(CarrierFile carrierFile):this()
  2.         {
  3.     this.carrierFile = carrierFile;
  4.         }
Variable names are free. Why reuse something in this manner
Expand|Select|Wrap|Line Numbers
  1. public EnterKeyWindow(CarrierFile IncomingcarrierFile)
  2. {
  3.     carrierFile = IncomingcarrierFile;
  4. }
Personally, I wouldn't create a class named "EnterKeyWindow" because it sounds like a verb/method name. It *sounds* like the method that will execute when you enter [into] the KeyWindow.

I suspect the real problem of not passing values is here:
Expand|Select|Wrap|Line Numbers
  1. private void FillCarrierFile()
  2. {            
  3.       carrierFile.SourceFileName = imageSourceLocation;
  4.       carrierFile.DestinationFileName = textBox1.Text;
  5.       carrierFile.CountBytesToHide = 0;
  6. }
This is a class named "EnterKeyWindow" right? Must be since you have a constructor for it. Where does this textBox1 come from? Is it part of EnterKeyWindow?

Based on your question
How to insert value from a form into a class?
I have to assume that textbox1 is in the form and EnterKeyWindow is the class.

Which means you are tightly tieing textbox1 of form1 to the class of EnterKeyWindow. STOP! Don't do that. Bad habit to get in to.
First: This presumes that EnterKeyWindow even knows of form1, let alone textbox1. I presume that you aren't getting the value passed because of this reason: This *instance* of the class has no knowledge of that instance of the form1.
Second: It breaks as soon as you rename textbox1 to something human-friendly like "tbCustomerName"
Third: It means your EnterKeyWindow class can't be used/re-used for any other purpose but to work on this form. If you are going to do that just stick the code in the form1.cs and simplify the solution.

If you want to pass a value from one class to anther then that is what you should do: Pass it deliberatley, not through assumption that one can see the other. When you make your EnterKeyWindow pass the textbox1.text to the new class instance.

Expand|Select|Wrap|Line Numbers
  1. EnterKeyWindow bob = new EnterKeyWindow();
  2. bob.DestinationPath = textbox1.text;
  3.  
Mar 21 '10 #7

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

Similar topics

1
1273
by: NathanV | last post by:
I have created a class and stored procedure to insert records into a table. The insert works but only the first character of the "Title" and "Body" fields are added. I have traced up to the point...
12
2973
by: shank | last post by:
I'm trying to use online samples for submitting multiple records from ASP into a stored procedure. Failing! Through the below form, a user could be submitting many records at a time. I'm not...
1
17600
by: hellyna | last post by:
How to insert radiobutton value into the mysql? This is addrecordfrom.php <form name="addsummonrecord" method="post" action="addrecordprocess.php" id="signup" onSubmit="return...
3
5810
by: Me | last post by:
Trying to get the current status of the insert key. Found my.computer.keyboard.capslock, but no such item for insert key. Anyone have some code to help. Can't figure out getkeystate. compiler...
1
1514
by: rajasekar | last post by:
hi friends, this my code.. i want insert multiple record.. how can i? plz send me the solution.. <? for($i=0;$i<$count;$i++) { $n++; $id=mysql_result($res,$i,trainer); ...
7
3168
by: Jim Langston | last post by:
I'm working on a program, and at one time I find it necessary to load classes into a map. Now, these classes don't have default constructors, so I can't retrieve them using MyMap. So I wound...
5
3437
by: =?Utf-8?B?bXBhaW5l?= | last post by:
Hello, I am completely lost as to why I can't update a DropDownList inside a DetailsView after I perform an insert into an object datasource. I tried to simply it down to the core demostration:...
58
8013
by: bonneylake | last post by:
Hey Everyone, Well recently i been inserting multiple fields for a section in my form called "serial". Well now i am trying to insert multiple fields for the not only the serial section but also...
0
7111
by: studentofknowledge | last post by:
hi my knowledgeable comrads I have created a web form using html, css with the addition of some basic javascript validation. Although now im having trouble to insert this information to my local...
19
9074
by: obtrs | last post by:
I have some code for drop down its not working it gives the errors every thing looks fine to me help me out. Errors Notice: Undefined index: from in C:\wamp\www\site\booking.php on line 6 ...
1
6891
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
5465
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
4916
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
4595
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
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
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 ...
1
659
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.