[英]Alternative to using Arrays and Redim Preserve
I have just one main requirement, that everything is Serializable. 我只有一个主要要求,即所有内容都是可序列化的。
I initially wanted to use List and Constructors with passable variables which made everything looked nice but then found out both those are not easily serializable. 我最初想将List和Constructors与可传递的变量一起使用,这使所有内容看起来都不错,但是后来发现它们都不容易序列化。
So, currently I am using an Array and ReDim Preserve
but what is a good way to store multiple items and still be able to serialize them? 因此,当前我正在使用Array and
ReDim Preserve
但是什么是存储多个项目并仍然能够序列化的好方法? Every time I go down one path I seem to hit a limitation/dead end. 每次我走一条路时,我似乎都遇到了局限性/死胡同。
I have my main class 'MARKFILE' and it contains a number of 'Markers' 我的主课为“ MARKFILE”,其中包含多个“标记”
Public Class MARKFILE
Private _Name As String
Public ReadOnly Property Name
Get
Return _Name
End Get
End Property
Public Property _Markers() As Marker()
Public Sub Add_Marker(ByRef Aa As String, ByRef Bb As String, Cc As Double)
Dim Mark As New Marker
Mark.A= Aa
Mark.B= Bb
Mark.C= Cc
If IsNothing(_Markers) Then
ReDim _Markers(0)
_Markers(_Markers.Length - 1) = Mark
Else
ReDim Preserve _Markers(_Markers.Length)
_Markers(_Markers.Length - 1) = Mark
End If
End Sub
End Class
Public Class Marker
Public A As String
Public B As String
Public C As String
Public D As String
End Class
There are a number of problems with the code, starting with it wont compile as posted: 代码有很多问题,从它开始就不会像发布时那样编译:
Public Sub Add_Marker(ByRef Aa As String, ByRef Bb As String, Cc As Double)
...
Mark.C = Cc ' Mark.C is String, cannot assign Double To String!
So, first turn on Option Strict. 因此,首先启用Option Strict。
To make a List(of Marker)
serializable, you need to add an attribute: 要使
List(of Marker)
序列化,您需要添加一个属性:
<Serializable>
Public Class Marker
Public Property A As String
Public Property B As String
Public Property C As String
Public Property D As String
End Class
I would also use properties (as shown) rather than Fields. 我还将使用属性(如图所示)而不是字段。 The collection class needs the attribute as well:
集合类也需要该属性:
<Serializable>
Public Class Markers
Private _Name As String
Public ReadOnly Property Name As String
Get
Return _Name
End Get
End Property
Public Property Markers As List(Of Marker)
Public Sub New()
Markers = New List(Of Marker)
End Sub
Public Sub Add(Aa As String, Bb As String, Cc As String)
Markers.Add(New Marker With {.A = Aa, .B = Bb, .C = Cc})
End Sub
End Class
Serializing the data is simple: 序列化数据很简单:
Dim col As New Markers
col.Add("A", "B", "C")
Using fs As New FileStream("C:\temp\marks.bin", FileMode.OpenOrCreate)
Dim bf As New BinaryFormatter
bf.Serialize(fs, col)
End Using
The error you were likely getting was not about the List
but that the stuff in the List
was not marked as serializable. 你可能得到的错误是不是在
List
但在东西List
没有标记为可序列化。
There are also a number of refinements which could (should) be done to the collection class. 还可以(应该)对集合类进行许多改进。 Rather than exposing the collection/List, there should likely be Add, Remove, Clear, Count, Item etc type functions on it to actually manage the list rather than just hold onto it and expose it to the world.
与其公开集合/列表,不如说应该有添加,删除,清除,计数,项目等类型的函数来实际管理列表,而不是仅仅保留它并将其公开。 Add could be overloaded to take a
Marker
object as well. 添加也可以重载以获取
Marker
对象。
It also seems like the Name Property ought to be on the little data class (Marker) rather than the collection. 似乎Name属性应该在小数据类(标记)上,而不是集合上。 As it is (ReadOnly) it is useless because there is no way to set it.
因为它是(ReadOnly),所以它是无用的,因为无法设置它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.