怎样把这段VB代码转换为C#

.Net技术 码拜 9年前 (2015-05-10) 1066次浏览 0个评论
 
 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

谢谢。

5分
无法直接翻译,c#不支持这类带参数的属性。你可以把它分别变为两个 Get_XXX 和 Set_XXX 普通方法来在C#中定义。
5分
本帖最后由 caozhy 于 2015-05-02 00:02:40 编辑

public object this[int x, int y]
{
    get …
    set …
}

5分
在 vb.net 中的 Item 对应 c# 的 this。而这个 Cell 属性不是。
应该是读取或设置EXCEL单元格内容的函数。
5分
可以这样写

        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]);
        }

不过写成两个方法要稳妥些

在vb.net中可以写 

    Public Property Cell(ByVal row_index As Integer, ByVal col_index As Integer) As Object    ’读写二维数组
        Get
            Return ......
        End Get

        Set(ByVal value As Object)
            .......
        End Set
    End Property

    Public Property Cell(ByVal name As String) As Object  ‘读写命名单元格
        Get
            Return ......
        End Get

        Set(ByVal value As Object)
            .......
        End Set
    End Property

还可以写几十个其它的带参数属性。vb.net的这种语法特性,是c#所不具备的。在vb.net中,只有Items属性才对应c#的this属性(而且它也比c#的功能更丰富),而这些都不是。无法用this来“代替”的,否则立刻就会发现代替不下去了。

分开写两个函数呗

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明怎样把这段VB代码转换为C#
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!