0

To give some context, I have a spreadsheet that prices financial derivatives by running a Monte Carlo simulation on an arbitrary user input payoff function that is defined by a string (a Python lambda expression). This is passed from Excel to my C# library using Excel DNA. The string is then executed as Python code using IronPython.

I've never had any problems with this but today I wanted to pass some more sophisticated Python code from Excel to C# that requires "\r\n" i.e. force a new line. The problem I am encountering is that when I try and input a backslash in Excel, when the string "arrives" in C# it has been modified as follows:

\ gets modified to "\"

and

"\" gets modified to "\"\\""

I think this is the source of the problem I am encountering (ultimately I get the following exception):

unexpected token '\'

Any help or tips would be really appreciated!

Thanks.

Ben
  • 1,638
  • 2
  • 14
  • 21
  • 1
    There would be no escaping going on between Excel and your C#. Is this a regular Excel-DNA worksheet UDF (public static....) being called from a formula? – Govert Feb 28 '14 at 09:14
  • Yes it's a regular `public static` function defined in C# that is exposed to Excel via Excel-DNA. One thing I should add that might be relevant is that the C# function argument is an `Object` and not explicitly required to be a `String` - could this be causing the problem? – Ben Feb 28 '14 at 18:03
  • 'Object' won't make a difference. How are you looking at the string - in the debugger? I think you're seeing the debugger or watch display escaping your strings. It's not Excel or Excel-DNA. – Govert Feb 28 '14 at 18:10
  • I think you may be right actually. Perhaps it is an issue with IronPython in that case. I will look into that aspect a bit further. Thanks for your help! Also just wanted to add that Exce-DNA is a fantastic tool! :) – Ben Mar 03 '14 at 15:15

0 Answers0