Ma scusa se lui usa Settings in modo non convenzionale è logico che non possa intercettare l'errore :-\
O forse non ho capito cosa cerca...
Public Sub Main()
Dim sKey As String = "qq" '"x" "qq"
If OpenFromSettings1(sKey) Then
Print "OpenFromSettings1 OK"
Else
Print "OpenFromSettings1 not OK"
Endif
Print "------------------"
If OpenFromSettings2(sKey) Then
Print "OpenFromSettings2 OK"
Else
Print "OpenFromSettings2 not OK"
Endif
Print "------------------"
If OpenFromSettings3(sKey) Then
Print "OpenFromSettings3 OK"
Else
Print "OpenFromSettings3 not OK"
Endif
Catch
Debug Error.Text
Print "anything"
End
Public Function OpenFromSettings1(key As String) As Boolean
Dim returnValue As Boolean = False
Dim sValue As String
Dim bValue As Boolean
Dim iValue As Integer
If IsNull(key) Then
Error.Raise("OpenFromSettings1: Key is null")
Endif
sValue = Settings[key & "/SomeString", key]
Print sValue
bValue = Settings[key & "/SomeBoolean", True]
Print bValue
iValue = Settings[key & "/SomeInteger", 5]
Print iValue
returnValue = True
Finally
Return returnValue
Catch
Debug Error.Text
Print "anything"
End
Public Function OpenFromSettings2(key As String) As Boolean
Dim returnValue As Boolean = False
Dim sValue As String
Dim bValue As Boolean
Dim iValue As Integer
If IsNull(key) Then
Error.Raise("OpenFromSettings2: Key is null")
Endif
sValue = Settings[key & "/SomeString", key]
Print sValue
bValue = Settings[key & "/SomeBoolean", True]
Print bValue
iValue = Settings[key & "/SomeInteger", 33]
Print iValue 'IIf(IsNull(Settings[key & "/SomeInteger"]), 0, Settings[key & "/SomeInteger"])
' Try on statement followed by If Error will see error
returnValue = True
Finally
Return returnValue
Catch
Debug Error.Text
Print "anything"
End
Public Function OpenFromSettings3(key As String) As Boolean
Dim returnValue As Boolean = False
Dim sValue As String
Dim bValue As Boolean
Dim iValue As Integer
If IsNull(key) Then
Error.Raise("OpenFromSettings3: Key is null")
Endif
Try sValue = Settings[key & "/SomeString", key]
If Error Then
Debug Error.Text
Return False
Endif
Print sValue
Try bValue = Settings[key & "/SomeBoolean", False]
If Error Then
Debug Error.Text
Return False
Endif
Print bValue
Try iValue = Settings[key & "/SomeInteger", 1]
If Error Then
Debug Error.Text
Return False
Endif
Print iValue 'IIf(IsNull(Settings[key & "/SomeInteger"]), 0, Settings[key & "/SomeInteger"])
returnValue = True
Finally
Return returnValue
' Catch
' Debug Error.Text
' Print "anything"
End
Giusto per essere più chiari se io scrivo questo codice:
Public Sub Main()
Dim s As String = Settings["Pippo/Pappa"]
Print s
End
non si solleva un errore perché è come se io scrivessi:
Vale a dire che ogni tipo di dato che supporta Null non può sollevare errore se messo a Null
Ma se io scrivo:
Public Sub Main()
'Dim s As String = Null
Dim i As Integer = Settings["Pippo/Pappa"]
Print i
Catch
Debug Error.Text
End
Catch giustamente intercetta l'errore.