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.

Well I'm sorry you are under the misunderstanding that this is for academics. This is for business and requires a solution fairly quickly. But if I must explain...

I used to store the result as an object. That way it could either be a number or a string. I would just determine the type (double or string) through an algorithm and convert it accordingly. That, however, caused problems since I lose the precision of a double by storing it in an object. So if there is someway to call one method that can return a double OR a string (by using some type of polymorphism), I can keep the double precision.

You can't really return more than one value from a single method type, even if you do overloading.

But, what you could do is break up your logic some, or use an object that contains a String, a Double, and a Result Code. Have the code return this Result object, then code logic to say something like this:

Result r;
//do something to populate r

switch (r.ResultCode)
   {
	case ResultCode.Fail:
		// do something with r.String result
	case ResultCode.Success:
		//do something with the r.Double result
   }

Basically, what I'm saying is that you could make an object that has the properties that you want, and based on what the Result Code is.

This article has been dead for over six months. Start a new discussion instead.