Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 10th, 2008, 7:16 AM   #1
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 169
Rep Power: 1 BstrucT is on a distinguished road
Data between Forms

Hi guys.

One of the biggest hick - ups for me was trying to access data between forms using getters and setters
I also had a bit of trouble with MessageBoxes and certain file streaming IO functions.

I wrote a little application last night so that if I forget about one of these little issues, I can just go back and look again.

It may also prove handy for some newcomers out there who would like to know more about these things, maybe it won't.
The coding is definetely not top-notch, and the design sux, but here it is.

Description:

Application gets user information for a membership of some sort.
It then istantiates that info to another form, where the user can save the data.
(file will be written to default Debug folder.)
The file can then be opened or deleted or edited and saved again.

Probably not be a very practical example of where to use this type of stuff, but all comments will be appreciated. (That includes negative feedback, as that creates room for improvement)

>BstrucT
Attached Files
File Type: zip Working With Forms.zip (84.2 KB, 8 views)
__________________
The more the human race tries to change everything, when not needed, the less will they be able to change themselves when they need to.
BstrucT is offline   Reply With Quote
Old Jun 10th, 2008, 8:38 AM   #2
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 379
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Re: Data between Forms

2 issues :

csharp Syntax (Toggle Plain Text)
  1. public string _txtBoxAge2
  2. {
  3. set
  4. {
  5. lblAge.Text = value;
  6. }
  7. }
Why do you name properties starting with a _ and a lower case letter. The norm is to start with an uppercase letter. - UpperCamelCase -

and the seccond :
csharp Syntax (Toggle Plain Text)
  1. try
  2. {
  3. StreamWriter writeMember = new StreamWriter(
  4. lblName.Text);
  5.  
  6. //Specify strings needed to be written
  7. writeMember.WriteLine(lblName.Text);
  8. writeMember.WriteLine(lblAge.Text);
  9. writeMember.WriteLine(lblAddress.Text);
  10. writeMember.WriteLine(lblContact.Text);
  11. writeMember.WriteLine(lblMembership.Text);
  12.  
  13. //Close the stream
  14. writeMember.Close(); //[b]if an error appears before this you are not disposing of the writer[/b]
  15. }
  16. catch
  17. {
  18. MessageBox.Show(
  19. "Make sure all fields have been populated.",
  20. "Error",
  21. MessageBoxButtons.OK,
  22. MessageBoxIcon.Warning);
  23. }
  24. [b]finally{
  25. writeMember.Close(); // this should be here.
  26. } [/b]
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jun 10th, 2008, 11:34 AM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: Data between Forms

Issue #1 is not an issue, it's a preference.
OpenLoop is offline   Reply With Quote
Old Jun 10th, 2008, 1:30 PM   #4
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 379
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Re: Data between Forms

Actually it's more about convention. If most of the people use a certain style - it's best to use that style.

For example - when I look at something like : _someName - I instantly assume it's a private variable. And so will most of .net programmers.
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jun 10th, 2008, 5:51 PM   #5
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,005
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Data between Forms

Quote:
Originally Posted by xavier
Actually it's more about convention. If most of the people use a certain style - it's best to use that style.

For example - when I look at something like : _someName - I instantly assume it's a private variable. And so will most of .net programmers.
QFT. This is exactly what I do in my code, as a) it's completely unambiguous, and b) it's less typing (and less ugly) than the ubiquituous 'm_' prefix that some prepend.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jun 11th, 2008, 12:36 AM   #6
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 169
Rep Power: 1 BstrucT is on a distinguished road
Re: Data between Forms

Thanks for the tip xavier, I had a feeling the "_" wasn't a good idea, but wasn't sure how to write it. Much appreciated.

Oh my, I didn't even know there had to be a finally{} after the exception.
So I understand that if the error is catched, the execution will jump to the finally{} statement? I see now that I am disposing of the writer incorrectly.

@lectricpharaoh:
I have to agree, looks kinda ugly and out of place. hehe, will refrain from doing that again.

