Google Answers Logo
View Question
 
Q: VB.net null reference testing ( No Answer,   1 Comment )
Question  
Subject: VB.net null reference testing
Category: Computers > Programming
Asked by: sceleratus-ga
List Price: $2.00
Posted: 25 Apr 2004 06:52 PDT
Expires: 25 Apr 2004 17:41 PDT
Question ID: 335856
As a former VB6 programmer, I am used to being able to test for a null reference

e.g.: 
If ObjectReference = null then 
     'Do Stuff
End If

I know that .net does not support direct testing for null (or,
"nothing" in this case). I would like to know the best way to test an
object reference for a null value.

I am using an array of a custom class to hold school grade entries
deleted from a listbox, so that a user may undo the deletion of an
entry (up to the 20 most recent deletions). My "btnRestore" click
event has code similar to the following:

'module level declarations
Dim intDeletedGradeIndex as Integer
Dim DeletedGrades(20) as GradeEntry

   Private Sub btnRestore_Click(ByVal sender As System.Object, _
   ByVal e As System.EventArgs) Handles btnRestore.Click

            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            'If DeletedGrades(intDeletedGradeIndex) <> Nothing Then'
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            DeletedGrades(intDeletedGradeIndex).ListBoxDisplay(lstGrade)
            DeletedGrades(intDeletedGradeIndex) = Nothing
            intDeletedGradeIndex -= 1
            ''''''''
            'End If'
            ''''''''
    End Sub

Thanks,
Sceleratus

Clarification of Question by sceleratus-ga on 25 Apr 2004 17:41 PDT
Thanks for clearing that up. I was confused. You are correct in
assuming I was refering to ADO.NET. I must not have been forming my
boolean expressions correctly in vb.
Answer  
There is no answer at this time.

Comments  
Subject: Re: VB.net null reference testing
From: dotnethelp-ga on 25 Apr 2004 14:38 PDT
 
Sceleratus-

.Net most definatly supports Null reference types (and checking for Null)

Quick example

VB:

        Dim obj As Object = Nothing

        If Not obj = Nothing Then
            MessageBox.Show("Is Not Nothing")
        Else
            MessageBox.Show("Is Nothing")
        End If

C#:

       object obj = null;

       if(obj != null)
       {
           MessageBox.Show("Is Not Nothing")
       }
       else
       {
           MessageBox.Show("Is Nothing")
       }

When you say ".Net does not support direct testing for null", I think
you may be refering to ADO.NET.

If your have a field in a ADO.NET table that is null you have to compare its
value against System.DBNull.Value, you cannot do a direct test against
null (or nothing).  However this does not affect you here.

Regards,

Brett

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy