[C++/CLI] testing a gcrooted member for null

  • Thread starter Thread starter bonk
  • Start date Start date
B

bonk

I have a native class that has a managed private member:

private:
gcroot<MyManagedRefType^> m_managed;

Now at some point in time this member will eventually get initialized:

m_managed = gcnew MyManagedRefType();


How can test, wether this has already happend or not ? Without having to
catch the NullReferenceException (wich will be thrown if I try to access
Member of MyManagedRefType before the ctor was called).

if (m_managed)
if (m_managed != nullptr)

does not do the trick.
 
bonk said:
I have a native class that has a managed private member:

private:
gcroot<MyManagedRefType^> m_managed;

Now at some point in time this member will eventually get initialized:

m_managed = gcnew MyManagedRefType();


How can test, wether this has already happend or not ? Without having
to catch the NullReferenceException (wich will be thrown if I try to
access Member of MyManagedRefType before the ctor was called).

if (m_managed)
if (m_managed != nullptr)

does not do the trick.

You have to cast it back to the managed type:

if (static_cast<MyManagedRefType^>(m_managed) == nullptr)
{
// it's null
}

-cd
 
Back
Top