Как сохранить всю объектную переменную на диск?
Создалл модуль класса,
объявил переменную объектную.
1) Как сохранить в файл всю переменную?
2) Как потом загрузить оную?
MSDN писал(а):Using the PropertyBag Object to Persist an Object
In order to persist an ActiveX component, you need to create an instance of a PropertyBag object. This may seem redundant — after all, the class already has it's own PropertyBag. Why can't you just use that? Simple. When the object goes away, so does its PropertyBag. It only exists in memory; for persistence you need to store a copy of the object somewhere so you can retrieve it later.
Think of a PropertyBag as a sack that you can fill up with stuff and stash away somewhere for safe keeping. Where you stash it is entirely up to you. The following form code demonstrates how you can persist an object to a text file:
- Код: Выделить всё
Private pb As PropertyBag ' Declare a PropertyBag object.
Private LoanObject As Loan ' Declare a Loan object.
Private Sub Form_Unload(Cancel As Integer)
Dim varTemp as Variant
' Instantiate the PropertyBag object.
Set pb = New PropertyBag
' Save the object to the PropertyBag using WriteProperty.
pb.WriteProperty "MyLoanObject", LoanObject
' Assign the Contents of the PropertyBag to a Variant.
varTemp = pb.Contents
' Save to a text file.
Open "C:\Loandata.txt" For Binary As #1
Put #1, , varTemp
Close #1
End Sub
The Contents property of the PropertyBag object contains the Loan object stored as an array of bytes. In order to save it to a text file, you first must convert it to a data type that a text file understands — in this case, a Variant.
Depersisting An Object
Once the object is contained inside a text file (or any other type of storage), it can easily be transported to another location. Imagine that our Loan object contains not only the InterestRate, but also property values to represent all of the fields in a loan application. You could take the Loandata.txt file and send it to the central office for approval. The code for a form that would reuse the Loan object would look something like this:
- Код: Выделить всё
Private pb As PropertyBag ' Declare a PropertyBag object.
Private LoanObject As Loan ' Declare a Loan object.
Private Sub Form_Load()
Dim varTemp As Variant
Dim byteArr() as Byte
' Instantiate the PropertyBag object.
Set pb = New PropertyBag
' Read the file contents into a Variant.
Open "C:\Loandata.txt" For Binary As #1
Get #1, , varTemp
Close #1
' Assign the Variant to a Byte array.
ByteArr = varTemp
' Assign to the PropertyBag Contents property.
Pb.Contents = ByteArr
' Instantiate the object from the PropertyBag
Set LoanObject = pb.ReadProperty("MyLoanObject")
End If
You may have noticed that the object had to be assigned three times: first from the text file to a Variant, then from a Variant to a Byte array, then to the Contents property. That's because the Contents property will only accept a Byte array — if you tried to assign any other data type you would get an error.
So what's going on here? Can you actually take an object created in one place and reuse it in another, complete with its data? Well, not exactly. The original object is long gone. What you are passing in a PropertyBag is an exact copy of the object, not the object itself. This ability to "clone" an object for reuse is a powerful concept, especially when it comes to designing workflow applications.
tyomitch писал(а): ' Assign the Variant to a Byte array.
ByteArr = varTemp
zHackLeX писал(а):А почему, ккак пытаюсь сохранять коллекции объектов, при попытке загрузки он не загружает ничего вообще?
(разумеется, все классы persistable)
Cryonyx писал(а):Простите, вопрос немного не в тему, но может мне кто- нибудь объянить механизм этого преобразования:tyomitch писал(а): ' Assign the Variant to a Byte array.
ByteArr = varTemp
alibek писал(а):Здесь &h22 это первый байт, &h22 второй байт.
Анекдот писал(а):А почему не.... А, так это вы, Путин?
Dim FL As FLI 'Class FLI is a custom persistable class
Private Sub save_Click()
'save
Dim PB As PropertyBag
Dim BAr() As Byte
Dim tmp As Variant
Set PB = New PropertyBag
PB.WriteProperty "CLS", FL
BAr = PB.Contents
tmp = BAr
Open App.Path + "\tes.tst" For Binary As #1
Put #1, , tmp
Close #1
Set PB = Nothing
End Sub
Private Sub Load_Click()
Dim PB As PropertyBag
Dim BAr() As Byte
Dim tmp As Variant
Open App.Path + "\tes.tst" For Binary As #1
Get #1, , tmp
Close #1
BAr = tmp
Set PB = New PropertyBag
PB.Contents = BAr
Set tob = PB.ReadProperty("CLS")
Set PB = Nothing
' set fl=get
End Sub
PB.WriteProperty "CLS", FL
Set FL = PB.ReadProperty("CLS")
Сейчас этот форум просматривают: AhrefsBot, Google-бот и гости: 175