VB.net ตรวจสอบว่ามีองค์ประกอบอาร์เรย์อยู่หรือไม่

ฉันมีรูทีนย่อย 2 รูทีนที่เรียกใช้รูทีนอื่นเพื่อเขียนไฟล์บันทึก ข้อมูลถูกใส่ไว้ในอาร์เรย์ที่เรียกว่า LogData() รูทีนย่อย 1 มี 3 องค์ประกอบ ในขณะที่รูทีนย่อย 2 มี 4 ถ้าฉันเพิ่งเขียนบันทึกโดยใช้องค์ประกอบ LogData() ทั้งหมด มันจะเกิดข้อผิดพลาดเมื่อรูทีนย่อย 1 ทำงาน เนื่องจากไม่มีองค์ประกอบใดๆ ที่ LogData(3) ฉันจะตรวจสอบได้อย่างไรว่าองค์ประกอบ LogData (3) มีค่าอยู่หรือไม่ และหากเป็นเช่นนั้น ให้เขียนลงในบันทึก ฉันลองใช้คำสั่ง If ด้านล่าง แต่ข้อผิดพลาดนี้:

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

ฉันยังลอง:

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

ถ้าฉันเพิ่งเขียน LogData() ลงในไฟล์บันทึก ฉันก็แค่ใช้ลูป อย่างไรก็ตาม เนื่องจากฉันกำลังเพิ่มข้อความระหว่างแต่ละองค์ประกอบ ฉันจึงไม่สามารถทำได้

ย่อย 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

ย่อย 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

บันทึกย่อย:

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

person B Blackmore    schedule 08.05.2017    source แหล่งที่มา


คำตอบ (1)


ตรวจสอบความยาวของอาร์เรย์

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
person dbasnett    schedule 08.05.2017