Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional C# 2008 ISBN: 978-0-470-19137-8
This is the forum to discuss the Wrox book Professional C# 2008 by Christian Nagel, Bill Evjen, Jay Glynn, Morgan Skinner, Karli Watson; ISBN: 9780470191378
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C# 2008 ISBN: 978-0-470-19137-8 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #11 (permalink)  
Old October 12th, 2008, 02:54 PM
Friend of Wrox
Points: 1,104, Level: 13
Points: 1,104, Level: 13 Points: 1,104, Level: 13 Points: 1,104, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 224
Thanks: 0
Thanked 30 Times in 29 Posts
Default

I'm going to wade in here, not because the question hasn't been answered, but because this is such a common problem for many programmers.

First, develop a naming convention for the form that controls the program. As Old Pendant states, this is the one that shares the EXE name. During development, I always call this form frmMain since it contains the Main() method. I tend to make frmMain() an MDI form and use it as the parent for all other (child) forms.

Second, following OOP practices, I don't like "global data". If you need a Demo object (using Handman's narrative), frmMain should have that object defined within frmMain, such as:

public class frmMain()
{
   Demo myDemoObject = new Demo();

   // more code
} // End frmMain code

Handman: You got the "doesn't exist in current context" because you forgot the parentheses after the word Demo at the end of the definition of myDemoObject. Always remember: the new keyword causes a call to the Windows Memory Manager to get enough memory to hold a Demo object. Assuming there is enough free memory, the subsequent call to Demo() calls the constructor to initialize that chunk of "Demo" memory to the default values for all data in the Demo object. That is, value types are initialized to zero and all reference types (e.g., strings) are initialized to null. The only reason to write your own constructor is when you're not happy with these default values.

Third, assuming you want to pass your myDemoObject to a different form named frmNextForm and assuming there's a button called btnNextForm, a simple way to do this is to add the following code to frmMain:

public class frmMain()
{
   Demo myDemoObject = new Demo();

   // more code

private void btnNextForm(object sender, EventArgs e)
   {
      frmNextForm frm = new frmNextForm(myDemoObject);
      frm.ShowDialog();
   }

} // End frmMain code

Note how we are passing your object, myDemoObject, to the next form at the time frmNextFrom's constructor is called. Keep in mind that myDemoObject is nothing but a memory reference, or pointer, to the myDemoObject data.

The code in the frmNextForm would look like:

public class frmNextForm()
{
    Demo refDemoData; // Create a reference for a Demo object

    // Definitons for other data in frmNextForm

public frmNextForm(Demo demoDataFromFrmMain) //Constructor
    {
       refDemoData = demoDataFromFrmMain; // Magic here!
    }
     // Other methods in this class
}

Note that the memory reference from the Demo object in frmMain is assigned into refDemoData in the second form. However, because refDemoData now contains the same memory address as does myDemoObject in frmMain, anything you do to refDemoData is actually changing myDemoObject in frmMain because they both point to the same location in memory. This means that anything you do to refDemoData in the second form is permanently recorded in the myDemoObject in frmMain because they are, in fact, the same object.

While this is perhaps an H-bomb to kill an ant, read it 10 times and then write the code and single-step through that code and you'll see exactly what I mean.

I hope this helps rather than hinders. (If you have access to my book, check pp.388-394.)

Dr. Purdum



Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
Reply With Quote
  #12 (permalink)  
Old October 19th, 2008, 10:28 PM
Registered User
 
Join Date: Oct 2008
Location: Roseburg, OR, USA.
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much for your help, Dr. Purdum and Old Pedant. I am sorry it has taken me a while to respond to you, but I have been letting this information sink in a bit. Dr. Purdum, your most recent response was quite helpful to me; in fact I feel a bit silly that I didn't think of using a new constructor to pass in the object as a parameter.

However, I remain a bit frustrated with the many cryptic error messages that I get in C#. I am starting to question that a hobbyist programmer can ever develop enough skill with the language to make it useful. Am I in over my head?

Despite the above paragraph, my ego will not let me give up yet. In fact, I have ordered your book, Dr. Purdum, to help me get more adept at C#.

Thanks again.

Reply With Quote
  #13 (permalink)  
Old October 20th, 2008, 10:06 AM
Friend of Wrox
Points: 1,104, Level: 13
Points: 1,104, Level: 13 Points: 1,104, Level: 13 Points: 1,104, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 224
Thanks: 0
Thanked 30 Times in 29 Posts
Default

I think Old Pendant would agree that C# can be learned by almost anyone who takes a disciplined approach to it. My "worst" students are those who learned a language on their own and come into my class with a "I-know-everything-already" attitude. Often, this means "unlearning" some bad programming habits and preconceived notions of how things work.

I think you'll find my book useful, but please read from start to end. (You can probably skip Chapter 1 since that's more about installing the software than anything else.) I'd love to hear your comments when your finished.

Dr. Purdum


Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
Reply With Quote
Reply


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Static Scope JaneDean Beginning PHP 0 November 25th, 2008 01:07 AM
Scope Issue iceman90289 C# 2005 8 April 5th, 2008 03:41 PM
"Out of scope" var??? yankleber XSLT 2 March 31st, 2007 03:53 AM
scope problem jc Javascript 0 July 29th, 2003 09:27 AM



All times are GMT -4. The time now is 07:57 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.