0

I'm building a VSTO Excel add-in, I would like to use Evaluate to evaluate Excel formulas.

With the following code, when there are some values in A1:A20, it returns a correct result. For instance, if A1 has 3 as value, in the output, it shows System.Object[,], then we can further cast it.

Excel.Application app = Globals.ThisAddIn.Application;
object result = app.Evaluate("=UNIQUE(A1:A20)");
Debug.WriteLine(result);

However, when A1:A20 are empty, in the output, it shows System.Object[*].

Does anyone know what System.Object[*] is? How could I cast such values to get the correct result (e.g., 0 for =UNIQUE(A1:A20) when A1:A20 are empty)?

SoftTimur
  • 5,630
  • 38
  • 140
  • 292
  • Did you try to google the answer? basically the first two results seem to answer your question: https://stackoverflow.com/questions/4539231/what-is-this-object-type-i-get-with-com-interop | https://stackoverflow.com/questions/3731287/how-to-cast-system-object-to-system-object - or don't they? – Rand Random Jun 30 '23 at 13:17
  • Does this answer your question? [What is this \`Object\[\*\]\` type I get with COM interop?](https://stackoverflow.com/questions/4539231/what-is-this-object-type-i-get-with-com-interop) – JuanR Jun 30 '23 at 13:29
  • I got confused with those answers (though I keep trying one by one). Hope somebody could propose a specific answer for my specific question. – SoftTimur Jun 30 '23 at 13:59
  • 1
    But what is your issue, to be precise? - the answers are basically copy & paste ready, without knowing why the answer don't fit your need, we can't help any further: https://dotnetfiddle.net/36wZKV – Rand Random Jun 30 '23 at 14:13
  • @RandRandom Thank you. I don't understand in which cases we need `object[] resultArray = array.Cast().ToArray();`? – SoftTimur Jun 30 '23 at 14:43
  • every time you don't want to deal with a `System.Array` but instead the more commonly known `object[]` - it is just code to turn `System.Array` into `object[]` - maybe this helps: https://stackoverflow.com/questions/49657591 – Rand Random Jun 30 '23 at 14:46
  • 1
    though I want to point out, which type of array you are actually dealing with is unknown - as is described in the question linked of my previous comment `System.Array` is the base class of all array so it could be any form of array eg. single, jagged... - see here: https://dotnetfiddle.net/Do8mJv - the code you mentioned assumes you are dealing with a single dimension array, but that assumption could be wrong – Rand Random Jun 30 '23 at 15:20
  • The result of `Evaluate` can indeed be two-dimensional. – SoftTimur Jun 30 '23 at 15:38

0 Answers0