derek7467
asked on
VB.net catching nulls
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
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, slight modification as your throw exception was causing another null exception.
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?
If largeInteger is nothing Then
Exit Function
Else
low= longInt.LowPart
high = longInt.HighPart
End If
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?
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?
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?
ASKER
I want to consider a null entry blank.
Your method returns a numeric value. You must return a value or throw an exception.
ASKER
it cant just skip it or leave it blank?
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.
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.
ASKER
low = longInt.LowPart