use of unassigned variables

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 652 Views - Last Post: 22 September 2013 - 01:03 PM Rate Topic: -----

#1 RoxanneBillings  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 19-March 11

use of unassigned variables

Posted 22 September 2013 - 11:22 AM

I am receiving an error that states "use of unassigned local variable . . ." in my calculate formula. I believe it is has to do with the program not knowing which selection (variable) was selected in the list box, but have no idea how to fix this error as I am very new to this programming (and programming in general). Any assistance you can provide would be greatly appreciated!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Assignment_3_Training_Selector
{
    public partial class trainingSelector : Form
    {
        public trainingSelector()
        {
            InitializeComponent();
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            //Declare variables
            int courseDay;                //To hold the course number of days
            int courseRegistration;      //To hold the course registration fee
            int lodgingFee;              //To hold lodging fee
            int lodgingTotal;            //To hold total of lodging fee
            int grandTotal;              //To hold the total of all fees
            string course;               //To hold name of selected course
            string location;             //To hold name of selected location

            try
            {
                if (crseDaysRegistrationFeeListBox.SelectedIndex != -1)
                {
                    //Get selected course
                    course = crseDaysRegistrationFeeListBox.SelectedItem.
                        ToString();

                    switch (course)
                    {
                        case "Java":
                            courseDay = 3;
                            courseRegistration = 1000;
                            break;
                        case "Application Server":
                            courseDay = 3;
                            courseRegistration = 800;
                            break;
                        case "Business Intelligence":
                            courseDay = 3;
                            courseRegistration = 1100;
                            break;
                        case "Enterprise Management":
                            courseDay = 4;
                            courseRegistration = 1200;
                            break;
                        case "Data Integration":
                            courseDay = 2;
                            courseRegistration = 600;
                            break;
                    }
                }
                else
                {
                    //No course was selected
                    MessageBox.Show("Please select a course.");
                }
                if (locationLodgingFeeListBox.SelectedIndex != -1)
                {
                    //Get selected item
                    location = locationLodgingFeeListBox.SelectedItem.ToString();

                    switch (location)
                    {
                        case "Atlanta":
                            lodgingFee = 150;
                            break;
                        case "Chicago":
                            lodgingFee = 210;
                            break;
                        case "Dallas":
                            lodgingFee = 180;
                            break;
                        case "Los Angelas":
                            lodgingFee = 220;
                            break;
                        case "Orlando":
                            lodgingFee = 280;
                            break;
                    }
                }
                else
                {
                    //No location selected
                    MessageBox.Show("Please select a location.");
                }

                //Calculate totals
                lodgingTotal = lodgingFee * courseDay;
                grandTotal = courseRegistration + lodgingTotal;

                //Display results
                totalCostLabel.Text = "Registration:" + " " + courseRegistration.ToString("c")
                    + "\n" + "Lodging:" + " " + lodgingFee.ToString("c") + " " + "x" + " " + courseDay
                    + " " + "days" + " " + "=" + " " + courseRegistration.ToString("c") + "\n" +
                    "Total:" + " " + grandTotal.ToString("c");

            }
            catch (Exception ex)
            {
                //Display the default error message.
                MessageBox.Show(ex.Message);
            }
        }




    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: use of unassigned variables

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: use of unassigned variables

Posted 22 September 2013 - 11:32 AM

Simple solution would be to just default out all your local variables:

int courseDay = 0;
int courseRegistration = 0;
...
...
string course = string.Empty;
...


Also it should be said that there is really no reason to define all your variables at the beginning of the method and then use them after. It looks a lot easier when you join the declaration and usage (but be careful of scoping issues):

string course = crseDaysRegistrationFeeListBox.SelectedItem.
                        ToString();

Was This Post Helpful? 0
  • +
  • -

#3 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,498
  • Joined: 08-April 09

Re: use of unassigned variables

Posted 22 September 2013 - 11:33 AM

Initialize course to string.Empty and see what happens.

Offtopic, why does this site have so many timeouts?

This post has been edited by andrewsw: 22 September 2013 - 11:42 AM
Reason for edit:: casing

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • Joined: 12-December 12

Re: use of unassigned variables

Posted 22 September 2013 - 11:41 AM

View PostRyano121, on 22 September 2013 - 06:32 PM, said:

Also it should be said that there is really no reason to define all your variables at the beginning of the method and then use them after.

Off-topic, but personally I still prefer to do this - it makes no difference to the compiled code. I define variables at the top of the method, but not for things like loop counters. That is, I do this for variables that are relevant throughout the method. (It also helps me to plan my assault, and comment them in one place.)

I'm old skool ;)

This post has been edited by andrewsw: 22 September 2013 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

#5 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: use of unassigned variables

Posted 22 September 2013 - 11:44 AM

Reminds me too much of using Delphi...
Was This Post Helpful? 0
  • +
  • -

#6 RoxanneBillings  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 19-March 11

Re: use of unassigned variables

Posted 22 September 2013 - 11:50 AM

Thank you for the quick reply but after adding the suggested string.empty code, there was no change in my error. The error is coming from the calculation area of the code:

//Calculate totals
lodgingTotal = lodgingFee * courseDay;
grandTotal = courseRegistration + lodgingTotal;



and I believe it is because the program doesn't know which lodging fee or registration fee was selected from the list box. How would I code the program to realize which lodging fee and registration fee to calculate is the key question (I think)? Any help with how to do that specifically would be greatly appreciated.

Thank you, again.

This post has been edited by andrewsw: 22 September 2013 - 11:52 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: use of unassigned variables

Posted 22 September 2013 - 11:54 AM

Did you give lodgingTotal, lodgingFee etc default values of zero like I said. Just giving the strings = string.Empty is not enough - you need to initalise the int's as well.
Was This Post Helpful? 1
  • +
  • -

#8 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • Joined: 12-December 12

Re: use of unassigned variables

Posted 22 September 2013 - 11:56 AM

Use MessageBoxes to display the values of course, location, etc., to make sure it is displaying the correct values from the ListBoxes.

You need to assign lodgingTotal a default value as well.

This post has been edited by andrewsw: 22 September 2013 - 11:59 AM

Was This Post Helpful? 0
  • +
  • -

#9 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 349
  • View blog
  • Posts: 1,498
  • Joined: 08-April 09

Re: use of unassigned variables

Posted 22 September 2013 - 12:00 PM

Why don't you use default: in your switch case, instead you use
else
                {
                    //No course was selected
                    MessageBox.Show("Please select a course.");
                }


Ryano121, i thought that the value types like int already have default values.
Quote from MSDN

Quote

Each value type has an implicit default constructor that initializes the default value of that type.

This post has been edited by Michael26: 22 September 2013 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#10 RoxanneBillings  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 19-March 11

Re: use of unassigned variables

Posted 22 September 2013 - 12:03 PM

Okay, I did that and it took away the error but my display still isn't working and the calculation zero out now -- it still doesn't know which selection to choose from the list box. What do you think I should look at now? Here is what I have now . . .

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Assignment_3_Training_Selector
{
    public partial class trainingSelector : Form
    {
        public trainingSelector()
        {
            InitializeComponent();
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            //Declare variables
            int courseDay = 0;                //To hold the course number of days
            int courseRegistration = 0;      //To hold the course registration fee
            int lodgingFee = 0;              //To hold lodging fee
            int lodgingTotal = 0;            //To hold total of lodging fee
            int grandTotal = 0;              //To hold the total of all fees
            string course = String.Empty;               //To hold name of selected course
            string location = String.Empty;             //To hold name of selected location

            try
            {
                if (crseDaysRegistrationFeeListBox.SelectedIndex != -1)
                {
                    //Get selected course
                    course = crseDaysRegistrationFeeListBox.SelectedItem.
                        ToString();

                    switch (course)
                    {
                        case "Java":
                            courseDay = 3;
                            courseRegistration = 1000;
                            break;
                        case "Application Server":
                            courseDay = 3;
                            courseRegistration = 800;
                            break;
                        case "Business Intelligence":
                            courseDay = 3;
                            courseRegistration = 1100;
                            break;
                        case "Enterprise Management":
                            courseDay = 4;
                            courseRegistration = 1200;
                            break;
                        case "Data Integration":
                            courseDay = 2;
                            courseRegistration = 600;
                            break;
                    }
                }
                else
                {
                    //No course was selected
                    MessageBox.Show("Please select a course.");
                }
                if (locationLodgingFeeListBox.SelectedIndex != -1)
                {
                    //Get selected item
                    location = locationLodgingFeeListBox.SelectedItem.ToString();

                    switch (location)
                    {
                        case "Atlanta":
                            lodgingFee = 150;
                            break;
                        case "Chicago":
                            lodgingFee = 210;
                            break;
                        case "Dallas":
                            lodgingFee = 180;
                            break;
                        case "Los Angelas":
                            lodgingFee = 220;
                            break;
                        case "Orlando":
                            lodgingFee = 280;
                            break;
                    }
                }
                else
                {
                    //No location selected
                    MessageBox.Show("Please select a location.");
                }

                //Calculate totals
                lodgingTotal = lodgingFee * courseDay;
                grandTotal = courseRegistration + lodgingTotal;

                //Display results
                totalCostLabel.Text = "Registration:" + " " + courseRegistration.ToString("c")
                    + "\n" + "Lodging:" + " " + lodgingFee.ToString("c") + " " + "x" + " " + courseDay
                    + " " + "days" + " " + "=" + " " + courseRegistration.ToString("c") + "\n" +
                    "Total:" + " " + grandTotal.ToString("c");

            }
            catch (Exception ex)
            {
                //Display the default error message.
                MessageBox.Show(ex.Message);
            }
        }




    }
}



Thank you again for all the help.

This post has been edited by andrewsw: 22 September 2013 - 12:08 PM
Reason for edit:: Removed quote again

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • Joined: 12-December 12

Re: use of unassigned variables

Posted 22 September 2013 - 12:04 PM

@Michael

Quote

Remember that using uninitialized variables in C# is not allowed.


Quote

The following categories of variables are automatically initialized to their default values:
  • Static variables.
  • Instance variables of class instances.
  • Array elements.

This list does not include local variables.

This post has been edited by andrewsw: 22 September 2013 - 12:07 PM

Was This Post Helpful? 1
  • +
  • -

#12 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: use of unassigned variables

Posted 22 September 2013 - 12:06 PM

This is a prime usage of a debugger - step through your code and find out what variables have what values. The problem will then become very apparent.

There is a link to a tutorial on C# debugging in andrewsw' signature.

This post has been edited by Ryano121: 22 September 2013 - 12:09 PM

Was This Post Helpful? 2
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • Joined: 12-December 12

Re: use of unassigned variables

Posted 22 September 2013 - 12:10 PM

After this line:

course = crseDaysRegistrationFeeListBox.SelectedItem.
                        ToString();

add this:
MessageBox.Show(course);

and the same for location so that you can check what these values are.
Was This Post Helpful? 0
  • +
  • -

#14 RoxanneBillings  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 68
  • Joined: 19-March 11

Re: use of unassigned variables

Posted 22 September 2013 - 12:22 PM

Okay, I changed some code to make it get the correct variable from the list box so the calculations would work properly. At first, it seemed to work until I ran the debugger. Then I get the message "Input string was not in correct format" so I obviously did something incorrectly. Please help . . . this is my first real program in this class and am so confused! Thank you again for all the help so far.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Assignment_3_Training_Selector
{
    public partial class trainingSelector : Form
    {
        public trainingSelector()
        {
            InitializeComponent();
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            //Declare variables
            int courseDay = 0;                //To hold the course number of days
            int courseRegistration = 0;      //To hold the course registration fee
            int lodgingFee = 0;              //To hold lodging fee
            int lodgingTotal = 0;            //To hold total of lodging fee
            int grandTotal = 0;              //To hold the total of all fees
            string course = String.Empty;               //To hold name of selected course
            string location = String.Empty;             //To hold name of selected location

            try
            {
                if (crseDaysRegistrationFeeListBox.SelectedIndex != -1)
                {
                    //Get selected course
                    course = crseDaysRegistrationFeeListBox.SelectedItem.
                        ToString();
                    courseDay = int.Parse(crseDaysRegistrationFeeListBox.SelectedItem.
                        ToString());
                    courseRegistration = int.Parse(crseDaysRegistrationFeeListBox.SelectedItem.
                        ToString());

                    switch (course)
                    {
                        case "Java":
                            courseDay = 3;
                            courseRegistration = 1000;
                            break;
                        case "Application Server":
                            courseDay = 3;
                            courseRegistration = 800;
                            break;
                        case "Business Intelligence":
                            courseDay = 3;
                            courseRegistration = 1100;
                            break;
                        case "Enterprise Management":
                            courseDay = 4;
                            courseRegistration = 1200;
                            break;
                        case "Data Integration":
                            courseDay = 2;
                            courseRegistration = 600;
                            break;
                    }
                }
                else
                {
                    //No course was selected
                    MessageBox.Show("Please select a course.");
                }
                if (locationLodgingFeeListBox.SelectedIndex != -1)
                {
                    //Get selected item
                    location = locationLodgingFeeListBox.SelectedItem.ToString();
                    lodgingFee = int.Parse(locationLodgingFeeListBox.SelectedItem.ToString());

                    switch (location)
                    {
                        case "Atlanta":
                            lodgingFee = 150;
                            break;
                        case "Chicago":
                            lodgingFee = 210;
                            break;
                        case "Dallas":
                            lodgingFee = 180;
                            break;
                        case "Los Angelas":
                            lodgingFee = 220;
                            break;
                        case "Orlando":
                            lodgingFee = 280;
                            break;
                    }
                }
                else
                {
                    //No location selected
                    MessageBox.Show("Please select a location.");
                }

                //Calculate totals
                lodgingTotal = lodgingFee * courseDay;
                grandTotal = courseRegistration + lodgingTotal;

                //Display results
                totalCostLabel.Text = "Registration:" + " " + courseRegistration.ToString("c")
                    + "\n" + "Lodging:" + " " + lodgingFee.ToString("c") + " " + "x" + " " + courseDay
                    + " " + "days" + " " + "=" + " " + courseRegistration.ToString("c") + "\n" +
                    "Total:" + " " + grandTotal.ToString("c");

            }
            catch (Exception ex)
            {
                //Display the default error message.
                MessageBox.Show(ex.Message);
            }
        }




    }
}


Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • Joined: 12-December 12

Re: use of unassigned variables

Posted 22 September 2013 - 12:24 PM

When you get the error what line (in your posted code) does it take you to?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2