none
How to convert this VB code into c# RRS feed

  • Question

  •  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
    Thanks.
    Friday, May 1, 2015 3:46 PM

Answers

  • If you want to have a property called Cell that returns excel's Cells collection (which is a Range) then just do this:

    public Range Cells
    {
        public get
        {
            return _excel.Cells;
        }
    }
    
    // Range is accessed by .Cells
    // Range has an index operator [,]
    // so you can access .Cells[row,col]
    

    If you want to intercept the indexing, then you must return a object that proxies the indexing.

    class MyRange
    {
        public MyRange( Range range ) { this.range = range; }
        Range range;
    
        // This just might be the syntax you're actually looking for
        public object this[int row, int col]
        {
            get {
                return range[row,col];
            }
            set
            {
                range[row,col] = value;
            }
        }
    }
    

    public MyRange Cells
    {
       get {
          return new MyRange( _excel.Cells );
       }
    }
    

    • Marked as answer by sincos2007 Sunday, May 3, 2015 2:53 PM
    Friday, May 1, 2015 5:36 PM