简体   繁体   English

计算未更新

[英]Calculation Not Updating

I have been running this syntax with one variable successfully, but now I am trying to change it to a foeach() loop and take a range of values and show the results as a message box. 我已经成功地使用一个变量运行了该语法,但是现在我试图将其更改为foeach()循环并采用一系列值并将结果显示为消息框。 My issue with the syntax is that ecll always retains the value of the first number passed, and the calculation is never updated for each subsequent number in the array. 我的语法问题是ecll始终保留传递的第一个数字的值,并且永远不会为数组中的每个后续数字更新计算。

Where did I err that is preventing this from being updated for each subsequent number in the array? 我在哪里犯了错,无法针对数组中的每个后续数字进行更新?

private void btnGetData_Click(object sender, EventArgs e)
{
  int start = 2;
  int end = 10;

  int[] nums = Enumerable.Range(start, end - start).ToArray();

  foreach (int n in nums)
  {  
      float tp_x = 0, tp_y = 0;

      SAP = new List<PointF>();
      float fbw = m_pd.bl[m_pd.bl.Count - 1].m_Width;
      float Location1_X = tp_x + fbw;
      float Location1_Y = tp_y;
      SAP.Add(new PointF(Location1_X, Location1_Y));

      float iBH = gbh(m_pd.bl.Count - 1);
      float lbw = m_pd.bl[0].m_Width;
      float Location2_X = tp_x + lbw;
      float Location2_Y = tp_y + (iBH) + 1.5f;

      PointF rip = new PointF();
      if (!Getrip(ftp, rhep, ref rip))
      {
        SAP = null;
        return;
      }

      for (int iRowIndex = saii; iRowIndex < m_pd.pp.Count; iRowIndex++)
      {
        float Xvalue = m_pd.pp[iRowIndex].X;
        float Yvalue = m_pd.pp[iRowIndex].Y;
        SAP.Add(new PointF(Xvalue, Yvalue));

        if (Yvalue == LeftIntersectionPoint.Y)
        {
          pp.X = Xvalue;
          pp.Y = Yvalue;
          continue;
        }

        if (Xvalue >= rip.X)
        {
          Xvalue = rip.X;
          SAP[SAP.Count - 1] = new PointF(rip.X, rip.Y);
        }

        if (Xvalue == rip.X)
        {
          break;
        }

        pp.X = Xvalue;
        pp.Y = Yvalue;
      }

      double ecll = Getll(Location1_X, Location1_Y, rip.X, rip.Y);

      Messagebox.Show(Convert.ToString(ec11));
      txtLength.Text = ll.ToString("0.00");
    }
}

I feel like this is more of a comment based on what's going on here, but I kind of need the code section to explain this better I believe. 我觉得这更多是根据此处发生的事情进行的评论,但是我认为需要代码部分来更好地解释这一点。

Let's simplify away from your points, widths, etc. I think we can all agree that n is never used within your function, so let's do a similar example: 让我们简化一下您的点,宽度等。我想我们都可以同意在函数中从未使用过n,所以让我们做一个类似的例子:

So I have a function I wrote that adds 1 to 1 所以我写了一个将1加1的函数

var newNum = 1 + 1;

It does what is expected, sets newNum to 2, but let's say I wanted to enhance it so that it adds 1 to the numbers in nums (from your original function): 它完成了预期的工作,将newNum设置为2,但是我想对其进行增强,以便它使num的数字加1(来自原始函数):

int start = 2;
int end = 10;

int[] nums = Enumerable.Range(start, end - start).ToArray();

but if I try to reuse my function outright: 但是,如果我尝试完全重用我的函数:

foreach (int n in nums)
{
  var newNum = 1 + 1;
} 

Every single pass, I'm always going to have newNum set at 2 because I'm not using the variable. 每次传递,我总是将newNum设置为2,因为我没有使用变量。

what I should do is write this: 我应该做的是这样写:

foreach (int n in nums)
{
  var newNum = 1 + n;
} 

so based on your 2 through 10, I should see newNum set to 3 through 11 at various iterations. 因此,根据您的2到10,我应该在不同的迭代中看到newNum设置为3到11。

Each iteration in the 'Foreach' loop assigns a new value to 'n'. “ Foreach”循环中的每次迭代都会为“ n”分配一个新值。 However in the loop body, that value (n) is not used anywhere in the calculations (unless I am missing something). 但是,在循环主体中,该值(n)不会在计算中的任何地方使用(除非我遗漏了某些内容)。 So, of course the result of these calculations will always be the same. 因此,当然,这些计算的结果将始终相同。

As soon as you include 'n' in some of the calclations, the result will change... 一旦在某些计算中包含“ n”,结果将发生变化...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM