【问题标题】:"array of x is a 'variable' but is used like a method" [closed]“x 的数组是一个‘变量’,但它像一种方法一样使用”[关闭]
【发布时间】:2014-01-01 23:50:24
【问题描述】:

声明的两个实例都不起作用。我是 C# 新手,这很明显。我正在尝试 通过转换为c#来使用vb中的代码,我不明白这个问题,所以我无法修复它。

PictureBox[] pics = {picBackGround, picBackGroundTwo, picBarrier,picEnd,picFloor};

PictureBox[] pics = new PictureBox[] {picBackGround, picBackGroundTwo, picBarrier,picEnd,picFloor};

for (int i = 1; i < pics.Length; i++)
{            
    if (i > 3 && picUser.Bounds.IntersectsWith(pics(i).Bounds))
    {
        //Call CollisionDetectionRight()
    }   
}

【问题讨论】:

  • 停止编辑您的问题。问题是问题,而不是回答。

标签: c# arrays


【解决方案1】:

您必须访问带有方括号 [] 的数组作为索引。

示例。

for (int i = 1; i < pics.Length; i++)
{            
            if (i > 3 && picUser.Bounds.IntersectsWith(pics[i].Bounds))
            {
                //Call CollisionDetectionRight()
            }   
}

【讨论】:

    【解决方案2】:

    您错误地访问了数组的元素:

    pics(i).Bounds // tries to call the undefined function pics with the parameter i, and then get the Bounds member assigned to the returned element, which may not even be returned: it could be a void - will not work; throws
    

    由于数组已使用 [ ] 运算符声明,因此您必须使用它们来访问元素:

    pics[i].Bounds // gets the value of the member Bounds of the picture at the i location of pics - will work
    

    工作代码:

    for (int i = 1; i < pics.Length; i++){            
            if (i > 3 && picUser.Bounds.IntersectsWith(pics[i].Bounds)){
                //Call CollisionDetectionRight()
            }   
    } 
    

    注意:您可能应该将 i 初始化为 0,因为这是数组的基数,而不是 1

    【讨论】:

      【解决方案3】:

      您应该使用pics[i] 来访问该数组。方括号用于访问数组元素,其中括号 (()) 用于方法调用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-18
        • 2018-02-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多