C#使用多维数组作为属性
Get和set是红线。 错误标志: 只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句C#使用多维数组作为属性,c#,C#,Get和set是红线。 错误标志: 只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句 如何解决这个问题?这里有一个常规方法:公共int-TeeData(int-IndexOne,int-IndexTwo,int-IndexThree)。get和set符号用于属性,而不是方法 我认为您需要的是一个索引属性-只需将paren改为方括号,您将使用this而不是名称: using System; namespace CardV5 { class Tee { p
如何解决这个问题?这里有一个常规方法:
公共int-TeeData(int-IndexOne,int-IndexTwo,int-IndexThree)
。get
和set
符号用于属性,而不是方法
我认为您需要的是一个索引属性-只需将paren改为方括号,您将使用this
而不是名称:
using System;
namespace CardV5
{
class Tee
{
private static int numOne = 4;
private static int numTwo = 2;
private static int numThree = 22;
public int Value { get; set; }
private int[, ,] m_tData = new int[numOne, numTwo, numThree];
public int TeeData(int IndexOne, int IndexTwo, int IndexThree)
{
get{return m_tData[IndexOne, IndexTwo, IndexThree];}
set{m_tData[IndexOne, IndexTwo, IndexThree] = Value;}
}
}
}
这将允许您执行以下操作:
public int this[int IndexOne, int IndexTwo, int IndexThree]
{
get{return m_tData[IndexOne, IndexTwo, IndexThree];}
set{m_tData[IndexOne, IndexTwo, IndexThree] = Value;}
}
获取并设置应用于属性。在您的例子中,您已经定义了一个方法。Get\Set在此上下文中无效您可以创建单独的Get/Set属性:
Tee tee = new Tee();
tee[0,0,0] = /*something*/;
或使其成为索引器:
public int GetTeeData(int IndexOne, int IndexTwo, int IndexThree)
{
return m_tData[IndexOne, IndexTwo, IndexThree];
}
public void GetTeeData(int IndexOne, int IndexTwo, int IndexThree, int value)
{
m_tData[IndexOne, IndexTwo, IndexThree] = value;
}
public int this[int IndexOne, int IndexTwo, int IndexThree]
{
get{return m_tData[IndexOne, IndexTwo, IndexThree];}
set{m_tData[IndexOne, IndexTwo, IndexThree] = Value;}
}