Solved

saving object data

Posted on 2000-02-26
6
142 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now