JDYoder Posted August 15, 2005 Posted August 15, 2005 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 Quote
VagabondSW Posted August 15, 2005 Posted August 15, 2005 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 Quote "Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
Administrators PlausiblyDamp Posted August 15, 2005 Administrators Posted August 15, 2005 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
JDYoder Posted August 16, 2005 Author Posted August 16, 2005 Too bad, but good to know. Thanks for the info. Quote
JDYoder Posted August 16, 2005 Author Posted August 16, 2005 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 Quote
Administrators PlausiblyDamp Posted August 16, 2005 Administrators Posted August 16, 2005 That's right. Value types effectively are the data, Reference types are a pointer to the data. That's why reference types need to be initialised before use and value types do not. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Machaira Posted August 16, 2005 Posted August 16, 2005 Is this same reason I can get away with... Yes. :) Value types don't need explicit allocation whereas reference types do. Quote Here's what I'm up to.
IngisKahn Posted August 16, 2005 Posted August 16, 2005 Yup, just like VB6, except now you can assign methods and such to value types. Quote "Who is John Galt?"
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.