当前位置: 代码迷 >> C# >> 怎样把这段VB代码转换为C#该如何处理
  详细解决方案

怎样把这段VB代码转换为C#该如何处理

热度:75   发布时间:2016-05-05 03:52:57.0
怎样把这段VB代码转换为C#

 Public Property Cell(ByVal row_index As Integer, ByVal col_index As Integer) As Object
        Get
            Return _excel.Cells(row_index, col_index).value
        End Get

        Set(ByVal value As Object)
            _excel.Cells(row_index, col_index).value = value
        End Set
    End Property


谢谢。
------解决思路----------------------
无法直接翻译,c#不支持这类带参数的属性。你可以把它分别变为两个 Get_XXX 和 Set_XXX 普通方法来在C#中定义。
------解决思路----------------------
本帖最后由 caozhy 于 2015-05-02 00:02:40 编辑
public object this[int x, int y]
{
    get ...
    set ...
}
------解决思路----------------------
在 vb.net 中的 Item 对应 c# 的 this。而这个 Cell 属性不是。
------解决思路----------------------
可以这样写
        private static string[,] Cell = new string[10, 10];
        public string this[int x, int y] //设置该类的索引器 
        {
            get { return Cell[x, y]; }
            set { Cell[x, y] = value; }
        }
        static void Main(string[] args)
        {
            Cell[2, 3] = "abcd";
            Cell[4, 1] = "123";
            Console.WriteLine("{0} {1}", Cell[4, 1], Cell[2, 3]);
        }

不过写成两个方法要稳妥些
  相关解决方案