我正在创建一个可以从List(Of String)
或ListBox.ObjectCollection
“发送”数据的课程。我怎么知道是否使用了什么构造函数?这是我的代码(我可以在我的代码中执行它,但它可能是粗略的方式)。
Imports System.Threading
Public Class ExtendedFileSender
Public LstFileList As List(Of String)
Public LvwFileList As ListBox.ObjectCollection
Public ExTcpClient As ExtendedTcpClient
Private ReadOnly _boolIsList As Boolean
Public Sub New(client As ExtendedTcpClient, data As List(Of String))
ExTcpClient = client
LstFileList = data
_boolIsList = True
End Sub
Public Sub New(client As ExtendedTcpClient, data As ListBox.ObjectCollection)
ExTcpClient = client
LvwFileList = data
_boolIsList = False
End Sub
Public Sub StartSend()
If _boolIsList = True Then
Dim senderThread = New Thread(AddressOf SendFileFromList)
senderThread.Start(LstFileList)
Else
Dim senderThread = New Thread(AddressOf SendFileFromListView)
senderThread.Start(LvwFileList)
End If
End Sub
Private Sub SendFileFromList(fileList As List(Of String))
For Each strFile In fileList
Debug.WriteLine(strFile)
Next
End Sub
Private Sub SendFileFromListView(fileList As ListBox.ObjectCollection)
For Each strFile In fileList
Debug.WriteLine(strFile)
Next
End Sub
End Class
答案 0 :(得分:0)
我多年没有完成VB,但你只需检查哪个字段没有填充。如果您的参数设置为Nothing,我建议您在构造函数中抛出ArgumentNullException。否则你的课程可能无法按照你的意愿运作。
Imports System.Threading
Public Class ExtendedFileSender
Public LstFileList As List(Of String)
Public LvwFileList As ListBox.ObjectCollection
Public ExTcpClient As ExtendedTcpClient
Public Sub New(client As ExtendedTcpClient, data As List(Of String))
ExTcpClient = client
LstFileList = data
End Sub
Public Sub New(client As ExtendedTcpClient, data As ListBox.ObjectCollection)
ExTcpClient = client
LvwFileList = data
End Sub
Public Sub StartSend()
If LstFileList IsNot Nothing Then
Dim senderThread = New Thread(AddressOf SendFileFromList)
senderThread.Start(LstFileList)
Else
Dim senderThread = New Thread(AddressOf SendFileFromListView)
senderThread.Start(LvwFileList)
End If
End Sub
Private Sub SendFileFromList(fileList As List(Of String))
For Each strFile In fileList
Debug.WriteLine(strFile)
Next
End Sub
Private Sub SendFileFromListView(fileList As ListBox.ObjectCollection)
For Each strFile In fileList
Debug.WriteLine(strFile)
Next
End Sub
End Class