Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

saving object data

Posted on 2000-02-26
6
145 Views
Last Modified: 2010-05-02
Can anyone suggest a good method for the following?

I have several different classes, and numerous objects of each class stored in collections.  I need a way to store the data in the objects to a file, and then to be able to restore them again.

Each class is just a bunch of public variables, I would make them types, but I need to be able to add them to collections.  

Currently, my only idea is to create a type for each class containing the necessary variables, and having a method to put the class info into the type, then putting it to a file, and then having a method to get the type from the file, and put the variables back into the class.  However, this seems like an ass-backwards approach, so I'm wondering if there's something I haven't considered yet.
0
Comment
Question by:Kail
  • 3
  • 3
6 Comments
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2560945
Why convert it to a type at all? If you have a lot of small variables you can allocate a byte array with the needed size and using the API RtlMoveMemory (also called CopyMem) to copy the small variables into that byte array, then you can save all the variables to the file with only one Put statement (= faster than adding all variables one-by-one).
0
 

Author Comment

by:Kail
ID: 2561479
Heh, I have almost no idea what you mean.

Could you give me an example?

And also, wouldn't strings be a problem?  You can't use fixed-length strings as public vars in objects(stupid VB).
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2561517
Variable-length strings can be stored by first adding a numerical value that sais how long the string is and then add the string. This way when the program reads back the information it will know exactly how long the string should be and reads that number of characters and store it into the string.

I can probably put a example showing how to do it, or perhaps you could show what kind of variables you want to store and I can show you how to do it. It's really not that hard. At least I do not think so. ;)
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:Kail
ID: 2561557
Please post the example.  I need to store about 50 strings, 2 integers, and 2 collections of strings.

Thanks...:)
0
 
LVL 7

Accepted Solution

by:
Vbmaster earned 50 total points
ID: 2561644
Here's a example I have created, hopefully this will show how you do it. This is not optimized using CopyMem API, this means a lot more work and I'm not sure about how much performance difference you will get.

I'm not really familiar with Collections, I do not use 'em because they do not give too good performance, do your Collections contained keys? Because I could not find a way to extract the keys. Besides from that storing collections works fine.


VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Class1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private m_OneString As String
Private m_AnotherString As String
Private m_OneInteger As Integer
Private m_AnotherInteger As Integer
Private m_Collection As New Collection

Public Sub Load(Optional Filename As String, Optional Filenr As Integer)

  Dim a As Long
  Dim CollSize As Long
  Dim StringLen As Long
  Dim CollItem As String
  Dim CloseFile As Boolean
 
  If (Filenr = 0) Then
    Filenr = FreeFile
    Open Filename For Binary As #Filenr
    CloseFile = True
  End If
 
  'Clear the values
  m_OneString = ""
  m_AnotherString = ""
  m_OneInteger = 0
  m_AnotherInteger = 0
 
  'Read the first string
  Get #Filenr, , StringLen
  m_OneString = Space$(StringLen)
  Get #Filenr, , m_OneString
 
  'Read another string
  Get #Filenr, , StringLen
  m_AnotherString = Space$(StringLen)
  Get #Filenr, , m_AnotherString
 
  'Read a integer (always 2 bytes)
  Get #Filenr, , m_OneInteger
 
  'Read another integer (always 2 bytes)
  Get #Filenr, , m_AnotherInteger
 
  'Clear the collection (we don't want the old items)
  Set m_Collection = New Collection
 
  'Read the collection
  Get #Filenr, , CollSize
  For a = 1 To CollSize
    Get #Filenr, , StringLen
    CollItem = Space$(StringLen)
    Get #Filenr, , CollItem
    Call m_Collection.Add(CollItem)
  Next
 
  If CloseFile Then Close #Filenr

End Sub

Public Sub Save(Optional Filename As String, Optional Filenr As Integer)

  Dim a As Long
  Dim CollSize As Long
  Dim CollItem As String
  Dim StringLen As Long
  Dim CloseFile As Boolean
 
  If (Filenr = 0) Then
    Filenr = FreeFile
    Open Filename For Binary As #Filenr
    CloseFile = True
  End If
 
  'Store the first string
  StringLen = Len(m_OneString)
  Put #Filenr, , StringLen
  Put #Filenr, , m_OneString
 
  'Store another string
  StringLen = Len(m_AnotherString)
  Put #Filenr, , StringLen
  Put #Filenr, , m_AnotherString
 
  'Store a integer (always 2 bytes)
  Put #Filenr, , m_OneInteger
 
  'Store another integer (always 2 bytes)
  Put #Filenr, , m_AnotherInteger
 
  'Store the collection
  CollSize = m_Collection.Count
  Put #Filenr, , CollSize
  For a = 1 To m_Collection.Count
    CollItem = m_Collection.Item(a)
    StringLen = Len(CollItem)
    Put #Filenr, , StringLen
    Put #Filenr, , CollItem
  Next
 
  If CloseFile Then Close #Filenr
 
End Sub

Private Sub Class_Initialize()

  Dim a As Long
 
  'Initialize some dummy values so you can see that the Save
  'and Load procedures works like they are supposed to
  m_OneString = "Test string"
  m_AnotherString = "Contains something else"
  m_OneInteger = 37
  m_AnotherInteger = 400
 
  For a = 1 To 10
    Call m_Collection.Add("Collection String " & a, "key" & a)
  Next
 
End Sub



Let's say you add these two procedures to your class, called OneClass. Then you can save/load the class' variables using code like...

  Call OneClass.Save("C:\Test.txt")   'Save the variables
  Call OneClass.Load("C:\Test.txt")   'Load the variables


However you might want to save multiple classes in the same file, this can easily be done using the Filenr parameter like...

  'Save the variables
  Dim Filenr As Integer
  Filenr = FreeFile
  Open "C:\Test.txt" For Binary As #Filenr
  Call OneClass.Save( , Filenr)
  Call TwoClass.Save( , Filenr)
  'You can add more classes here if you like
  Close #Filenr

  'Load the variables
  Dim Filenr As Integer
  Filenr = FreeFile
  Open "C:\Test.txt" For Binary As #Filenr
  Call OneClass.Load( , Filenr)
  Call TwoClass.Load( , Filenr)
  'You can add more classes here if you like
  Close #Filenr
0
 

Author Comment

by:Kail
ID: 2562871
Cool, thanks a bunch Vbmaster.

Though, I would like to add that its a lot easier if you write and use generic GetStr() and PutStr() functions...:)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question