Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, December 6, 2018 2:45 PM
Hello everyone.
I am quite new here so bare with me. I have made this simple script where the user selects a few items in some drop down boxes. The code checks to see what items were selected and returns a number to a specific int depending on what the result was. At the end, it will take these values and subtract them from 100 and display it on screen.
The problem: whenever i use the ints with the math equasion, it returns that my intergers "dont exist in the current context". Below is my code:
// Collect data from the IQ combo box
if (eq.SelectedText == "1 - 9")
{
int q = 110;
}
else if (eq.SelectedText == "10 - 19")
{
int q = 85;
}
else if (eq.SelectedText == "20 - 49")
{
int q = 50;
}
else if (eq.SelectedText == "50 - 99")
{
int q = 25;
}
else if (eq.SelectedText == "100 - 119")
{
int q = 5;
}
else if (eq.SelectedText == "120+")
{
int q = 10;
}
// Get the final result
int a = 100;
int r = a - q;
// Display the result
new Result().Show();
Error: The name 'q' does not exist in the current context (CS0103)
Can you help me out here? Thanks.
All replies (4)
Thursday, December 6, 2018 3:09 PM âś…Answered | 1 vote
The problem here is that you are declaring local scoped variables. When you are initialzing a variable in the if block it can only be accessible in that portion. When we need variables accessed in broader scope i.e. in your case in whole clae we create fields. So declare them as fields.
The following code variable
[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]
Blog | LinkedIn | Stack Overflow | Facebook
Thursday, December 6, 2018 4:00 PM | 1 vote
I'm in agreement with what Ehsan is recommending. The only thing that I'd clarify is that you should (for best code maintenance) promote up identifiers up the scope, not down. So start with the variable in the scope (curly braces) where you need it. If you find you need it outside the scope then promote up once. In your case that would mean moving from the curly brace scope to the containing method scope. If you find you need it in other methods then promote to a field. If you find you need it outside the type then promote to a property (or find an alternative approach).
Following this approach you will only expose variables for the minimal length of time you need them. This helps with code maintenance and helps avoid potential conflicts later.
Michael Taylor http://www.michaeltaylorp3.net
Thursday, December 6, 2018 4:07 PM
Thank you very much! I was actually aware of what i needed to fix now and i was completely unaware of it. Kudos to you.
There is no "Best" language to use. Just your personal favorite. So stop debating over which language is best because there is none.
Thursday, December 6, 2018 4:25 PM
Yes, totally agreed with CoolDadTx. Very useful points.
[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]
Blog | LinkedIn | Stack Overflow | Facebook