简体   繁体   English

错误(CS0120):该方法需要对象引用

[英]Error (CS0120): An object reference is required for the method

With the code below I get the following error, the method used is documented here . 使用下面的代码,我得到以下错误, 此处记录所使用的方法。 Why is the method not accepting my list of strings to create new layers? 为什么该方法不接受我的字符串列表来创建新图层?

Error (CS0120): An object reference is required for the non-static field, method, or property 'Rhino.DocObjects.Tables.LayerTable.Add(string, System.Drawing.Color)' (line 73)

Code: 码:

  private void RunScript(List<string> x, ref object A)
  {

    for (int i = 0; i <= x.Count;i++)
    {
      Rhino.DocObjects.Tables.LayerTable.Add(x[i], Color.Black);
    }
    A = x;
  }

Line 73 is this one: 73行就是这个:

Rhino.DocObjects.Tables.LayerTable.Add(x[i], Color.Black);

I think you need to change it to 我认为您需要将其更改为

Rhino.DocObjects.Tables.LayerTable.Add(x[i], ref Color.Black);

Note the ref before last parameter. 注意最后一个参数之前的ref

EDIT 编辑

To resolve this issue you may do something like this ; 要解决此问题,您可以执行以下操作;

 object ob = Color.Black; //box value
 Rhino.DocObjects.Tables.LayerTable.Add(x[i], ref ob );

Use ref as @Tigran suggested. 按照@Tigran的建议使用ref。

Also, 也,

  • make sure x is not null 确保x不为空
  • make sure that all strings in x are valid (not null) 确保x中的所有字符串均有效(不为null)
  • Use < rather than <= in your loop. 在循环中使用<而不是<= As it stands it will try to access one item beyond the end of the list and will thrown an index out of range exception. 按目前的情况,它将尝试访问列表末尾以外的一项,并将抛出索引超出范围的异常。

The method signature in the documentation is: 文档中的方法签名为:

public int Add(
    string layerName,
    Color layerColor
);

This is not a static method. 这不是静态方法。 You need to call it on an actual LayerTable object, such as in the example in the documentation: 您需要在实际的LayerTable对象上调用它,例如文档中的示例:

partial class Examples
{
    public static Rhino.Commands.Result AddLayer(Rhino.RhinoDoc doc)
    {
        // <snip>
        layer_index = doc.Layers.Add(layer_name, System.Drawing.Color.Black);
        // <snip>
    }
}

In the above example, doc.Layers returns a LayerTable object that you call Add on. 在上面的示例中, doc.Layers返回一个您称为Add on的LayerTable对象。

Just add static after private. 只需在private之后添加static。

private static void RunScript(List<string> x, ref object A)
  {

    for (int i = 0; i <= x.Count;i++)
    {
      Rhino.DocObjects.Tables.LayerTable.Add(x[i], Color.Black);
    }
    A = x;
  }

暂无
暂无

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

相关问题 CS0120:需要对象引用 - CS0120: An object reference is required 错误 CS0120:非静态字段、方法或属性“SetFaces.btnA”需要 object 引用 - error CS0120: An object reference is required for the non-static field, method, or property 'SetFaces.btnA' 错误 CS0120:非静态字段、方法或属性“CameraController.main”需要对象引用 - Error CS0120: An object reference is required for the non-static field, method, or property 'CameraController.main' 错误 CS0120:非静态字段、方法或属性“InventoryUI.newDiamondText”需要 object 引用 - Error CS0120: An object reference is required for the non-static field, method, or property 'InventoryUI.newDiamondText' 如何使用Xamarin中的Android Dropbox解决“错误CS0120:非静态字段,方法或属性需要对象引用”的问题? - How to resolve “Error CS0120: An object reference is required for the non-static field, method, or property” using Dropbox for Android in Xamarin? 需要对象引用C#CS0120 - An object reference is required C# CS0120 CS0120 非静态字段需要 object 参考 - CS0120 An object reference is required for the non-static field CS0120非静态字段,方法或属性需要对象引用 - CS0120 An object reference is required for the non-static field, method, or property CS0120 非静态字段、方法或属性需要对象引用 - CS0120 Object reference is required for the non-static feild, method, or property CS0120 非静态字段、方法或属性“Form1.rtBox”需要 object 引用 - CS0120 An object reference is required for the non-static field, method, or property 'Form1.rtBox'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM