Roy Hinkley
Roy Hinkley

Reputation: 10641

Testing object instance returns "Conversion from string "" to type 'Boolean' is not valid."

I am trying to test for an instance of an object, but VB pukes and throws the exception:

Conversion from string "" to type 'Boolean' is not valid.

Here is how I am testing:

Dim objGA As New Gatherer.Gathered("", -1)
objGA = objGatherers(idx) 
If Not objGA Is Nothing Then  <--exception occurs here
   ' Do something here

End If

If I do not do this check then I get:

Object reference not set to an instance of an object.

I don't understand the first error given the objGA is an object not a string!

How else should I perform this test? Is there a consistent way to check?

Upvotes: 2

Views: 266

Answers (3)

dbasnett
dbasnett

Reputation: 11773

Try this:

    Dim objGA As New Gatherer.Gathered("", -1)
    Stop 'examine objGA
    objGA = objGatherers(idx)
    Stop 'examine objGA

At the first stop is objGA a "Gathered object"? When you type the first line does it tell you what the return type is?

Upvotes: 0

ElektroStudios
ElektroStudios

Reputation: 20464

If objGA IsNot Nothing Then
    ' put some code here...
End If

MSDN: IsNot Operator

Upvotes: 2

SanketS
SanketS

Reputation: 973

try this.

try to check null object using IsDBNull(oValue) Method. IsDBNull is inbuilt function to check null value.

Dim oValue As Object
Dim DefaultValue As Object
    If IsDBNull(oValue) Then
        Return DefaultValue
    Else
        Return oValue
    End If

Upvotes: 0

Related Questions