13 Replies - 497 Views - Last Post: 07 October 2013 - 05:29 AM Rate Topic: -----

#1 phorton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-September 13

trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 11:33 AM

I am working on a school project, and using switch and int variables.

Need to display code in label but getting Use of unassigned local variable is the error in my calculations.

Any help is appreciated...
Is This A Good Question/Topic? 0
  • +

Replies To: trying to get a listbox to display to a label (using switch)

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 11:35 AM

Post the relevant code clearly stating which line the error appears on.
Was This Post Helpful? 0
  • +
  • -

#3 Michael26  Icon User is offline

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

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

Re: trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 11:36 AM

Post your code, and we'll see what can be done.
Was This Post Helpful? 0
  • +
  • -

#4 phorton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-September 13

Re: trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 11:36 AM

View Postphorton, on 22 September 2013 - 11:33 AM, said:

I am working on a school project, and using switch and int variables.

Need to display code in label but getting Use of unassigned local variable is the error in my calculations.

Any help is appreciated...

here is code...

private void btnCalc_Click(object sender, EventArgs e)
{
//Declare variables
int Day; //To hold the course number of days
int Registration; //To hold the course registration fee
int Fee; //To hold lodging fee
int FeeTotal; //To hold total of lodging fee
int Total; //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 (lstCourse.SelectedIndex != -1)
{
//Get selected course
course = lstCourse.SelectedItem.
ToString();

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

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

//Calculate totals
FeeTotal = Fee * Day;
Total = Registration + FeeTotal;

//Display results
lblTotal3.Text = "Registration:" + " " + Registration.ToString("c")
+ "\n" + "Lodging:" + " " + Fee.ToString("c") + " " + "x" + " " + Day
+ " " + "days" + " " + "=" + " " + Registration.ToString("c") + "\n" +
"Total:" + " " + Total.ToString("c");

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

Was This Post Helpful? 0
  • +
  • -

#5 Michael26  Icon User is offline

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

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

Re: trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 11:37 AM

Could you post that code in code tags?
Was This Post Helpful? 0
  • +
  • -

#6 phorton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-September 13

Re: trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 11:38 AM

Error is here:

//Calculate totals
FeeTotal = Fee * Day;
Total = Registration + FeeTotal;
Was This Post Helpful? 0
  • +
  • -

#7 Michael26  Icon User is offline

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

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

Re: trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 11:39 AM

What does it say, the error.
Was This Post Helpful? 0
  • +
  • -

#8 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 11:43 AM

You need to give default values to the local variables:

int Fee = 0;
...
int FeeTotal = 0;
...
string course = string.Empty;


On a side note this looks a lot like this thread
Was This Post Helpful? 0
  • +
  • -

#9 phorton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-September 13

Re: trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 11:45 AM

 private void btnCalc_Click(object sender, EventArgs e)
        {
            //Declare variables
            int Day;                //To hold the course number of days
            int Registration;      //To hold the course registration fee
            int Fee;              //To hold lodging fee
            int FeeTotal;            //To hold total of lodging fee
            int Total;              //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 (lstCourse.SelectedIndex != -1)
                {
                    //Get selected course
                    course = lstCourse.SelectedItem.
                        ToString();

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

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

                //Calculate totals
                FeeTotal = Fee * Day;
                  Total = Registration + FeeTotal;

                //Display results
                  lblTotal3.Text = "Registration:" + " " + Registration.ToString("c")
                     + "\n" + "Lodging:" + " " + Fee.ToString("c") + " " + "x" + " " + Day
                     + " " + "days" + " " + "=" + " " + Registration.ToString("c") + "\n" +
                     "Total:" + " " + Total.ToString("c");

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


View PostMichael26, on 22 September 2013 - 11:39 AM, said:

What does it say, the error.


Use of unassigned local variable is the error in my calculations

View PostRyano121, on 22 September 2013 - 11:43 AM, said:

You need to give default values to the local variables:

int Fee = 0;
...
int FeeTotal = 0;
...
string course = string.Empty;


On a side note this looks a lot like this thread



let me try this...
Was This Post Helpful? 0
  • +
  • -

#10 phorton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-September 13

Re: trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 11:54 AM

View PostRyano121, on 22 September 2013 - 11:43 AM, said:

You need to give default values to the local variables:

int Fee = 0;
...
int FeeTotal = 0;
...
string course = string.Empty;


On a side note this looks a lot like this thread


HA...that is my classmate, we are working together...too funny, she just told me about this site.
Was This Post Helpful? 0
  • +
  • -

#11 phorton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-September 13

Re: trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 12:08 PM

WOW...this is great...yes, the display is working...

the totals are not calculating, but let me work on that...

Thank you so much!!!
Was This Post Helpful? 0
  • +
  • -

#12 phorton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-September 13

Re: trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 04:58 PM

I still can't get the display to calculate. I was trying to attach an image of what it is depicting, but can't seem to figure that out?

 private void btnCalc_Click(object sender, EventArgs e)
        {
            //Declare variables
            int Day = 0;                //To hold the course number of days
            int Registration = 0;      //To hold the course registration fee
            int Fee = 0;              //To hold lodging fee
            int FeeTotal = 0;            //To hold total of lodging fee
            int Total = 0;              //To hold the total of all fees
  

            string course = string.Empty;               //To hold name of selected course
            string location;             //To hold name of selected location

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

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

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

                //Calculate totals
                FeeTotal = Fee * Day;
                  Total = Registration + FeeTotal;

                //Display results
                  lblTotal3.Text = "Registration:" + " " + Registration.ToString("c")
                     + "\n" + "Lodging:" + " " + Fee.ToString("c") + " " + "x" + " " + Day
                     + " " + "days" + " " + "=" + " " + Registration.ToString("c") + "\n" +
                     "Total:" + " " + Total.ToString("c");

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

Attached Image

This post has been edited by andrewsw: 23 September 2013 - 01:26 AM
Reason for edit:: Tidied post

Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3470
  • View blog
  • Posts: 10,703
  • Joined: 05-May 12

Re: trying to get a listbox to display to a label (using switch)

Posted 22 September 2013 - 08:07 PM

If you set a breakpoint on line 22, what is the value of the course variable?
Was This Post Helpful? 0
  • +
  • -

#14 phorton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-September 13

Re: trying to get a listbox to display to a label (using switch)

Posted 07 October 2013 - 05:29 AM

Just a follow-up....I changed the case to 0, 1, 2, etc, added %% lstCourse.SelectedIndex != -1 to the if statement, and cleaned up the code a bit, and all worked great...thank you so much for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1