Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have this code...

 

Public Structure clsEmployee
   Dim First As String
   Dim Last As String
End Structure

Dim oEmployee As clsEmployee

 

 

How can I check to see if oEmployee equals nothing? The following two lines are invalid...

 

 

If oEmployee Is Nothing Then
If oEmployee = Nothing Then

 

 

I see if I make it a "Class" instead it works, but I'd like to use a Structure in this case if possible. Anyone know what the deal is? I find it especially odd since this line is valid...

 

oEmployee = Nothing

Posted

Well, it is a value type and therefore would have to be SET to Nothing for it to equal Nothing, which would be a wasteful exercise. You could compare one of the structure members for Nothing, but that could be error-prone.

 

If oEmployee.Last Is Nothing Then
 'This works but is not ideal
End If

"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
  • Administrators
Posted

Value types (Structures) cannot be Nothing (null). Reference types (Classes) can.

Net 2.0 introduces the concept of a Nullable type that may work in this scenario - however as a rule of thumb if there is a need to express it as a null / nothing value then use a class rather than a structure.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Is this same reason I can get away with...

 

Dim oEmployee As clsEmployee

 

And start using the var right away. But when I turn it into a class, I having to use this syntax right away instead?...

 

Dim oEmployee As New clsEmployee

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...