The name 'i' does not exist in the current context.Please help fix the error

Давид Венгура 1 Reputation point
2022-01-14T18:02:20.22+00:00

165150-image.png

165217-image.png

Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Steven Crossley 1 Reputation point
    2022-01-20T16:23:22.16+00:00

    While it would be beneficial to see the cells.cs file, as some days have passed, getting access to this may be a problem.

    Instead then, I would draw your attention to your second screenshot, showing a number of runtime errors stating that the name 'i' does not exist in the current context. You can see for each of these that two numbers are provided in backets. They look like this: (a, b). These are normally very helpful clues in debugging, as they tell you exactly where the error occurred to the row (a) and column (b).

    First, we know that cells.cs is the file containing the errors. Each run-time error states this, so this is out starting point. Working initially with the first error which gives the co-ordinates (19, 17), open cells.cs and then go down to row 19. Your IDE will typically have numbered rows, so this should be easy to see. Now, using your keyboard's arrow keys, go right until you reach column 19. Don't worry about counting these, your IDE does it for you. To see the column your cursor is currently on, take a look a the bottom right corner of VSCode where you can see this: Ln 30 Col 1'. This indicates that you are currently on line 30, column 1. You want to be on line 19, column 17.

    Once you arrive, you should see the reference to the variable name 'i' that is causing this particular instance of the error. There are many reasons for the error 'name x does not exist in the current context', for example the variable might be declared below its usage, or it might be declared inside a lower-level code block that the call at 19,17 doesn't have access to (this is referred to as being out of scope). This would be the case if, for example, the variable with the name 'i' on line 22 of controller.cs was the only variable with the name 'i' in your project.

    The error occurs several times - at (19, 17), (21, 12) and (23, 12), and you may need to investigate each of these individually - or, resolving one occurrence may make the cause of the others more obviously recognisable.

    We will be able to help you further pin-point the exact cause of the exception if we can see cells.cs. If that is not an option, then I hope this at least gives you a reasonable starting point for continued investigation.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.