|
Question : Odd behavior with C# COM interop and null values
|
|
I have a method on a COM object that I want to call from C#. This object resides in an OCX file, to which I added a reference in my C# project (which automatically created an interop assembly). This method requires one argument - can either be a string, an array or null. When I pass a string to this method, it works (whether I do it from VBScript or C#). Unfortunately, I must pass null to the method, which only seems to work from VBScript and not from C#.
When I use this code in C#, I get an ArgumentException "value does not fall within the expected range" on the last line: TDConnection td = new TDConnection(); [initialize td object..] TestFactory testFact = (TestFactory) td.TestFactory; Test test = (Test)testFact.AddItem (null);
However, when I use the equivalent code in VBScript, it works as expected: Set td = CreateObject("TDApiOle.TDConnection") Set testFact = td.TestFactory Set test = testFact.AddItem (Null)
In either language, If I replace "null" with some string - it will work (but doesn't give me the result I need, because the method behaves differently when given null).
|
|
Answer : Odd behavior with C# COM interop and null values
|
|
It sounds like you need a Variant null. Try this:
Test test = (Test)testFact.AddItem (new System.Runtime.InteropServices.VariantWrapper(null));
|
|
|
|