Wrox Programmer Forums
|
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 24th, 2012, 09:50 PM
Authorized User
 
Join Date: Oct 2012
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default Lesson 9 Try It (declaring variable before opening forms)

Hi,

Just a quick question.

Is it always necessary to create a variable before opening another form?

example:
Code:
private void Form1_Load(object sender, EventArgs e)
        {
            //initialise the form variables but dont display them

            theGettingThereForm = new GettingThereForm();
            theGettingAroundForm = new GettingAroundForm();
            theLodgingForm = new LodgingForm();
            theFunStuffForm = new FunStuffForm();

        }
        
        private void btnGettingThere_Click(object sender, EventArgs e)
        {
            //On button Click, open a new form instance

            theGettingThereForm.Show();

        }
Can you not create a variable and just open the form directly? like below

Code:
GettingThereForm.Show();
Just curious...(might save some typing)


Also, the book wasnt clear on how to get the Load event handler into the code. I found it by double clicking Load in the form1 design events list. I dont recall seeing it mentioned in the book. Maybe I missed it.

I am loving this book, learning so much
__________________
"These pretzels are making me thirsty"
 
Old October 25th, 2012, 04:00 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

The short answer is, "Yes you must make a variable of the form's type and then display that."

When you add a form to a project, you are defining the type of the form. (The form's class.) You don't actually have an instance of the form.

It's sort of like the int data type defines what an integer is but it doesn't actually make a particular integer.

When you do something like this:

Code:
theGettingThereForm = new GettingThereForm();
you're telling the program to make an instance of the form. Now you can display that instance.

You could also make several variables refer to different instances of the form (just like you can have several int variables that represent different values) and display some or all of them.

Note that if you try to say GettingThereForm.Show() you get a syntax error.

Also you could say (new Form2()).Show() but then you wouldn't have a variable referring to the form so the code couldn't interact with it later.

Aside: In Visual Basic there is a special instance of each form with the same name as the form so you can basically do what you suggest. I have never liked that special instance because it often confuses programmers so I never use it.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
The Following User Says Thank You to Rod Stephens For This Useful Post:
MinusZero (October 25th, 2012)
 
Old October 25th, 2012, 08:22 PM
Authorized User
 
Join Date: Oct 2012
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Ahh, thanks.

Quote:
Originally Posted by Rod Stephens View Post
When you do something like this:

Code:
theGettingThereForm = new GettingThereForm();
you're telling the program to make an instance of the form. Now you can display that instance.
Makes sense now, I was thinking the form was already an instance and didnt need to be made (new) to be able to be displayed.
__________________
"These pretzels are making me thirsty"





Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 10 - Result Set: must provide a value to the new variable. samirayes BOOK: Knight's 24-Hour Trainer: Microsoft SQL Server 2008 Integration Services 1 January 26th, 2014 10:18 AM
Lesson 9: Initializing new forms? killinger BOOK: Stephens' Visual Basic Programming 24-Hour Trainer 1 October 21st, 2012 06:42 PM
pp 136 - 137: declaring variable in the switch statement ... shivesh BOOK: Ivor Horton's Beginning Visual C++ 2010 3 March 28th, 2012 10:19 AM
Lesson 3: Variable Scope dthoma128 BOOK: Java Programming 24-Hour Trainer by Yakov Fain 0 October 5th, 2011 11:27 AM
error in declaring the scalar variable n_srikanth4 General .NET 0 November 2nd, 2006 05:40 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.