变量已声明但在调试C#期间未显示

时间:2010-12-29 02:56:50

标签: c# visual-studio-express visual-studio-debugging

debug:http://img1.uploadscreenshot.com/images/orig/12/36121481470-orig.jpg

的屏幕截图

注意x,y是如何得到值的(我不知道为什么x和y在for循环中停在69上 - x应该上升到86而y到183)但r根本没有值。 (变量不存在?什么?)我应该如何解决这个问题?

代码如果你想阅读:

        public float[] cht(int[,] matrix)
    {
        float[] xyrd = new float[4];
        int xthreshold, ythreshold;
        float slope;
        double dir;
        float zone;
        int[] limitsStorage = new int[3] { matrix.GetLength(0), matrix.GetLength(1), matrix.GetLength(0) / 2 - 10 };
        short[,,] accumulator = new short[limitsStorage[0]+1, limitsStorage[1]+1,limitsStorage[2]+1];
        for (int x = 0; x < limitsStorage[0]; x++)
        { //first dimension loop of matrix 100
            for (int y = 0; y < limitsStorage[1]; y++)
            { //second dimension loop of matrix 120
                if (matrix[x, y] == 225)
                {//the data at the for loop location is a 1 and not 0 hit.
                    xthreshold = x - limitsStorage[0] / 2;
                    ythreshold = y - limitsStorage[1] / 2;
                    //forget angle, search slope: float angle = xthreshold > 0 ? ((float)Math.Atan2(xthreshold, ythreshold)) : ((float)Math.Atan2(xthreshold, ythreshold) + 180);
                    slope = xthreshold / ythreshold;
                    //initiate if loops.
                    dir = 180 + Math.Round(Math.Atan2(ythreshold, xthreshold) * 57.2957 / 45, 0) * 45 + 45 * (Math.Round(((Math.Atan2(ythreshold, xthreshold) * 57.2957) % 45) / 45));
                    if (slope > .404 || slope < -.404)
                    {
                        if (slope < 2.3558 || slope > -2.3558)
                        {
                            if (xthreshold > 0)
                            {
                                if (ythreshold > 0)
                                {
                                    //+x+y zone
                                    zone = 45 + 180;
                                }
                                else
                                {
                                    //+x-y zone
                                    zone = 180 - 45;
                                }
                            }
                            else
                            {
                                if (ythreshold > 0)
                                {
                                    //-x+y zone
                                    zone = 360 - 45;
                                }
                                else
                                {
                                    //-x-y zone
                                    zone = 45;
                                }
                            }
                        }
                        else if (ythreshold > 0)
                        {
                            //+y zone
                            zone = 360 - 90;
                        }
                        else
                        {
                            //-y zone
                            zone = 90;
                        }
                    }
                    else if (xthreshold > 0)
                    {
                        //+x zone
                        zone = 180;
                    }
                    else
                    {
                        //-x zone
                        zone = 0;
                    }
                    for (int R = 6; R < limitsStorage[2]; R++)
                    { //Radius loop for scan 44
                        float delta = (float)((1 / R) * 57.2957);
                        for (float Theta = zone - 25; Theta < zone + 25; Theta += delta)
                        {
                            accumulator[(int)(((R * Math.Cos(Theta / 57.2957)) < 0 || (R * Math.Cos(Theta / 57.2957)) > limitsStorage[0]) ? 0 : R * Math.Cos(Theta / 57.2957)), (int)(((R * Math.Sin(Theta / 57.2957)) < 0 || (R * Math.Sin(Theta / 57.2957)) > limitsStorage[1]) ? 0 : R * Math.Sin(Theta / 57.2957)),R]++;
                            //btw, 0,0,R is always the non-center area.
                        }
                    }

                }
            }
        }

        for (int x = 1; x < limitsStorage[0]; x++)
        {
            for (int y = 1; y < limitsStorage[1]; y++)
            {
                for (int r = 6; r < limitsStorage[2]; r++)
                {
                    if (xyrd[3] > accumulator[x, y, r])
                    {
                        xyrd[0] = x;
                        xyrd[1] = y;
                        xyrd[2] = r;
                        xyrd[3] = accumulator[x, y, r];
                    }
                }
            }
        }

        if (accPrint)
        {
            //do something for debugging?
            accPrint = false;
        }
        return xyrd;
    }

3 个答案:

答案 0 :(得分:2)

我刚注意到xy下面有一个小锁符号,表示您在类中有xy的私有变量方法正在执行。这些是您在调试器中看到的xy

当你退出声明它的循环时,

r被适当地超出了范围。

顺便说一下,xy是非常糟糕的成员变量名称,对forint的{​​{1}}循环变量来说,这些名称是可怕的坏名称,特别是如果它们是包含在名为xy的成员变量的类中。

答案 1 :(得分:1)

您声明r的唯一地方是for声明,对吧?这意味着一旦循环结束,r就会超出范围。因此,如果您在函数末尾检查变量,那么r就不会存在。

承认我不知道为什么xy根据评论在范围内。它们可能是类变量,但提问者却没有。不过,这是我能想到的唯一解释。

答案 2 :(得分:0)

这种行为并不奇怪 - 你实际上得到了你所期望的。

请注意,观察窗口只能准确显示断点处 范围 的值。

在突出显示的断点处,只有累加器[x,y,r]在范围内,您可以看到预期的值。