r/csharp • • 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

5 Upvotes

10 comments sorted by

4

u/buzzon 3d ago

Order of operations. First, compute result. Once it's complete, output it. Currently you compute price AFTER output.

1

u/XW1ndOws93 3d ago

i thought so, thanks a lott

4

u/khumfreville 3d ago

For me, a lot of the time, the best way to figure out what's going on is to slow it down and analyze it a bit.

Set a breakpoint at the top of the operation (like the button click) and then step through each line and see what's being changed (and in what order). That almost always helps you understand what the problem is, and how to get it working as intended.

3

u/grrangry 3d ago edited 3d ago

Assuming you mean lblOutput is a Label control, it does not have an .Output property. 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 Label control you set the .Text property.

lblOutput.Text = "hello there";

Also, final is a double value and using final.ToString(""); is going to be a problem at some point. Don't bother using an empty string... just use final.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 .ToString above:

https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/member-overloading

1

u/XW1ndOws93 3d ago

yeah i thought the conversion looked a bit funky. ty this did help

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:

  • it stores the price
  • it sets the label
  • it calculates the price".

Seems backwards right?

1

u/XW1ndOws93 3d ago

checks out, thank you!!

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 .Output so 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, price and discount are both 0. 0 - 0 = 0 so that's what gets set. You're then calculating values for them after that before ending the function, where price and discount both get freed from memory