C
C#9mo ago
Mix

Getting an error for an undefined variable

Hello, I'm a CS major working on a project so I'm still pretty new. I'm getting a CS0165 error on line 85 of this program I'm working on. Wouldn't the variable already defined after I've done all of the previous equations above it? The weird part is that it's not showing as an error for the rest of my code, just this one specific line and I'm not sure why. I've attached a picture as well as a BlazeBin link. https://paste.mod.gg/trcpyfqktemo/0
BlazeBin - trcpyfqktemo
A tool for sharing your source code with the world!
No description
31 Replies
Angius
Angius9mo ago
Well, points can be uninitialized What will be the value of points if exerciseComboBox.SelectedText is "Shrek"?
Mix
MixOP9mo ago
posted this before i added the else statements, refresh the link but it looks like it works fine if I include it in one of the above if statements with no errors i just don't think that it makes sense to just copy/paste those lines across 5 different statements if i don't need to
Angius
Angius9mo ago
I don't see any other revisions in the link
Mix
MixOP9mo ago
oh it made a new one ok click the new link
Angius
Angius9mo ago
You can just add an else to your chain of else-ifs Or turn one of the else-ifs into just an else Or you can extract the repeated code into a method
Mix
MixOP9mo ago
so if I add another else in there with the chunk of code that I mentioned, it'll apply to the rest of the previous else/else if statements?
Angius
Angius9mo ago
The else-if chain will be exhaustive In case the input is "Shrek", the code inside of the else will execute And, for example, set points to 0 Or... you can give points a default value 0 seems like a safe default
Mix
MixOP9mo ago
so I initally had it set to 0, but then I was getting a bunch of weird errors after I tried to use it in a equation
Mix
MixOP9mo ago
No description
Mix
MixOP9mo ago
is it because i put the const there?
Angius
Angius9mo ago
Well, if points is a constant, then... it cannot change So, sure, you can give it an initial value
Mix
MixOP9mo ago
oh true, duh
Angius
Angius9mo ago
But none of your points = will do anything
Mix
MixOP9mo ago
i understand that, but then without the points = , how would i do the equation and have it output correctly?
Mix
MixOP9mo ago
this is an example of what the program is supposed to look like for reference
No description
Angius
Angius9mo ago
Just don't make points a constant
Mix
MixOP9mo ago
yes, but for example if I were to do weightPoints * exerciseDuration; in one of my if statements without the points = in front, how would the program know that I want the product to apply to the points variable? i'm just trying to understand why it wouldn't be necessary anymore, i know the code works either way
Angius
Angius9mo ago
Why would you want to do it without points = in front? Of course it would be necessary All I said, is that it won't work if points is a constant
Mix
MixOP9mo ago
well you said it wouldn't do anything earlier
Angius
Angius9mo ago
Yes, if points is constant
Mix
MixOP9mo ago
ohhhh ok
Mix
MixOP9mo ago
then why would i still be getting this if I have int points = 0;?
No description
Angius
Angius9mo ago
Is your code still this? https://paste.mod.gg/trcpyfqktemo/0
Mix
MixOP9mo ago
this appears in all of the points = line but doesn't stop the program from running yes
Mix
MixOP9mo ago
No description
Angius
Angius9mo ago
Then that would be because you no longer use an unassigned value points are set in line 60, then used on 64 and 70 Everything is fine
Mix
MixOP9mo ago
ok cool cool
Angius
Angius9mo ago
So... go back to how it was, but now with a default points value
Mix
MixOP9mo ago
yep, it works fine also another thing, which i dont think you'd really be able to help me with without me putting this on github, but for some reason, when i run the program, the button isnt clickable? Do you know why this would be? I've checked the Event Handler to make sure it's linked and it is
Angius
Angius9mo ago
No idea
Mix
MixOP9mo ago
all good, thanks for your help!
Want results from more Discord servers?
Add your server