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)
{
}
}
}