>BstrucT
__________________
The more the human race tries to change everything, when not needed, the less will they be able to change themselves when they need to.
BstrucT is offline   Reply With Quote
Old Jun 11th, 2008, 4:59 AM   #7
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 379
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Re: Data between Forms

Quote:
Oh my, I didn't even know there had to be a finally{} after the exception.
So I understand that if the error is catched, the execution will jump to the finally{} statement? I see now that I am disposing of the writer incorrectly.
No .
http://www.csharpfriends.com/Article...?articleID=128 here's an article .. you can go from there with help from google.
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Jun 13th, 2008, 3:27 AM   #8
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,005
Rep Power: 5 lectricpharaoh will become famous soon enough
Re: Data between Forms

Quote:
Originally Posted by BstrucT
Oh my, I didn't even know there had to be a finally{} after the exception.
So I understand that if the error is catched, the execution will jump to the finally{} statement? I see now that I am disposing of the writer incorrectly.
Nope. A finally block is usually to do cleanup. For example, say you want to open a file, read something, and then close the file. Loading configuration data at program start is a good example. Now, you wrap the read operation in a try block in case there's an I/O error or something, but whether or not an exception gets thrown, you still want to close the file, so you put that in the finally block.

In other words, whenever you have code that grabs/allocates a resource, uses it, and then releases/deallocates that resource, you might want to use a finally statement. It depends on the situation, but this is the most common case.

As for your original problem, yeah, just use properties. We don't call them 'getters' and 'setters' in C# most of the time, though (that's a term from languages like C++ and Java that don't support properties, so you need a method to do this if you want to maintain proper OO data hiding). What you can do is have one or more status properties on your secondary form(s) that you can check to determine whether other properties are valid. If you're opening a 'modal' dialog box- this is one that must be closed before you can interact with the main form, and is invoked with the ShowDialog() method- you can use the method's return value to determine status.

As an example, take the OpenFileDialog class. You instantiate it, call the ShowDialog() method, and after it returns, you can check the result to determine if the user selected a file, or instead clicked 'cancel' or hit 'esc'. If the user did not cancel the dialog box, then you can check the FileName or FileNames property to determine what file(s) they want to open. Basically, the idea here is to ensure the 'data' property actually contains something valid. If you try to open the file when they didn't select one, you've got a problem, so check the status first. This holds true in dialog boxes you create too.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jun 17th, 2008, 1:57 AM   #9
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 169
Rep Power: 1 BstrucT is on a distinguished road
Re: Data between Forms

Wow thanks man.

Quote:
What you can do is have one or more status properties on your secondary form(s) that you can check to determine whether other properties are valid. If you're opening a 'modal' dialog box- this is one that must be closed before you can interact with the main form, and is invoked with the ShowDialog() method- you can use the method's return value to determine status.

As an example, take the OpenFileDialog class. You instantiate it, call the ShowDialog() method, and after it returns, you can check the result to determine if the user selected a file, or instead clicked 'cancel' or hit 'esc'. If the user did not cancel the dialog box, then you can check the FileName or FileNames property to determine what file(s) they want to open.
This will help me alot, thanks for showing me, also I read up alot on Exeption handling this weekend and think I'va got it covered more or less.

Thanks for the good advice.

>BstrucT
__________________
The more the human race tries to change everything, when not needed, the less will they be able to change themselves when they need to.
BstrucT is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question regarding data input into PHP/Mysql using something other than GET davil PHP 6 Nov 20th, 2007 8:06 AM
convert unarrenged .txt data to arrange .txt data pastalover Visual Basic 2 Jun 23rd, 2006 4:22 PM
Recommended Practice for returning data from function Arla C# 1 Aug 16th, 2005 12:21 PM
help with sockets, having a client recieve data as well as send. cypherkronis Python 7 Jul 1st, 2005 5:59 PM
Passing Data between open forms? melbolt Visual Basic .NET 5 May 24th, 2005 9:27 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC