Skip to content

Is this possible?

Dan

Asked Nov 28, 2007, 1:51 PM EST2 answers
Share
LinkedInX
I have an spreadsheet type application I am creating, but I ran into some problems with polymorphism. When all of the expressions need to be calculated, I compile it in CodeDom so I can evaluate the expressions. The whole spreadsheet is actually an object with rows and cells, so I pass that into the compiler, run it in my program, and have it calculate the results. Then I display those results in the spreadsheet.
    ---- The problem is this -----
    If I have the expression "R1C1 + 5" in a cell, in code it is actually "Code.Row[1].Cells[1].getResult() + 5". The problem is, R1C1 can either be a cell containing a string, or a cell containing a double. So if R1C1 contains a number, I want getResult() to return a double. If it isn't a number, I want it to return a string (in this scenario it would result in a compile error, so assume R1C1 contains a number.)
    Is there anyway that I can store the result in the Cell object and return either a double or a string at runtime depending on what type the result is? I've tried using the primitive type 'object,' but I end up losing precision and the calculations are wrong. Any solution to this problem would be greatly appreciated. Thanks.

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Yeah, this is doable, if I understand what you are asking. So all the numbers that you get from these cells are strings and you are converting them to int or double is that right? This is what I would try.

                string strCell = "25";
                double dblCell;
                if (strCell == "25") dblCell = Convert.ToDouble(strCell);

    Is that what you are looking for? Let me know if I misunderstood.

    Thanks,
    0
  • 0
    Found the perfect solution: Generics. It's the same principle behind lists. You declare what type you want the class to be at design time and the rest of the methods follow suite.
    0