我创建此属性是为了添加多个附件:
Private ReadOnly Index As Integer
Private mAttachments(1 To 10) As String
Private ReadOnly NewAttachment As String
Public Property AddAttachment() As String
Get
If Index >= 1 And Index <= UBound(mAttachments) Then
AddAttachment = mAttachments(Index)
End If
Return mAttachments(Index)
End Get
Set
If Index >= 1 And Index <= UBound(mAttachments) Then
mAttachments(Index) = NewAttachment
Else
MessageBox.Show("Error")
End If
End Set
End Property
为了测试它,我使用了这两个文件的调用,但我收到的电子邮件没有附件。无法理解我可能做错了什么。
mAttachments(0) = "C:\teste.txt"
mAttachments(1) = "C:\teste2.txt"
您可能应该有一个专门的类来处理附件列表。
选项 1:
如果你想保留数组,你可以让这个工作重载一个索引属性。
非索引属性返回数组的副本,以强制消费者使用您的属性更改数组中的值(由于返回了副本,因此任何更改仅适用于副本,而不适用于处理错误的原始数组)。
该m_MaxAttachments
油田是不是绝对必要的。您可以使用Array.Length
.
您不能List(Of String)
使用此设置直接用 a 替换 Array 。
Private ReadOnly m_MaxAttachments As Integer = 10
Private ReadOnly m_Attachments(m_MaxAttachments - 1) As String
Public ReadOnly Property MaxAttachments As Integer = m_MaxAttachments
Public ReadOnly Property Attachments As String()
Get
Return m_Attachments.Select(Function(s) s).ToArray()
End Get
End Property
Public Property Attachments(index As Integer) As String
Get
If index < 0 OrElse index >= m_MaxAttachments Then
AttachmentError(index)
Return String.Empty
End If
Return m_Attachments(index)
End Get
Set
If index >= 0 AndAlso index < m_MaxAttachments Then
m_Attachments(index) = Value
Else
AttachmentError(index)
End If
End Set
End Property
Private Sub AttachmentError(value As Integer)
MessageBox.Show($"Index out of range: {value} Valid range: [0, {m_MaxAttachments - 1}]")
End Sub
现在,您可以使用索引器从/向底层字符串数组获取/设置值,或者使用非索引属性迭代集合的副本。例如:
Attachments(0) = "[Some Path]"
Attachments(1) = "[Other Path]"
Attachments(10) = "[10th Path]" '<- A MessageBox informs that the index is out of range
选项 2:
使用 Class 对象来处理 aList(Of String)
并让 Public 属性仅充当中介者。
该类公开了允许添加/删除/迭代的方法(最后一个留给您,如果需要可以实现;重载的索引属性,移到类中,只返回一个ReadonlyCollection
或一个索引值)。
Imports System.Collections.ObjectModel
Private ReadOnly Property m_Attachments As MyAttachments = New MyAttachments()
Public ReadOnly Property Attachments As MyAttachments
Get
Return m_Attachments
End Get
End Property
Public Property Attachments(index As Integer) As String
Get
Return m_Attachments.Values(index)
End Get
Set
If index >= 0 AndAlso index < MyAttachments.MaxAttachments Then
m_Attachments.Values(index) = Value
End If
End Set
End Property
所以你可以写:
Attachments.Add("Some Path 1")
Attachments.Add("Some Path 2")
Attachments.Add("Some Path 3")
Attachments.Clear()
Attachments.Add("New Path")
' If you add more items than the max Capacity, it will return
' the number of strings added to the List
Dim Items Added = Attachments.AddRange([Some IEnumerable(Of String)])
Attachments.Remove("New Path")
Attachments.RemoveAt(0)
等等。
处理程序类:
Public Class MyAttachments
Private Shared ReadOnly m_MaxCount As Integer = 10
Private ReadOnly m_Values As New List(Of String)(m_MaxCount)
Public Shared ReadOnly Property MaxAttachments As Integer = m_MaxCount
Public ReadOnly Property Values As ReadOnlyCollection(Of String)
Get
Return m_Values.AsReadOnly()
End Get
End Property
Public Property Values(index As Integer) As String
Get
Return m_Values(index)
End Get
Set
If index >= 0 AndAlso index < MaxAttachments Then
m_Values(index) = Value
End If
End Set
End Property
Public Sub Add(value As String)
If m_Values.Count < m_Values.Capacity Then
m_Values.Add(value)
End If
End Sub
Public Function AddRange(values As IEnumerable(Of String)) As Integer
Dim startItemsCount = m_Values.Count
Dim indexer = startItemsCount
For Each value As String In values
If indexer >= m_MaxCount Then Exit For
Add(value)
indexer += 1
Next
Return indexer - startItemsCount
End Function
Public Sub Remove(value As String)
m_Values.Remove(value)
End Sub
Public Sub RemoveAt(index As Integer)
If index >= 0 AndAlso index < m_Values.Count Then
m_Values.RemoveAt(index)
End If
End Sub
Public Sub RemoveRange(indexes As Integer())
For Each index As Integer In indexes
RemoveAt(index)
Next
End Sub
Public Sub RemoveRange(values As String())
For Each value As String In values
Remove(value)
Next
End Sub
Public Sub Clear()
m_Values.Clear()
End Sub
Public ReadOnly Property Count As Integer
Get
Return m_Values.Count
End Get
End Property
End Class
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句