顺便说一句,您不要将变量保存到类中,而是构建您的类以执行这些功能。
所以也许你的课程结构可能是......
Class MyClass
Private mFileName as string
Private mVariables as Dictionary(of TKey, TValue)
'filename property
Property Filename() as string
'variables collection as key value/pairs
Property Variables as Dictionary(of TKey, TValue)
'property accessor for single key/value from collection like Variables(2)
Property VariableX(Index as integer) as string
'function to load saved data from xml file
Function LoadMySavedXmlData(Filename) as Dictionary(of TKey, TValue)
'subroutine to save variables data as xml file
Sub SaveMyXmlData(Filename as string, Data as Dictionary(of TKey, TValue))
End Class
然后你的xml文件可能结构如下
<?xml version="1.0"?>
<MySavedData>
<Filename>Bobs_Prefs.Xml</Filename>
<Data>
<Variable1>
<Key>Name</Key>
<Value>Bob Johnson</Value>
</Variable1>
<Variable2>
<Key>Telephone</Key>
<Value>0123 456 7896</Value>
</Variable2>
<Variable3>
<Key>Car</Key>
<Value>Chrysler</Value>
</Variable3>
</Data>
</MySavedData>
通过使用 XML 文件,不必局限于预定义的字段名称或字段类型,用户可以根据他/她的变量类型键入数字或字符串,例如颜色可以是字符串,如 'RED' 或 HEX像'FF0000'
这将比涉足 SQL 简单得多,并且通过在加载和保存时询问文件名,您可以保存/加载特定的文件。