I'm writing COM wrappers that return an object, or null if the object does not exist. When calling from VBScript, returning null throws the error "Object required: 'ComObj.Prop2'" Code 800A01A8...
C#
public class testCOM
{
public object Func(int i)
{
if (i == 1) return new object();
if (i == 2) return DBNull.Value;
return null;
}
}
VBScript
set ComObj = CreateObject("ClassLibrary1.testCOM")
set TestObj = ComObj.Func(1) 'This Works
set TestObj = ComObj.Func(2) 'Throws "Object required: 'ComObj.Func(...)'" Code 800A01A8
set TestObj = ComObj.Func(3) 'Throws "Object required: 'ComObj.Func(...)'" Code 800A01A8
set TestObj = Nothing 'This is what I want to occur with Func(2) and Func(3)
I tried returning DBNull.Value, which is supposed to marshal as VT_NULL, but no luck...
I really don't want to write a wrapper object similar to Nullabe<> with .HasValue and .Value... Another option I don't like is to create a Nothing object (inside no namespace) so I can do:
If TestObj Is Nothing And TestObj = "Nothing" Then
'TestObj was nothing or "Nothing"
End If
If I use the Nothing object, then my COM object has to reutrn type Object instead of the expected typed object, making the code a bit harder to read though functionally the same for my purposes.
What is the best way to return a null object to VBScript via COM?