[英]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, 也,
<
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.