2013-10-10 14 views
7

Ho un codice -
IsNull in VB6 e VB.net

strTest=" "  
IsNull(Trim(strTest)) 

Esso restituisce False in VB6.

Scrivo questo codice di VB.net ma

IsNull (Trim (strTest))

rendimenti Vero.
Quindi, IsNull (Trim ("")) in VB6 = ?? in VB.net
Grazie.

risposta

9

Non c'è la funzione IsNull in VB.Net. Invece ha altre cose come la funzione String.IsNullOrEmpty e la proprietà String.Empty per scoprire se una stringa è vuota o meno.

IsNull in VB6/VBA indica se un'espressione non contiene dati validi. Si sta ottenendo False in vb6 perché è stato inizializzato strTest. Tiene una stringa vuota. Si potrebbe anche voler vedere THIS

VB6

IsNull(Trim(strTest)) 

in VB.Net, IsNullOrEmpty Indica se la stringa specificata è Nothing o una stringa Empty.

VB.NET

If String.IsNullOrEmpty(strTest.Trim) Then DoWhatever 
If strTest.Trim = String.Empty Then DoWhatever 
If strTest.Trim = "" Then DoWhatever  '<~~ Same in VB6 as well 
If String.IsNullOrWhiteSpace(strTest) Then DoWhatever '<~~ VB2010 onwards only 

Tutti questi torneranno True in VB.Net perché la stringa IS VUOTO. Si potrebbe voler vedere

Se il valore della stringa è tutti gli spazi, utilizzare strTest.Trim() prima di utilizzare le prime 3 opzioni oppure utilizzare direttamente la quarta opzione che controlla se non è nulla, o solo una stringa vuota o tutti gli spazi.

+0

'Se IsNull (strTest)' in VB6 = 'If strTest Is Nothing' in VB.net ?? È anche giusto? – nnnn

+1

+1 Sid. Ben spiegato. @nnnn: Sì, è vero. Il controllo 'IsNull' può essere comparato con' Nothing' in VB.NET. Ma tieni presente che le stringhe si comportano in modo leggermente diverso in VB6 e VB.NET. Io uso sempre il controllo 'IsNullOrEmpty' o il controllo' IsNullOrWhiteSpace' (a seconda dei casi), piuttosto che un diretto '" "' 'compare o' Nothing' o 'String.Empty'. VB.NET internamente fa un sacco di cose per rendere simili queste tre cose. Non ho mai sentito l'uso di confrontare qualsiasi stringa con Nothing fino ad ora in vb.net ancora. –

+0

@PradeepKumar 'Non ho mai sentito l'uso di confrontare qualsiasi stringa con Nothing finora in vb.net. Anche io :) e BTW ... Grazie :) –