Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 4.0 aka C# 2010 > BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 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
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 24th, 2012, 09:50 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Location: Australia
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"
  #2 (permalink)  
Old October 25th, 2012, 04:00 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
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)
  #3 (permalink)  
Old October 25th, 2012, 08:22 PM
Authorized User
Points: 130, Level: 2
Points: 130, Level: 2 Points: 130, Level: 2 Points: 130, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Location: Australia
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"
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
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



All times are GMT -4. The time now is 05:39 PM.


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