none
extending the Object "Range" to implement new functionality RRS feed

  • Question

  • I'm new to .NET and C# and to this forum. My apologies if the following doesnt fit here:

    To have a more convenient way to address a range in an excel worksheet I tried to extend the Range-object and implement a method. What I would like to use is e.g.:

         Excel.Range myRange = ((MyRange) mySheet.Cells).get (2, 2, 1, 1);

    so I tried to define the following class (which doesnt compile):

    class MyRange : Excel.Range {

    public Excel.Range get (int top, int left, int rows, int cols)
    {
    return this.get_Range (this.get_Item(top,left), this.get_Item(top+rows, left+cols));
    }
    }

    The compiler complains that MyRange fails toimplement Activate() and 198 further methods. The problem seems to be that Microsoft.Office.Interop.Excel.Range is an interface and not a class. 

    Is there a class that I can inherit from  ? And/or better ways to achieve this ? 

    With kind regards,

    BLW553C

    complete code:

    using System;
    using Excel = Microsoft.Office.Interop.Excel;

    namespace Test2
    {

    class MyRange : Excel.Range {

    public Excel.Range get (int top, int left, int rows, int cols)
    {
    return this.get_Range (this.get_Item(top,left), this.get_Item(top+rows, left+cols));
    }
    }

    class MainClass
    {
    public static void Main (string[] args)
    {
    Excel.Application app = new Excel.Application ();
    Excel.Workbook workbook = app.Workbooks.Add ();
    Excel.Worksheet mySheet = (Excel.Worksheet)workbook.Sheets.Add ();
    app.Visible = true;

    //this works
    Excel.Range myRange = 
    mySheet.Cells.get_Range (mySheet.Cells.get_Range ("b2"), 
                            mySheet.Cells.get_Range ("c3"));
    myRange.Interior.ColorIndex = 4;

    // this is what I would like to use:
    myRange = ((MyRange)mySheet.Cells).get (2, 2, 1, 1);

    while (true)
    ;
                                                 
    }
    }
    }

    Friday, June 8, 2012 10:55 AM

Answers

  • using System;
    using Excel = Microsoft.Office.Interop.Excel;
    
    namespace Test2
    {
    
        static class MyRangeHelper
        {
            public static Excel.Range GetRange(Excel.Range cells, int top, int left, int rows, int cols)
            {
                return cells.get_Range((object)cells.get_Item(top, left), (object)cells.get_Item(top + rows, left + cols));
            }
        }
    
        class MainClass
        {
            public static void Main(string[] args)
            {
                Excel.Application app = new Excel.Application();
                Excel.Workbook workbook = app.Workbooks.Add();
                Excel.Worksheet mySheet = (Excel.Worksheet)workbook.Sheets.Add();
                app.Visible = true;
    
                //this works
                Excel.Range myRange =
                mySheet.Cells.get_Range(mySheet.Cells.get_Range("b2"),
                                        mySheet.Cells.get_Range("c3"));
                myRange.Interior.ColorIndex = 4;
    
                // this is what I would like to use:
                // myRange = ((MyRange)mySheet.Cells).get(2, 2, 1, 1);
                myRange = MyRangeHelper.GetRange(mySheet.Cells, 4, 4, 5, 6);
                myRange.Interior.ColorIndex = 6;
               
    
                while (true)
                    ;
    
            }
        }
    }

    • Marked as answer by BLW553C Friday, June 8, 2012 11:47 AM
    Friday, June 8, 2012 11:28 AM

All replies

  • using System;
    using Excel = Microsoft.Office.Interop.Excel;
    
    namespace Test2
    {
    
        static class MyRangeHelper
        {
            public static Excel.Range GetRange(Excel.Range cells, int top, int left, int rows, int cols)
            {
                return cells.get_Range((object)cells.get_Item(top, left), (object)cells.get_Item(top + rows, left + cols));
            }
        }
    
        class MainClass
        {
            public static void Main(string[] args)
            {
                Excel.Application app = new Excel.Application();
                Excel.Workbook workbook = app.Workbooks.Add();
                Excel.Worksheet mySheet = (Excel.Worksheet)workbook.Sheets.Add();
                app.Visible = true;
    
                //this works
                Excel.Range myRange =
                mySheet.Cells.get_Range(mySheet.Cells.get_Range("b2"),
                                        mySheet.Cells.get_Range("c3"));
                myRange.Interior.ColorIndex = 4;
    
                // this is what I would like to use:
                // myRange = ((MyRange)mySheet.Cells).get(2, 2, 1, 1);
                myRange = MyRangeHelper.GetRange(mySheet.Cells, 4, 4, 5, 6);
                myRange.Interior.ColorIndex = 6;
               
    
                while (true)
                    ;
    
            }
        }
    }

    • Marked as answer by BLW553C Friday, June 8, 2012 11:47 AM
    Friday, June 8, 2012 11:28 AM
  • Dear Adnan,

    thanks for your answer: this works and I will use it.

    regards,


    Rafel

    Friday, June 8, 2012 12:09 PM