Reputation: 127
I'm working on an extension for a VB6 legacy application.
We've developed an extension in C#, which is exposed to VB6 via COM. One of the methods in the C# solution returns an object with several properties, some of which are themselves instances of a class defined in the project (not an actual snippet; unfortunately I'm not allowed to post the original code):
public class CustomType
{
...
}
public class ComAdapter
{
public CustomType ExampleProperty
{
get { ... } // edited: this was not an auto-property and causing an exception
}
... (more properties and some logic)
}
In Vb6, we receive an instance of the ComAdapter class. In some cases, the property Instance can be null - this is intended. This should trigger different behavior in the legacy app. The question is: how do I check if the ExampleProperty property is null?
I've tried all of the following:
Dim oAdapter as ComAdapter
... (code to retrieve instance ComAdapter)
If oAdapter.ExampleProperty Is Nothing Then (...)
If oAdapter.ExampleProperty Is Null Then (...)
If oAdapter.ExampleProperty Is Empty Then (...)
If IsNothing(oAdapter.ExampleProperty) Then (...)
If IsNull(oAdapter.ExampleProperty) Then (...)
If IsEmpty(oAdapter.ExampleProperty) Then (...)
All fail. I get "Object reference not set to an instance of an object" in all cases. I'm guessing this is related to the fact that I'm trying to check a property, but I don't know how to solve it.
Edit: I checked that oAdapter is properly set. There are several other properties aside from the one I'm trying to check which are actually accessible, so it doesn't seem likely that oAdapter is the problem.
Upvotes: 2
Views: 271
Reputation: 10931
The first one is the correct syntax.
If oAdapter.ExampleProperty Is Nothing Then (...)
If you're getting "Object reference not set to an instance of an object", either oAdapter
is Nothing
(null
) or ExampleProperty
is not actually an auto-parameter as you suggest.
Upvotes: 2
Reputation: 26259
Perhaps try like this (obviously, I can't test this without your code):
Dim oAdapter as ComAdapter
... (code to retrieve instance ComAdapter)
Dim oCustomType as CustomType
Set oCustomType = oAdapter.ExampleProperty
if oCustomType Is Nothing Then (...)
Upvotes: 0