2017-05-08 1 views
0

J'ai 2 sous-routines qui appellent un autre pour écrire un fichier journal. Les données sont placées dans un tableau appelé LogData(). La sous-routine 1 contient 3 éléments, tandis que la sous-routine 2 contient 4. Si j'écris simplement le journal en utilisant tous les éléments LogData(), elle commet des erreurs quand la sous-routine 1 s'exécute, car elle ne contient aucun élément à LogData (3). Comment puis-je vérifier si l'élément LogData (3) contient une valeur, et si oui, l'écrire dans le journal? J'ai essayé d'utiliser le ci-dessous Si la déclaration, mais cette erreur:VB.net Vérifier si l'élément de tableau existe

If (Not LogData(3) Is Nothing) Then 
w.WriteLine("Address : {0}", LogData(3)) 
End If 

J'ai aussi essayé:

If LogData.Count > 2 Then 
If Not arrayList(yourIndex) Is Nothing Then 

Si je en train d'écrire logData() dans le fichier journal, je pouvais utiliser une boucle. Cependant, parce que j'ajoute du texte entre chaque élément, je ne peux pas le faire.

Sub 1:

 Dim appData As String = GetFolderPath(SpecialFolder.ApplicationData) 
     Dim LogPath As String = appData & "\myApplication" 
     Dim LogData() As String = {Username, Action, Domain} 
     If (Not System.IO.Directory.Exists(LogPath)) Then 
      System.IO.Directory.CreateDirectory(LogPath) 
     End If 
     Using w As StreamWriter = File.AppendText(LogPath & "\log.txt") 
      Log(LogData, w) 
     End Using 

Sub 2:

 Dim appData As String = GetFolderPath(SpecialFolder.ApplicationData) 
     Dim LogPath As String = appData & "\myApplication" 
     Dim LogData() As String = {Username, Action, Domain, Address} 
     If (Not System.IO.Directory.Exists(LogPath)) Then 
      System.IO.Directory.CreateDirectory(LogPath) 
     End If 
     Using w As StreamWriter = File.AppendText(LogPath & "\log.txt") 
      Log(LogData, w) 
     End Using 

Log sub:

Public Shared Sub Log2(LogData As Array, w As TextWriter) 
    w.Write(vbCrLf) 
    w.WriteLine("Log Entry   : {0} - {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString()) 
    w.WriteLine("username : {0}", LogData(0)) 
    w.WriteLine("Action : {0}", LogData(1)) 
    w.WriteLine("Domain : {0}", LogData(2)) 
    If (Not LogData(3) Is Nothing) Then 
     w.WriteLine("Address : {0}", LogData(3)) 
    End If 
    w.WriteLine("Application version: " & Application.ProductVersion) 
    w.WriteLine("-------------------------------") 
End Sub 

Répondre

0

longueur de départ de matrice

Public Shared Sub Log2(LogData() As String, w As IO.TextWriter) 
    w.Write(vbCrLf) 
    w.WriteLine("Log Entry   : {0} - {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString()) 
    w.WriteLine("username : {0}", LogData(0)) 
    w.WriteLine("Action : {0}", LogData(1)) 
    w.WriteLine("Domain : {0}", LogData(2)) 

    If LogData.Length = 4 Then'<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
     w.WriteLine("Address : {0}", LogData(3)) 
    End If 

    w.WriteLine("Application version: " & Application.ProductVersion) 
    w.WriteLine("-------------------------------") 
End Sub 
+0

Perfec t, ça a marché! Je vous remercie! –