none
C# Int does not exist in current context when doing a basic math equasion RRS feed

  • Question

  • 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.


    • Edited by Kiddo 9000 Thursday, December 6, 2018 2:48 PM
    Thursday, December 6, 2018 2:45 PM

Answers

  • 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 Answered"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
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by Ehsan Sajjad Thursday, December 6, 2018 3:10 PM
    • Proposed as answer by CoolDadTxModerator Thursday, December 6, 2018 3:58 PM
    • Marked as answer by Kiddo 9000 Thursday, December 6, 2018 4:06 PM
    Thursday, December 6, 2018 3:09 PM

All replies

  • 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 Answered"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
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites


    • Edited by Ehsan Sajjad Thursday, December 6, 2018 3:10 PM
    • Proposed as answer by CoolDadTxModerator Thursday, December 6, 2018 3:58 PM
    • Marked as answer by Kiddo 9000 Thursday, December 6, 2018 4:06 PM
    Thursday, December 6, 2018 3:09 PM
  • 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:00 PM
    Moderator
  • 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:07 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 Answered "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
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 6, 2018 4:25 PM