Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 09-11-2008, 07:56 PM   #1 (permalink)
 
*^[#@"'.,;:\|/:;,.'"@#]^*

Join Date: Dec 2006

Location: Leicester, England

Posts: 3,655

Oreo will become famous soon enoughOreo will become famous soon enough

Send a message via MSN to Oreo
Default Quick Nooby Question, C#

Okay so i'm running through some tutorials, and the guy goes on to explain how to do a very basic calculator.

So he goes though how to do it all and i end up with this:


int firstTextBox = 0;
int secondTextBox = 0;
int result = 0;

firstTextBox = int.Parse (textBox1.Text);
secondTextBox = int.Parse (textBox2.Text);

result = firstTextBox + secondTextBox;
label1.Text = result.ToString();

It works, but i'm just wondering that howcome no matter what i change the:


int firstTextBox = 0;
int secondTextBox = 0;
int result = 0;

0 numbers to it still works, even if i do this:


int firstTextBox = 7;
int secondTextBox =12;
int result = 4;

It will still work properly as a calculator, so i guess what im asking out of curiosity is what do them three 0's actually represent ?
Oreo is offline  
Old 09-11-2008, 08:34 PM   #2 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,466

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: Quick Nooby Question, C#

The zero's are being set as a safety measure. Before they are set to zero there's just random garbage from memory in each of those variables.

The reason it doesn't matter what you initialize them to is because you're setting them to the values of what's in the text boxes right after.
__________________

Baez is online now  
Old 09-12-2008, 10:38 AM   #3 (permalink)
 
*^[#@"'.,;:\|/:;,.'"@#]^*

Join Date: Dec 2006

Location: Leicester, England

Posts: 3,655

Oreo will become famous soon enoughOreo will become famous soon enough

Send a message via MSN to Oreo
Default Re: Quick Nooby Question, C#

Ohhh, thanks
Oreo is offline  
Old 09-12-2008, 10:51 AM   #4 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: Quick Nooby Question, C#

Quote:
Before they are set to zero there's just random garbage from memory in each of those variables.
This is C# we're talking about here. Class variables are initialized to 0 (or null if an object, and false if a boolean) by the CLR when the class is instantiated, so there's never any random garbage in the values. If the programmer manually initializes the values of class variables to 0, the C# compiler will emit the extra instructions to set memory to 0 again, which is inefficient. So it's not wrong, but it's definitely inefficient.
jaeusm is offline  
 
Closed Thread

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Quick Question about Firefox... digital_ninja2k Browser & General Internet Questions 15 06-02-2008 12:43 PM
a quick question here tmd07 Building, Buying, Upgrading for General PCs 5 02-20-2008 08:50 PM
Quick Ram Question (Please at least look at) Higach Hardware Troubleshooting 2 01-06-2008 02:06 AM
Quick case question sailpog Building, Buying, Upgrading for General PCs 6 10-05-2007 12:21 PM
Very quick question my friends psychedelos Building, Buying, or Upgrading High Performance PC Systems 7 06-13-2007 03:00 PM