Members Online
Total Online: 22 Web Spiders: 14
Guests Online: 22
Members Online: 0
Registered Members: 70170 Newest Member: bahmx
|
View Thread
| Author |
C# issue |
Dystopia
Member

Posts: 22
Location: Localhost
Joined: 15.09.09 Rank: Moderate |
|
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.
|
|
| Author |
RE: C# issue |
Dystopia
Member

Posts: 22
Location: Localhost
Joined: 15.09.09 Rank: Moderate |
|
|
Well, I figured it out. The program now runs. However, when calculating TotalNumberOfGroups and AccumulatedCharges, they do not add up at all. If anyone has any suggestions, great. If not, this was a waste of a thread and I'll figure it out in time. |
|
| Author |
RE: C# issue |
bdafae
Banned
Posts: 30
Location:
Joined: 28.10.09 Rank: Apprentice |
|
This thread reminded me of three things;
1) HBH's code tags are disregarding tabbing/spacing/code layout. This makes me raeg.
2) CamelCase fucking sucks.
3) 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 scblockedript. 10x faster. 10x more useful. 10x readable. |
|
| Author |
RE: C# issue |
false
Member
Posts: 13
Location: (づの‿の)づ
Joined: 25.10.09 Rank: Active User |
|
Well, there are a lot of problems. Is this copy-pasted code?
The whole calculation needs to be re-written.
//Process - Calculate Current Charge, Accumulated Charges, Number of Groups, and Average charge
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:dblAccumulatedCharges += totalAccumulatedCharges;
is the same as:dblAccumulatedCharges = 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:
Code
indentation
works
perfectly
fine.
PEBKAC
curl -s http://www.hellboundhackers.org/ | sed -n '29s/^<div[^>]*>\(.*\)<\/div>$/\1/p'
Edited by false on 05-11-09 02:27 |
|
| Author |
RE: C# issue |
Dystopia
Member

Posts: 22
Location: Localhost
Joined: 15.09.09 Rank: Moderate |
|
|
OP, let me guess, school project?
The thing you're trying to do can be done with Perl/Python, as a CLI scblockedript. 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.
|
|
| Author |
RE: C# issue |
false
Member
Posts: 13
Location: (づの‿の)づ
Joined: 25.10.09 Rank: Active User |
|
Dystopia wrote:
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.
Yes, but my point was to point out what's wrong with that line.
This line:
dblAccumulatedCharges += totalAccumulatedCharges; is equal to:dblAccumulatedCharges = 0 + 0
The same with all the other local variables besides dblCurrentCharge.
Another hint: totalAccumulatedCharges never gets set. ie. it will always be 0.
curl -s http://www.hellboundhackers.org/ | sed -n '29s/^<div[^>]*>\(.*\)<\/div>$/\1/p'
|
|
| Author |
RE: C# issue |
Dystopia
Member

Posts: 22
Location: Localhost
Joined: 15.09.09 Rank: Moderate |
|
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++;
|
|
| Author |
RE: C# issue |
bdafae
Banned
Posts: 30
Location:
Joined: 28.10.09 Rank: Apprentice |
|
Guide to debugging for noobs;
1) Display variables
2) ???
3) PROFIT |
|
| Author |
RE: C# issue |
Dystopia
Member

Posts: 22
Location: Localhost
Joined: 15.09.09 Rank: Moderate |
|
...That doesn't really help me. But thanks. I'll figure it out in time.
/thread |
|
| Author |
RE: C# issue |
false
Member
Posts: 13
Location: (づの‿の)づ
Joined: 25.10.09 Rank: Active User |
|
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.
// 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++;
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.
curl -s http://www.hellboundhackers.org/ | sed -n '29s/^<div[^>]*>\(.*\)<\/div>$/\1/p'
|
|
| Author |
RE: C# issue |
Dystopia
Member

Posts: 22
Location: Localhost
Joined: 15.09.09 Rank: Moderate |
|
|
Thank you for your input guys. I was clearly being sloppy and missing some basics. I re-coded the entire thing and it works perfectly now. |
|
|
|
|