C# issue
Hey guys. I'm trying to write this program, but I'm having a bit of trouble. I'm getting the error 'Use of unassigned local variable dblAccumulatedCharges' and 'Use of unassigned local variable dblNumberOfGroups'. I'm pretty sure the reason that I'm getting this error is because I'm trying to use the value of a variable that I have not yet assigned a value to. Here is my code so far:
{ public partial class Form1 : Form { //Global Declarations: Accumulated Charges, Total Amount of Groups, and a constant for the Rental Rate double totalAccumulatedCharges = 0; double totalAmountOfGroups = 0; double rentalRate = 200 / 60;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnCalculate_Click(object sender, EventArgs e)
{
// Declarations: Input - Name, Minutes used.
// Output - Current charge, Group name, Accumulated Charges, Average Charge, Number of Groups
double dblMinutesUsed;
double dblAverageCharge, dblCurrentCharge, dblAccumulatedCharges, dblNumberOfGroups;
//Input - Gather the data
try
{
dblMinutesUsed = double.Parse(txtMinutesUsed.Text);
}
catch
{
MessageBox.Show("Input was either non-numeric or blank","Invalid Input!");
txtMinutesUsed.Focus();
return;
}
//Process - Calculate Current Charge, Accumulated Charges, Number of Groups, and Average charge
dblAccumulatedCharges += totalAccumulatedCharges;
dblNumberOfGroups += totalAmountOfGroups;
dblAverageCharge = dblAccumulatedCharges/dblNumberOfGroups;
dblCurrentCharge = dblMinutesUsed * rentalRate;
//Output - Display the information
//Clear all the output listboxes
lstAccumulatedCharges.Items.Clear();
lstAverageCharge.Items.Clear();
lstCurrentCharge.Items.Clear();
lstGroupName.Items.Clear();
lstNumberOfGroups.Items.Clear();
//Display the information
lstAccumulatedCharges.Items.Add(dblAccumulatedCharges.ToString("n1"));
lstAverageCharge.Items.Add(dblAverageCharge.ToString("n1"));
lstCurrentCharge.Items.Add(dblCurrentCharge.ToString("n1"));
lstGroupName.Items.Add(txtGroupName.Text);
lstNumberOfGroups.Items.Add(dblNumberOfGroups.ToString("n1"));
}
private void btnClear_Click(object sender, EventArgs e)
{
// Clear all input text boxes and output list boxes
txtGroupName.Clear();
txtMinutesUsed.Clear();
lstAccumulatedCharges.Items.Clear();
lstAverageCharge.Items.Clear();
lstCurrentCharge.Items.Clear();
lstGroupName.Items.Clear();
lstNumberOfGroups.Items.Clear();
txtGroupName.Focus();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
So, I believe the error to be caused be the 2 lines of code
dblAccumulatedCharges += totalAccumulatedCharges; dblNumberOfGroups += totalAmountOfGroups; because I'm trying to add totalAccumulatedCharges and totalAmountOfGroups to both dblAccumulatedCharges and dblNumberOfGroups, which have not yet been assigned a variable?
It would be great if someone could point me in the proper direction to solve this. Thanks. Also, sorry for the code looking jumbled - kind of went to hell when I pasted it in here.
This thread reminded me of three things;
- HBH's code tags are disregarding tabbing/spacing/code layout. This makes me raeg.
- CamelCase fucking sucks.
- Holy shit, it's C#.
OP, let me guess, school project? The thing you're trying to do can be done with Perl/Python, as a CLI script. 10x faster. 10x more useful. 10x readable.
Well, there are a lot of problems. Is this copy-pasted code?
The whole calculation needs to be re-written.
dblAccumulatedCharges += totalAccumulatedCharges;
dblNumberOfGroups += totalAmountOfGroups;
dblAverageCharge = dblAccumulatedCharges/dblNumberOfGroups;
dblCurrentCharge = dblMinutesUsed * rentalRate;```Is this the first program you've written?
I'll give you a hint:
This line:```markupdblAccumulatedCharges += totalAccumulatedCharges;```
is the same as:```markupdblAccumulatedCharges = dblAccumulatedCharges + totalAccumulatedCharges;```
dblAccumulatedCharges isn't initialized before use but, even if it was, it's a local variable and will therefore be reset to whatever it's default value is each time the button is pressed.
EDIT:
@bdafae:
```markupCode
indentation
works
perfectly
fine.```
PEBKAC
OP, let me guess, school project? The thing you're trying to do can be done with Perl/Python, as a CLI script. 10x faster. 10x more useful. 10x readable. Yeah. It's just something for one of my classes.
Is this the first program you've written? I'll give you a hint: This line: dblAccumulatedCharges += totalAccumulatedCharges; is the same as: dblAccumulatedCharges = dblAccumulatedCharges + totalAccumulatedCharges;
No, it's not the first program that I've written. I am aware that those two lines are interchangeable, which is why I didn't bother changing it. I was going to when I finished the program.
But thank you for your advice, I'll fiddle around with the calculations and see what I can come up with.
Dystopia wrote: [quote]Is this the first program you've written? I'll give you a hint: This line: dblAccumulatedCharges += totalAccumulatedCharges; is the same as: dblAccumulatedCharges = dblAccumulatedCharges + totalAccumulatedCharges;
No, it's not the first program that I've written. I am aware that those two lines are interchangeable, which is why I didn't bother changing it. I was going to when I finished the program.
But thank you for your advice, I'll fiddle around with the calculations and see what I can come up with.
[/quote]Yes, but my point was to point out what's wrong with that line.
This line:
markupdblAccumulatedCharges += totalAccumulatedCharges;
is equal to:markupdblAccumulatedCharges = 0 + 0
The same with all the other local variables besides dblCurrentCharge.
Another hint: totalAccumulatedCharges never gets set. ie. it will always be 0.
Alright. I've changed the declarations a bit and now when I run the program, it counts the number of times I have hit the calculate button aka total number of groups. The current charge also works correctly.
// Declarations: Input - Name, Minutes used. // Output - Current charge, Group name, Accumulated Charges, Average Charge, Number of Groups double dblMinutesUsed; double dblAverageCharge, dblCurrentCharge, dblAccumulatedCharges, dblNumberOfGroups; dblAccumulatedCharges = 0; dblNumberOfGroups = 1;
The accumulated charges is still not working. Neither is the average charge. But instead of coming up with 'NaN', I get a zero. Which means it must be calculating "0 divided by 1". But I'm not sure how to go about coding it properly so that it doesn't occur.
Here is my new process:
//Process - Calculate Current Charge, Accumulated Charges, Number of Groups, and Average charge dblCurrentCharge = dblMinutesUsed * rentalRate; dblAverageCharge = totalAccumulatedCharges / dblNumberOfGroups; dblAccumulatedCharges = dblCurrentCharge + totalAccumulatedCharges; dblNumberOfGroups += totalNumberOfGroups; totalNumberOfGroups++;
Dystopia wrote: Alright. I've changed the declarations a bit and now when I run the program, it counts the number of times I have hit the calculate button aka total number of groups. The current charge also works correctly.
[quote]// Declarations: Input - Name, Minutes used. // Output - Current charge, Group name, Accumulated Charges, Average Charge, Number of Groups double dblMinutesUsed; double dblAverageCharge, dblCurrentCharge, dblAccumulatedCharges, dblNumberOfGroups; dblAccumulatedCharges = 0; dblNumberOfGroups = 1;
The accumulated charges is still not working. Neither is the average charge. But instead of coming up with 'NaN', I get a zero. Which means it must be calculating "0 divided by 1". But I'm not sure how to go about coding it properly so that it doesn't occur.
Here is my new process:
//Process - Calculate Current Charge, Accumulated Charges, Number of Groups, and Average charge dblCurrentCharge = dblMinutesUsed * rentalRate; dblAverageCharge = totalAccumulatedCharges / dblNumberOfGroups; dblAccumulatedCharges = dblCurrentCharge + totalAccumulatedCharges; dblNumberOfGroups += totalNumberOfGroups; totalNumberOfGroups++;
[/quote]I think you need to do some (more?) reading on variable scope & lifetime. The only variable that is going to increase between button clicks is totalNumberOfGroups. The rest will be reset to their defaults. Besides that, you're taking the average too soon. It should be after you've calculated the accumulated charges and number of charges.
If you're really interested in programming, do yourself a favor: stop and do some more reading. You're missing some very basic concepts that are going to make it impossible for you to continue. This isn't meant as a flame; just try to help you. As I mentioned above, your current problems have to do with variable scope and variable lifetime. Reading more about that should help you solve your current problem.