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 November 26th, 2012, 09:15 PM
Authorized User
 
Join Date: Oct 2012
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default Variables and Format Codes

Hi,

Maybe its been too long since I went through the variables chapter, but I am having problems making a variable for a format code.

What i want to do:

I have made a unit converter and want to add a combo box to select the amount of decimal points for the answer. Basically, I want the variable to fill in the correct code based on selection.

Code:
tbxTempCel.Text = celsius.ToString();
When selected, i want a variable for the combo box selection so the variable is equal to F1 for instance. The code would be equivalent to
Code:
tbxTempCel.Text = celsius.ToString("F1");
Code:
private void btnConvertTemp_Click(object sender, EventArgs e)
        {
            if (decimalSelectToolStripMenuItem.Text == "0")
            {
                string dpoint = "F0";
            }

            else
                if (decimalSelectToolStripMenuItem.Text == "1")
                {
                    string dpoint = "F1";
                }

                else
                    if (decimalSelectToolStripMenuItem.Text=="2")
                    {
                        string dpoint="F2";
                    }



            //CELSIUS
            //check the user input text box, if it is empty, display a warning message
            if (tbxTempToConvert.Text == "")
            {
                MessageBox.Show("Enter a number to convert!", "Data Required", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

            else
                //if the radio button is checked, do the calculation and display the string
                if (cboTemp.Text == "Celsius (°C)")
                {
                    //parse the information in the text box and change to an integer (double). Define temperature variable
                    double temperature = double.Parse(tbxTempToConvert.Text);
                    //define variables and use temperature (textbox) to calculate the answer
                    double celsius = temperature * 1;
                    double fahrenheit = (temperature * 1.8) + 32;
                    double kelvin = temperature + 273.15;

                    //convert the answer to a string and enter it in the appropriate text box.
                    tbxTempCel.Text = celsius.ToString(dpoint);
                    tbxTempFahr.Text = fahrenheit.ToString(dpoint);
                    tbxTempKelv.Text = kelvin.ToString(dpoint);

                }
The string is accepted without errors, but the variable name has an error in the ToString format brackets. "The name 'dpoint' does not exist in the current context"

Will it not accept my variable name because it isnt contained in the same if statement?
__________________
"These pretzels are making me thirsty"
 
Old November 27th, 2012, 12:22 AM
Authorized User
 
Join Date: Oct 2012
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default *Rolls Eyes*

Why dont I try harder?

I figured it out in a different way.

I forgot the ifs and simply added a concatenation.

Code:
string dpoint2 = "F" + decimalSelectToolStripMenuItem.Text;
tbxTempCel.Text = celsius.ToString(dpoint2);
So now the string is made up of the letter F and whatever is selected in the combo. So if 2 is selected the format code becomes F2 and shows two decimal places.

So simple.
__________________
"These pretzels are making me thirsty"
 
Old November 27th, 2012, 12:37 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Glad I could help!

Seriously it's good that you figured out a way around this and the way you found is a fairly subtle but powerful method. You can compose format strings in code, concatenating all sorts of stuff together, and then use them to format stuff. You can even do it with the more complicated formats you get when using String.Format.

As for your original question about why the code couldn't find dpoint, it's because you declared it inside { brackets }. That means it only exists within those brackets and disappears with the closing bracket.

Code:
if (decimalSelectToolStripMenuItem.Text == "0")
{
    string dpoint = "F0";
}
else if (decimalSelectToolStripMenuItem.Text == "1")
{
    string dpoint = "F1";
}
else if (decimalSelectToolStripMenuItem.Text=="2")
{
    string dpoint="F2";
}
For example, is the menu item's Text is 2, then the last section executes, creates a variable named dpoint, sets it equal to F2, and then exits so dpoint goes out of scope and disappears.

In the following version the variable is declared outside of the if-then statements so it still exists when they end.

Code:
string dpoint = "F0";

if (decimalSelectToolStripMenuItem.Text == "0")
{
    dpoint = "F0";
}
else if (decimalSelectToolStripMenuItem.Text == "1")
{
    dpoint = "F1";
}
else if (decimalSelectToolStripMenuItem.Text=="2")
{
    dpoint="F2";
}
Now the following code can use dpoint.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old November 27th, 2012, 09:54 PM
Authorized User
 
Join Date: Oct 2012
Posts: 32
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Ah, thanks for the explanation.

One other thing, on my converter when using the F2 format it rounds off numbers. For example, when i convert 698 millimetres using two decimal places, it shows the answer as 0.70 metres. Is there a format code to not round off the numbers? That way, if I want a more accurate figure then i have to add more decimal points.
__________________
"These pretzels are making me thirsty"
 
Old November 28th, 2012, 12:38 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Well F2 means 2 decimal places so if the value has more it needs to round it off to make it have only 2.

You can use F3 to get 3 decimal places. You can use Math.Round to round off to a given number of decimal places.

The G format gives you "The most compact of either fixed-point or scientific notation." but it's default precision for different types is larger than F. See:

http://msdn.microsoft.com/en-us/libr...#GFormatString

So in this case, I think you want G.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sending xml format through email, format issue!!! abhishekkashyap27 ASP.NET 2.0 Professional 0 December 14th, 2010 07:35 AM
date format differs, need to force format somehow patricolsson ASP.NET 2.0 Basics 1 December 3rd, 2009 12:53 AM
Convert British format date to American format? fyr PHP How-To 0 December 19th, 2007 03:17 PM
Convert Rich Text Format to Ordinary Format in ASP Andraw Classic ASP Basics 1 August 31st, 2007 10:05 AM
date format differs, need to force format somehow patricolsson HTML Code Clinic 2 January 12th, 2006 05:55 AM





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