Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#使用多维数组作为属性_C# - Fatal编程技术网

C#使用多维数组作为属性

C#使用多维数组作为属性,c#,C#,Get和set是红线。 错误标志: 只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句 如何解决这个问题?这里有一个常规方法:公共int-TeeData(int-IndexOne,int-IndexTwo,int-IndexThree)。get和set符号用于属性,而不是方法 我认为您需要的是一个索引属性-只需将paren改为方括号,您将使用this而不是名称: using System; namespace CardV5 { class Tee { p

Get和set是红线。 错误标志:

只有赋值、调用、递增、递减、等待和新对象表达式可以用作语句


如何解决这个问题?

这里有一个常规方法:
公共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;}
    }