none
A problem with defining an Array RRS feed

  • Question

  • Hi,

    I have the following code:

     private void simpleButton2_Click(object sender, EventArgs e)
            {
                string[] test = new string[] { };
                String[] files= this.get_setOfOrderedFiles_inPath("D:\\x");

                // A.
                // New list here.
                List<string> myList = new List<string>();
                myList.Add("one");
                myList.Add("two");
                myList.Add("three");
                myList.Add("four");
                myList.Add("five");

                // B.
                string[] s = myList.ToArray();

    }

    I am trying to define an array but whenever I used an array it cannot take any values and I got the message

    if I used the quick watch option of the debugger:

    The name '...' does not exist in the current context

    For example, the arrays: 'test', 'files', and 's' above.

    While the list is defined and worked well and I can see its items after they added to it, as the 'myList' list above.

    Could you please help me how to deal with the arrays and what is the problem I have?

    Thanks so much in advance,

    Best Regards.

    Aya.



    Aya Zoghby

    Tuesday, December 26, 2017 11:35 AM

Answers

  • Maybe you are trying to debug the Release version, or a configuration with “Optimize code” option enabled, which sometimes removes the unused variables, such as test and s. Try the default Debug configuration.

    • Proposed as answer by Fei HuModerator Wednesday, December 27, 2017 2:09 AM
    • Marked as answer by ZoghbyAya Wednesday, December 27, 2017 9:26 AM
    Tuesday, December 26, 2017 10:34 PM
  • Thank you so much. That's it!

    I found the "Optimize code" option selected. and I released it. The problem now is solved.

    Aya Zoghby

    • Marked as answer by ZoghbyAya Wednesday, December 27, 2017 9:26 AM
    Wednesday, December 27, 2017 9:24 AM

All replies

  • Your code looks perfectly fine from the point of view of defining the arrays. In fact, I have copied it into Visual Studio and it runs perfectly well, and I can see the values in the debugger as shown in the screenshot below.

    Screencap

    So the problem must be somewhere else. Maybe it is in the way that you are using to access the values. But the array declarations themselves are perfectly good.

    Tuesday, December 26, 2017 12:44 PM
    Moderator
  • Thank you for the reply.

    However, this is what I actually got. I need to know how to get your result instead.


    Aya Zoghby

    Tuesday, December 26, 2017 1:06 PM
  • I can see that you are in the "Watch" tab instead of the "Autos" tab that I was using. I suspect that you may have added the wrong variable to the Watch tab. Clear all watches, and then run your code until it stops at a breakpoint within the subroutine where your arrays are defined. Then, right-click on your array variable, and select "Add Watch". This will add the correct entry to the Watch window and you will be able to see the values.
    Tuesday, December 26, 2017 1:42 PM
    Moderator
  • Thank you.

    That exactly what I did. Now look at this very strange state:

    I repeated my code that is already worked before in the old version of the program: 'string[] files'. However, the old code line is worked well, while the new one, 'string[] test_files',  is cased the same problem again! 


    Aya Zoghby

    Tuesday, December 26, 2017 1:52 PM
  • That's unusual. I copied your code into a project in Visual Studio and added the Watch and it worked perfectly well. So the problem must be somewhere else, not in the source code itself. Maybe something has broken inside your Visual Studio. Do you have the same problem in other subroutines or other projects? What about the Locals and Autos windows? Do they fail the same as the Watches?
    Tuesday, December 26, 2017 9:17 PM
    Moderator
  • Maybe you are trying to debug the Release version, or a configuration with “Optimize code” option enabled, which sometimes removes the unused variables, such as test and s. Try the default Debug configuration.

    • Proposed as answer by Fei HuModerator Wednesday, December 27, 2017 2:09 AM
    • Marked as answer by ZoghbyAya Wednesday, December 27, 2017 9:26 AM
    Tuesday, December 26, 2017 10:34 PM
  • Thank you so much. That's it!

    I found the "Optimize code" option selected. and I released it. The problem now is solved.

    Aya Zoghby

    • Marked as answer by ZoghbyAya Wednesday, December 27, 2017 9:26 AM
    Wednesday, December 27, 2017 9:24 AM