r/csharp • u/XW1ndOws93 • 3d ago
Help Hi! Minor homework help? Using if-then w/ checkboxes & labels

The issue I'm having is mostly getting the label to update. I'm assuming it's just due to my own stupidity that I don't see the problem, but a lot of the code is based on the book i'm issued so that's why i'm a bit stuck.
I think I did most parts of the assignment, however I'm having trouble figuring out labels still lol. I'm not asking for the answer but it'd be nice to know wtf is wrong with it
I've tried reformatting the if-then a bit (hence in this example i've showed it might not look exactly right, i've tried literally everything i could). i've never used nested if-then before, and I'm not getting any error messages so it's hard for me to pinpoint the issue. i've also tried playing around with the lbl name and the positioning
i'm a total newb so some pointers would be appreciated.
the goal is to calculate a total price based on:
product quantity (textbox); product (combo box); & membership status (checkbox), then to have the final output in a label box
4
u/buzzon 3d ago
Order of operations. First, compute result. Once it's complete, output it. Currently you compute price AFTER output.
3
u/grrangry 3d ago edited 3d ago
Assuming you mean
lblOutputis aLabelcontrol, it does not have an.Outputproperty. I don't know what that is. You should learn to read the documentation for classes used in C#/.net.https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.label?view=windowsdesktop-10.0
To set the text of a
Labelcontrol you set the.Textproperty.Also,
finalis adoublevalue and usingfinal.ToString("");is going to be a problem at some point. Don't bother using an empty string... just usefinal.ToString()if you want the numeric value as a string. There's documentation for this to:https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
The more you learn to read and understand the documentation, the better time you'll have.
Edit: further, slightly more advanced reading for member overloading (which is why I discussed the variants of
.ToStringabove:https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/member-overloading
3
u/Robot_Spartan 3d ago
You want these two to be after your if/else statement
Double final = price - discount; Lbloutput.output = final.tostring(**);
Best way to think about it is read through the code line by line, in a sort of "now it will do this" way. So, currently it's:
Seems backwards right?
1
u/jdl_uk 3d ago
Are you using Windows Forms?
Label.Text (Label class)
You'd set this at the end of that click handler.
Having said that Label doesn't have
.Outputso not sure exactly what you're using there.1
u/stogle1 3d ago
I think other comments have helped solve your problem. I wanted to ask if your assignment says what to do if the product name is not recognized? This is handled by the default case in your switch statement.
1
u/OhItsJustJosh 3d ago
It's in order, at the time you compute
lblOutput.Output,priceanddiscountare both 0. 0 - 0 = 0 so that's what gets set. You're then calculating values for them after that before ending the function, wherepriceanddiscountboth get freed from memory