General |
6.84 E’ possibile testare se un valore sia “logicamente” nullo a prescindere dal tipo dato? |
Roberto |
(D) E’ possibile testare se un valore sia "logicamente" nullo a prescindere dal tipo dato? (R) Quando si ha la necessità di verificare se una variabile, un campo o un controllo non contengano alcun valore, ovvero abbiano un valore “logicamente” nullo, normalmente la funzione If va scritta in maniera diversa a seconda del tipo del dato. Usando invece la funzione IsNothing, viene restituito il valore True nei casi in cui: 1) Il valore sia Empty o Null 2) Il tipo dato sia Numerico o Valuta e uguale a zero 3) Una stringa abbia lunghezza zero 4) Il tipo dato sia booleano e uguale a True Function IsNothing(varToTest As Variant) As Integer 'Autore: J. L. Viescas IsNothing = True Select Case VarType(varToTest) Case vbEmpty Exit Function Case vbNull Exit Function Case vbBoolean If varToTest Then IsNothing = False Case vbByte, vbInteger, vbLong, vbSingle, vbDouble, vbCurrency If varToTest <> 0 Then IsNothing = False Case vbDate IsNothing = False Case vbString If (Len(varToTest) <> 0 And varToTest <> " ") Then IsNothing = False End Select End FunctionQui di seguito riporto un esempio dell’utilizzo della funzione IsNothing: If IsNoting(Nome) Then '... Else '... End Ifdove Nome rappresenta il nome della variabile, del campo o del controllo da testare. |