C
C#3mo 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
Angius3mo ago
Well, points can be uninitialized What will be the value of points if exerciseComboBox.SelectedText is "Shrek"?
Mix
Mix3mo 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
Angius3mo ago
I don't see any other revisions in the link
Mix
Mix3mo ago
oh it made a new one ok click the new link
Angius
Angius3mo 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
Mix3mo 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
Angius3mo 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
Mix3mo 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
Mix3mo ago
No description
Mix
Mix3mo ago
is it because i put the const there?
Angius
Angius3mo ago
Well, if points is a constant, then... it cannot change So, sure, you can give it an initial value
Mix
Mix3mo ago
oh true, duh
Angius
Angius3mo ago
But none of your points = will do anything
Mix
Mix3mo ago
i understand that, but then without the points = , how would i do the equation and have it output correctly?
Mix
Mix3mo ago
this is an example of what the program is supposed to look like for reference
No description
Angius
Angius3mo ago
Just don't make points a constant
Mix
Mix3mo 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
Angius3mo 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
Mix3mo ago
well you said it wouldn't do anything earlier
Angius
Angius3mo ago
Yes, if points is constant
Mix
Mix3mo ago
ohhhh ok
Mix
Mix3mo ago
then why would i still be getting this if I have int points = 0;?
No description
Angius
Angius3mo ago
Is your code still this? https://paste.mod.gg/trcpyfqktemo/0
Mix
Mix3mo ago
this appears in all of the points = line but doesn't stop the program from running yes
Mix
Mix3mo ago
No description
Angius
Angius3mo 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
Mix3mo ago
ok cool cool
Angius
Angius3mo ago
So... go back to how it was, but now with a default points value
Mix
Mix3mo 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
Angius3mo ago
No idea
Mix
Mix3mo ago
all good, thanks for your help!