3

I have this C# code in Visual Studio 2013. It uses a third-party library but I don't know if that's a factor in this problem. The code builds and executes perfectly. But there's a variable in it that I can't examine in the debugger. Here's the entire routine . . .

private void AddGeometry()
{
    var simplePositions = CreateRectShape(startPosition: new Point3D(0, 0, 0));
    var simplePositionCollection = new Point3DCollection(simplePositions);
    int size = simplePositionCollection.Count;
    MainViewport.BeginInit();
    MainViewport.Children.Clear();
    var polyLineVisual3D = new Ab3d.Visuals.PolyLineVisual3D()
    {
        Positions = simplePositionCollection,
        LineColor = Colors.DarkOrange,
        LineThickness = 2,
        Transform = new TranslateTransform3D(0, 0, 0)
    };

    MainViewport.Children.Add(polyLineVisual3D);
    MainViewport.EndInit();
}

If I breakpoint at the Children.Add() statement and try to examine polyLineVisual3D or place a quickwatch on it, I get...

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

... from Visual Studio. I have no trouble examining other local variables like simplePositions or simplePositionCollection at the same breakpoint.

user2864740
  • 60,010
  • 15
  • 145
  • 220
user316117
  • 7,971
  • 20
  • 83
  • 158
  • 4
    The variable has probably been optimized away by the compiler. Previous discussions [here](http://stackoverflow.com/questions/17386591/the-name-test1-does-not-exist-in-the-current-context) and [here](http://stackoverflow.com/questions/13517521/variable-does-not-exist-in-the-current-context-while-debugging). – Chris R. Timmons Dec 30 '16 at 20:04
  • 1
    The build configuration is Active (Debug). I thought optimization only applied to Release configurations. Is that wrong? – user316117 Dec 30 '16 at 20:08
  • If the settings are different from the default ones, then optimization might still be enabled. – Random Davis Dec 30 '16 at 20:26
  • 1
    in Build>General, Optimize code is UN-checked. – user316117 Dec 30 '16 at 20:34
  • 1
    A couple more things to try. (1) The debugger might not "see" `polyLineVisual3D` until after it's used. Move the breakpoint to `MainViewport.EndInit();` and see if the error goes away. Alternatively, put `Debug.WriteLine(polyLineVisual3D);` before the `Children.Add(..)` call. (2) Open the options dialog and turn on Debugging/General/Use Managed Compatibility Mode. There's a Microsoft Connect issue about this bug [here](https://connect.microsoft.com/VisualStudio/feedback/details/1038150/visual-studio-2015-debugger-doesnt-recognize-a-variable). – Chris R. Timmons Dec 30 '16 at 22:35
  • You might have to instrument, add a line `System.Diagnostics.Debug.WriteLine(polyLineVisual3D.###)` just any property, then you should be able to view the object in the debugger (doing this in hopes to keep the variable from being optimized away). – Chris O Dec 30 '16 at 22:47
  • @Chris R. Timmons I tried both (1) and (2) and no luck. But those were good ideas - thanks for suggesting them! I'm wondering if this has anything to do with the 3rd-party library. What if the DLL that gets invoked when I access Ab3d.Visuals.PolyLineVisual3D was built with optimizations turned on - could that cause this? – user316117 Dec 31 '16 at 01:11
  • I installed the trial for the AB4D library. There's code at the beginning of `TubePathSample.xaml.cs` that's very similar to yours. I ran the sample app and stepped through the code in that file, but couldn't reproduce the weird behavior you're running into - the debugger inspected `polyLineVisual3D ` without complaint.. For what it's worth, I'm running VS 2013 Update 5 on a Windows 8.1 box. – Chris R. Timmons Dec 31 '16 at 02:51

0 Answers0