Solved

VB.net catching nulls

Posted on 2014-11-12
9
83 Views
Last Modified: 2014-11-13
I query an ldap server and pull back lastlogon timestamp, which i have to convert with the below function.  What happens sometimes is that value is null if the ID exists but maybe is a system id and lastlogon doesnt have a value and errors out with a nullvalueexception.  How can i try catch a function to just move on from blanks:

Function GetInt64FromLargeInteger(ByVal largeInteger As Object) As Int64
        Dim low As Int32
        Dim high As Int32
        Dim valBytes(7) As Byte

        Dim longInt As IADsLargeInteger = CType(largeInteger, IADsLargeInteger)
        low = longInt.LowPart
        high = longInt.HighPart

        BitConverter.GetBytes(low).CopyTo(valBytes, 0)
        BitConverter.GetBytes(high).CopyTo(valBytes, 4)
        Return BitConverter.ToInt64(valBytes, 0)
        Return BitConverter.ToInt64(valBytes, 0)
    End Function

Open in new window

0
Comment
Question by:derek7467
  • 5
  • 4
9 Comments
 

Author Comment

by:derek7467
ID: 40437274
the error happens on this line:

low = longInt.LowPart
0
 

Author Comment

by:derek7467
ID: 40437321
Also, heres how i call it to a textbox control:

If ADEntry.Properties("lastLogon").Value Is Nothing Then
                    acll.Text = ""
                Else
                    acll.Text = pwdDate.ToString()
                End If

Open in new window

0
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40437749
To check if largeInteger is null, just add a test:
If largeInteger Is Nothing Then Throw New ArgumentNullException("largeInteger")

Open in new window

The caller should ensure it doesn't call this method with a null argument.
0
 

Author Comment

by:derek7467
ID: 40437838
Thanks, slight modification as your throw exception was causing another null exception.

If largeInteger is nothing Then
Exit Function
Else
low= longInt.LowPart
high = longInt.HighPart
End If

Open in new window


Its underlining my exit function telling me it doesn't return a value.  is that ok?  It leaves the text box blank, which is ok.  but does that make sense?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 11

Expert Comment

by:louisfr
ID: 40439296
It's not the job of that method to decide what to do with a null value.
It should throw an exception on a null argument.

The caller should check the value is not null and not call the method if it is.

Sometimes, you can deal with null arguments by returning something significant. In your case, it's not possible : any value you would return could be a valid result. Do you want to consider a null argument as a zero?
0
 

Author Comment

by:derek7467
ID: 40439542
I want to consider a null entry blank.
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40439612
Your method returns a numeric value. You must return a value or throw an exception.
0
 

Author Comment

by:derek7467
ID: 40439955
it cant just skip it or leave it blank?
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40442027
A method declared as returning a value will always return a value.
In VB, if the code does not explicitly return anything, you'll get a warning and the method will return zero. The caller has no way of telling if it's been explicitly returned or if it's the default return value.

The caller should check the largeInteger is not null and call the method only if it's not.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now