To anyone creating a C# program for intro C# class.
My program is suppose to calculate shipping cost when user chooses either two radio buttons,and choose from the list box with the shipping cost, while typing in the distance.
But while doing the code I get the error message Use of unassigned local variable 'rate'
is there a way to define the variable into the if statement.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace HW3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            double shipping_cost, distance,rate;
            distance = double.Parse(textBox2.Text);
            if (radioButton1.Checked)


                switch (listBox1.SelectedIndex)
                {
                    case 0:
                        rate = 1.0;
                        break;
                    case 1:
                        rate = 2.0;
                        break;
                    case 2:
                        rate = 2.5;
                        break;
                    case 4:
                        rate = 3.0;
                        break;



                }


            shipping_cost = distance * rate;
            textBox1.Text = shipping_cost.ToString("C");



            if (radioButton2.Checked)
                switch (listBox1.SelectedIndex)
                {
                    case 0:
                        rate = 1.5;
                        break;
                    case 1:
                        rate = 2.5;
                        break;
                    case 2:
                        rate = 3.0;
                        break;
                    case 3:
                        rate = 3.5;
                        break;


                }



            {




            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

Here is the code you need to look at:

double shipping_cost, distance,rate;
distance = double.Parse(textBox2.Text);
if (radioButton1.Checked)
    switch (listBox1.SelectedIndex) {
        case 0: rate = 1.0; break;
        case 1: rate = 2.0; break;
        case 2: rate = 2.5; break;
        case 4: rate = 3.0; break;
    }

shipping_cost = distance * rate;

The compiler is telling you that it is possible for 'rate' to not have a value when it reaches the shipping_cost line (for example, if the radio buttion isn't checked). You should always initialize variables. You can change line 1 in this block to double shipping_cost, distance, rate = 0;

Now a comment on style: the if statement should have {} to surround the switch. Yes, it works without them and isn't required, but this type of thing will eventually lead to bugs when you want to add a line to the if block but forget the {}. Always use {} on anything that requires more than 1 line to express, i.e.

if (myVar == 3) b += 5;  // OK! No {} needed
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.