C
C#ā€¢2w ago
Nightswyng

Workshop calculate not working

Here is my code both in form1 and designer. I know I messed up and had a dislexic moment and switched two words around but when i try to fix it, it gives me errors. But I also cant get the calculate button to work when running the program. I just cant seem to get these assignments with calculations to work. https://paste.mod.gg/eykezrwgbwdh/0
BlazeBin - eykezrwgbwdh
A tool for sharing your source code with the world!
No description
No description
No description
54 Replies
Pobiega
Pobiegaā€¢2w ago
Can you be more specific about what doesnt work and how it doesnt? ie, what is the expected result, and what are you seeing I'm seeing some unusual variable names thou, that might cause some confusion..
double dayslabel;
double registrationFeeLabel;
double lodgingFeeLabel;
double dayslabel;
double registrationFeeLabel;
double lodgingFeeLabel;
these are just variables for storing floating point numbers, they are in no way connected to their respective labels also, all those variables are declared in the class member scope, but are only ever used as local variables in the calculateButton_Click method @Nightswyng
Nightswyng
NightswyngOPā€¢2w ago
Apologies had to step away
Nightswyng
NightswyngOPā€¢2w ago
so, i deleted the errors, but when I had the label double lodgingTotalLabel it is showing as unused
No description
Nightswyng
NightswyngOPā€¢2w ago
when i tried to change it to the totalLodgingLabel it would give me an error
Pobiega
Pobiegaā€¢2w ago
All those variables are declared in the wrong place, if you ask me. Every single one.
Nightswyng
NightswyngOPā€¢2w ago
No description
Pobiega
Pobiegaā€¢2w ago
And that switch is a mess too :p set the value with a switch expression, then update the label afterwards will cut that down a lot in size and complexity
Nightswyng
NightswyngOPā€¢2w ago
I am not sure what you mean
Pobiega
Pobiegaā€¢2w ago
before
switch (selectedLocation)
{
case "Austin":
lodgingFeeLabel = 150;
totalLodgingLabel.Text = "$150";
break;
case "Chicago":
lodgingFeeLabel = 225;
totalLodgingLabel.Text = "$225";
break;
case "Dallas":
lodgingFeeLabel = 175;
totalLodgingLabel.Text = "$175";
break;
case "Orlando":
lodgingFeeLabel = 300;
totalLodgingLabel.Text = "$300";
break;
case "Phoenix":
lodgingFeeLabel = 175;
totalLodgingLabel.Text = "$175";
break;
case "Raleigh":
lodgingFeeLabel = 150;
totalLodgingLabel.Text = "$150";
break;
}
switch (selectedLocation)
{
case "Austin":
lodgingFeeLabel = 150;
totalLodgingLabel.Text = "$150";
break;
case "Chicago":
lodgingFeeLabel = 225;
totalLodgingLabel.Text = "$225";
break;
case "Dallas":
lodgingFeeLabel = 175;
totalLodgingLabel.Text = "$175";
break;
case "Orlando":
lodgingFeeLabel = 300;
totalLodgingLabel.Text = "$300";
break;
case "Phoenix":
lodgingFeeLabel = 175;
totalLodgingLabel.Text = "$175";
break;
case "Raleigh":
lodgingFeeLabel = 150;
totalLodgingLabel.Text = "$150";
break;
}
after
lodgingFee = selectedLocation switch
{
"Austin" => 150,
"Chicago" => 225,
"Dallas" => 175,
"Orlando" => 300,
"Phoenix" => 175,
"Raleigh" => 150,
_ => throw new ArgumentOutOfRangeException() // Invalid location selected
};
totalLodgingLabel.Text = $"${lodgingFee}";
lodgingFee = selectedLocation switch
{
"Austin" => 150,
"Chicago" => 225,
"Dallas" => 175,
"Orlando" => 300,
"Phoenix" => 175,
"Raleigh" => 150,
_ => throw new ArgumentOutOfRangeException() // Invalid location selected
};
totalLodgingLabel.Text = $"${lodgingFee}";
which one is easier to read and maintain?
Nightswyng
NightswyngOPā€¢2w ago
the second one is alot cleaner can you explain that last line $"${lodgingFee}";
Pobiega
Pobiegaā€¢2w ago
sure, the first dollar sign is BEFORE the string quotation mark
Pobiega
Pobiegaā€¢2w ago
that means that string is using string interpolation
$ - string interpolation - format string output - C# reference
String interpolation using the $ token provides a more readable and convenient syntax to format string output than traditional string composite formatting.
Pobiega
Pobiegaā€¢2w ago
a fancy way of saying, I want to "inject" some variables into the string think of {lodgingFee} as a placeholder that will be replaced with the value of lodingFee so it becomes $150 for example
Nightswyng
NightswyngOPā€¢2w ago
if i throw it in to VS, I would have to rewrite everything because its giving me so many errors, so how do I clean this up to work? also with the current code, when I run the program and select each item, hit calculate nothing works or shows up in the 4 boxes Am I using the wrong thing(label)?
Pobiega
Pobiegaā€¢2w ago
I can't tell, because you didn't show that part of the code oh you did, you just didnt give it a filename. okay I see lemme look I don't see any binding to the click event
Nightswyng
NightswyngOPā€¢2w ago
oh wow I see that now, there is no event
Pobiega
Pobiegaā€¢2w ago
ie, your button doesnt actually invoke the method
Nightswyng
NightswyngOPā€¢2w ago
how did that happen?
Pobiega
Pobiegaā€¢2w ago
ĀÆ\_(惄)_/ĀÆ also, I think you have several calculation issues too totalCost = lodgingFeeLabel * registrationFeeLabel; doesn't seem right lodingfee TIMES registrationfee? also, you update theactual lodgingfee label twice both in the switch, with hardcoded values, then later with the calculated one (* days) but ppleeease rename your variables lol daysLabel is a baaad name. numberOfDays is muuuch better
Nightswyng
NightswyngOPā€¢2w ago
I was going off the directions and previous instruction I will change it
Pobiega
Pobiegaā€¢2w ago
oh, and also look at this beauty
var (numberOfDays, registrationFee) = selectedWorkshop switch
{
"Handling Stress" => (3, 1000),
"Time Management" => (3, 800),
"Supervision Skills" => (3, 1500),
"Negotiation" => (5, 1300),
"How to Interview" => (1, 500),
_ => throw new ArgumentOutOfRangeException() // Invalid workshop selected
};
var (numberOfDays, registrationFee) = selectedWorkshop switch
{
"Handling Stress" => (3, 1000),
"Time Management" => (3, 800),
"Supervision Skills" => (3, 1500),
"Negotiation" => (5, 1300),
"How to Interview" => (1, 500),
_ => throw new ArgumentOutOfRangeException() // Invalid workshop selected
};
šŸ¤¤ switch expressions :catlove:
Nightswyng
NightswyngOPā€¢2w ago
so when I put that in, it was telling me there was an error with the underscore
Pobiega
Pobiegaā€¢2w ago
what error?
Nightswyng
NightswyngOPā€¢2w ago
CS 7.3 and using something higher than 8.0
Pobiega
Pobiegaā€¢2w ago
oh, you made the project using .NET framework 4.x not modern .NET 9
Nightswyng
NightswyngOPā€¢2w ago
yea, that was what I was told to use
Pobiega
Pobiegaā€¢2w ago
For shame.
Nightswyng
NightswyngOPā€¢2w ago
still learning the difference
Pobiega
Pobiegaā€¢2w ago
Your teachers are bad and should feel bad. .NET framework "died" in 2016 I can see schools not updating directly and still teaching old stuff for a few years, but 9 whole years?
Nightswyng
NightswyngOPā€¢2w ago
I think thats why i am struggling, alot of the resources are showing different versions
Pobiega
Pobiegaā€¢2w ago
Most resources online will assume modern .NET versions these days again, it has been 9 years.
Nightswyng
NightswyngOPā€¢2w ago
I will schedule a zoom meeting with my professor to clarify since we are going of the book, I don't want to be outdated in my learning
Pobiega
Pobiegaā€¢2w ago
I think the book is in fact using the old version thou, unless I am remembering wrong
Nightswyng
NightswyngOPā€¢2w ago
But pointing out there was no events in the designer helped immensely and it works!
Pobiega
Pobiegaā€¢2w ago
We talked about this on your last thread, I think
Nightswyng
NightswyngOPā€¢2w ago
You are correct, we did discuss that last time
Pobiega
Pobiegaā€¢2w ago
so just to recap, if I were to write this, it'd be something like...
private void calculateButton_Click(object sender, EventArgs e)
{
if (workshopListBox.SelectedIndex == -1)
{
// If none selected
MessageBox.Show("Select a Workshop, Please.");
return; // we can't continue if no workshop is selected
}

if (locationListBox.SelectedIndex == -1)
{
// If no location selected
MessageBox.Show("Select a Location, Please.");
return;
}

var selectedWorkshop = workshopListBox.SelectedItem.ToString();

var (numberOfDays, registrationFee) = selectedWorkshop switch
{
"Handling Stress" => (3, 1000),
"Time Management" => (3, 800),
"Supervision Skills" => (3, 1500),
"Negotiation" => (5, 1300),
"How to Interview" => (1, 500),
_ => throw new ArgumentOutOfRangeException() // Invalid workshop selected
};

totalDaysLabel.Text = numberOfDays.ToString();
feeOutputLabel.Text = registrationFee.ToString("c");

var selectedLocation = locationListBox.SelectedItem.ToString();

var lodgingFee = selectedLocation switch
{
"Austin" => 150,
"Chicago" => 225,
"Dallas" => 175,
"Orlando" => 300,
"Phoenix" => 175,
"Raleigh" => 150,
_ => throw new ArgumentOutOfRangeException() // Invalid location selected
};

// Calculate Lodging cost
lodgingFee = numberOfDays * lodgingFee;
totalLodgingLabel.Text = lodgingFee.ToString("c");

// Calculate Total cost
var totalCost = lodgingFee + registrationFee;
totalCostLabel.Text = totalCost.ToString("c");
}
private void calculateButton_Click(object sender, EventArgs e)
{
if (workshopListBox.SelectedIndex == -1)
{
// If none selected
MessageBox.Show("Select a Workshop, Please.");
return; // we can't continue if no workshop is selected
}

if (locationListBox.SelectedIndex == -1)
{
// If no location selected
MessageBox.Show("Select a Location, Please.");
return;
}

var selectedWorkshop = workshopListBox.SelectedItem.ToString();

var (numberOfDays, registrationFee) = selectedWorkshop switch
{
"Handling Stress" => (3, 1000),
"Time Management" => (3, 800),
"Supervision Skills" => (3, 1500),
"Negotiation" => (5, 1300),
"How to Interview" => (1, 500),
_ => throw new ArgumentOutOfRangeException() // Invalid workshop selected
};

totalDaysLabel.Text = numberOfDays.ToString();
feeOutputLabel.Text = registrationFee.ToString("c");

var selectedLocation = locationListBox.SelectedItem.ToString();

var lodgingFee = selectedLocation switch
{
"Austin" => 150,
"Chicago" => 225,
"Dallas" => 175,
"Orlando" => 300,
"Phoenix" => 175,
"Raleigh" => 150,
_ => throw new ArgumentOutOfRangeException() // Invalid location selected
};

// Calculate Lodging cost
lodgingFee = numberOfDays * lodgingFee;
totalLodgingLabel.Text = lodgingFee.ToString("c");

// Calculate Total cost
var totalCost = lodgingFee + registrationFee;
totalCostLabel.Text = totalCost.ToString("c");
}
Nightswyng
NightswyngOPā€¢2w ago
ahh ok, so you put the messages at the top pretty much to establish and get out of the way. I really like the cleaned up version and what is the throw new mean?
Pobiega
Pobiegaā€¢2w ago
its how we do errors in C# so the _ => throw new ArgumentOutOfRangeException() line means "if its ANY OTHER value than the ones specified above, create a new error object and throw it up the call stack"
Nightswyng
NightswyngOPā€¢2w ago
oh ok can you be my professor? šŸ˜† I will have to add that to my notes for the future
Pobiega
Pobiegaā€¢2w ago
Would if it paid decently šŸ˜›
Nightswyng
NightswyngOPā€¢2w ago
Its an online class so whats some extra income? lol I am gonna save your code to use as reference to clean up, but I was able to get the existing on working so I will submit that and set up with my professor one the version
Pobiega
Pobiegaā€¢2w ago
The trick to writing code like this is to break the problem down into smaller problems and handle them one at a time
Nightswyng
NightswyngOPā€¢2w ago
I was going over everything and couldnt figure it out until you pointed out there were no bindings then it clicked
Pobiega
Pobiegaā€¢2w ago
We can't do anything if the user didn't select values, so we check that first. Then we get the numbers for the workshop, then the location, and finally we do the math
Nightswyng
NightswyngOPā€¢2w ago
I have to remember to break it down, just get frazzled and lose sight
Pobiega
Pobiegaā€¢2w ago
You are far from alone in doing that. The code you pasted looks a lot like what most beginners code looks like
Nightswyng
NightswyngOPā€¢2w ago
its honestly what they show in the book
Pobiega
Pobiegaā€¢2w ago
Its more natural to think "if the user selected a valid value, calculate the registration fee..."
Nightswyng
NightswyngOPā€¢2w ago
No description
No description
Pobiega
Pobiegaā€¢2w ago
Rather than "if the user didn't select a valid value, stop them" Well the book is using ancient versions of C# so it doesn't have switch expressions :p
Nightswyng
NightswyngOPā€¢2w ago
lol can you do me a favor, and if you have a more updated modern suggestion on a book, I will gladly get it and read it to improve the outdated basics
Pobiega
Pobiegaā€¢2w ago
Sorry, I don't. The book I used to learn was outdated 20 years ago šŸ˜‰ Gonna sleep, nn
Nightswyng
NightswyngOPā€¢2w ago
lol thats ok and totally fair, jsut thought i would ask. Thank you again for your help! much appreciated! sleep well

Did you find this page helpful?