将值添加到另一个类类的类成员

时间:2014-06-06 18:11:17

标签: vb.net

以下程序验证供应商文件。

以下是规则:

  • 当时的流程1供应商
  • 验证以下3个方案(供应商发送文件,未发送文件,发送过多文件)
  • 将验证结果存储在InvoiceFileValidationClass类
  • InvoiceFileValidationClass的1个成员名为" Files"是" ArhiveFilesClass"
  • 的类型列表
  • 会员"档案"需要存储多个文件名以便存档
  • 向InvoiceFileValidationClass的所有其他成员添加值不是问题
  • 将值添加到成员"文件"在以下代码行中生成一个null异常: 的 ValidateFileCount.Files.Add(温度)

有关如何避免空例外的任何想法?

    Public Class InvoiceFileValidationClass
        Public VendorName As String
        Public FileName As String
        Public FileCount As InvoiceFileCount
        Public FileContent As InvoiceFileContent
        Public ErrorMessage As String
        Public Files As List(Of ArhiveFilesClass)
    End Class
    Public Class ArhiveFilesClass
        Public OriginalFilePathandName As String
        Public ArchiveFilePathandName As String
    End Class
Public Enum InvoiceFileCount
    FileMissing
    PassedValidation
    TooManyFiles
End Enum

Public Shared Function ValidateFileContent(Vendor As VendorClass) As InvoiceFileValidationClass
        ValidateFileContent = New InvoiceFileValidationClass
        ValidateFileContent.FileContent = InvoiceFileContent.PassedValidation
        Dim MyUnzippedFolder As DirectoryInfo = New DirectoryInfo(Vendor.FTPInPath)
        Dim LineNumber As Integer = 0
        Dim ErrorLine As String = Nothing
        For Each VendorFile In MyUnzippedFolder.GetFiles()
            If InStr(VendorFile.Name.ToString, Vendor.InvoiceFileMask) <> 0 Then 'match mask
                If File.Exists(VendorFile.FullName) Then
                    REM
                    REM Check File Name Extension
                    REM
                    If VendorFile.Extension.ToUpper <> ".CSV" Then
                        ValidateFileContent.ErrorMessage = Vendor.VendorName & ": did not provide a csv file."
                        ValidateFileContent.FileContent = InvoiceFileContent.NotCSVFile
                    End If

                    REM
                    REM Check Column Count Per Row 
                    REM
                    Using reader As New StreamReader(VendorFile.FullName)
                        Dim line As String = reader.ReadLine()
                        While line IsNot Nothing
                            Dim temp As New InvoiceTextFileClass
                            Dim fields() As String = line.Split(",".ToCharArray())
                            If LineNumber > 0 Then 'Do Not Analyze, this is the column header
                                If fields.Length <> Vendor.ExpectedColumnCount Then
                                    ValidateFileContent.FileContent = InvoiceFileContent.IntegrityCheckFailed
                                    ErrorLine = ErrorLine & LineNumber.ToString & ", "
                                End If
                            End If
                            line = reader.ReadLine()
                            LineNumber += 1
                        End While
                    End Using
                End If
            End If
        Next
        If ErrorLine <> Nothing Then
            ValidateFileContent.ErrorMessage = Vendor.VendorName & ": integrity check failed in the following rows (" & ErrorLine & ")."
        End If

        REM
        REM Check File Row Count
        REM
        If LineNumber < 2 Then
            ValidateFileContent.ErrorMessage = Vendor.VendorName & ": empty file."
            ValidateFileContent.FileContent = InvoiceFileContent.FileIsEmpty
        End If
    End Function

1 个答案:

答案 0 :(得分:7)

以下是重申我们评论的示例

Module Module1

    Sub Main()

        Dim test As New TestingSomethingHere    
        test.Files.Add("test") '' THIS WORKS!!!! BECAUSE FILES HAS BEEN INITIALIZED!!!

        Dim test2 As New TestingSomethingElseHere    
        test2.Files.Add("test2") '' THIS DOESN'T WORK!! BECAUSE FILES HASN'T BEEN INITIALIZED!!!

    End Sub

    Public Class TestingSomethingHere    
        Public Files As New List(Of String)    
    End Class

    Public Class TestingSomethingElseHere    
        Public Files As List(Of String)    
    End Class

End Module

请注意,在我的TestingSomethingHere课程中,当我声明New时,我会使用Files关键字。这告诉它每当构造该类时,它会自动初始化该成员。 (你没有这个)

在我的TestingSomethingElseHere课程中,我不使用它(就像你一样),当我尝试向集合添加内容时,我得到NullReferenceException。这是因为Files 尚未初始化

有很多方法可以初始化它,但关键是你必须初始化它


要完全拼出来,这是你的班级:

Public Class InvoiceFileValidationClass
    Public VendorName As String
    Public FileName As String
    Public FileCount As InvoiceFileCount
    Public FileContent As InvoiceFileContent
    Public ErrorMessage As String
    Public Files As List(Of ArhiveFilesClass)
End Class

这就是你需要的。

Public Class InvoiceFileValidationClass
    Public VendorName As String
    Public FileName As String
    Public FileCount As InvoiceFileCount
    Public FileContent As InvoiceFileContent
    Public ErrorMessage As String
    Public Files As New List(Of ArhiveFilesClass)
End Class

改变是

Public Files As List(Of ArhiveFilesClass)

更改为

Public Files As New List(Of ArhiveFilesClass)

这是在.NET中初始化需要它的对象的一种方法