Question Saving Data

stevendee96

New member
Joined
Nov 16, 2010
Messages
4
Programming Experience
Beginner
Hey guys so basically I'm making a game where it's like a collectible item game and when you find items, they get added to your inventory. But the problem is is that what you have say like 3 items in your inventory, and you close the application, all the items will go away because there's no way to save. So how would I make like the program to where it saves like a .DLL file (preferably .DLL not .TXT so users cant edit easy)? Is there a way to do this like maybe when they get an item, it adds a code like IDK help me out here. Thanks
 
You're not going to save a DLL because DLLs are executable code, not data. Simple options to save the data in a form that is not necessarily secure but safe from casual view would be to compress it using a GZipStream or to save it in binary format using a BinaryWriter or perhaps binary serialization. For greater security, you would need to use encryption.
 
Okay cool. I don't really want to worry about security at this moment in time, because I still don't know how to like save the data into perhaps a txt. file, or what you said. What would the code be say if you pick up an item and it's in your inventory, it would be saved to the txt. file so when you open the application again, it's still there because it's in the txt. file?
 
well you could just add a savefiledialog and do the following code:
Private Sub CommandButton1_Click()
If Range("F12").Value = "" Then
MsgBox "Quote # Cell is empty please fill in the required cell."
Application.EnableEvents = False ' so the selection change event isn't called
Range("F12").Select
ActiveSheet.Unprotect "dod"
Range("C12").Interior.ColorIndex = 3
Application.EnableEvents = True
Exit Sub

ElseIf Range("O12").Value = "" Then
MsgBox "Sales Person is empty please fill in the required cell."
Application.EnableEvents = False ' so the selection change event isn't called
Range("O12").Select
ActiveSheet.Unprotect "dod"
Range("K12").Interior.ColorIndex = 3
Application.EnableEvents = True
Exit Sub





End If

With Sheets(1)
strSubject = "SR# - " & .Range("AB3") & " - " & "Bill To - " & .Range("F16")

End With

Kill "P:\SR.xls"

ChDir _
"P:\"
ActiveWorkbook.SaveAs Filename:= _
"P:\SR.xls" _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False


Set myOlApp = CreateObject("Outlook.Application")
Set mymail = myOlApp.CreateItem(olMailItem)
mymail.Subject = strSubject
mymail.Body = "See the customer info inside the doc. YES WE CAN!"
mymail.Display
mymail.ReadReceiptRequested = False
mymail.attachments.Add "P:\SR.xls"
mymail.to = "slz design d.o.d"
mymail.Send

'DISABLED FUNCTION

'MsgBox "You have successfully submitted a sample request to SLZ Design D.O.D", vbExclamation, "COMPANY NAME"

iResponse% = MsgBox("Do you want to save this document", vbQuestion + vbYesNo, "COMPANY NAME")

If iResponse% = vbYes Then

fileSaveName = Application.GetSaveAsFilename( _
fileFilter:="Microsoft Excel Workbook (*.xls), *.xls")
If fileSaveName <> False Then
MsgBox "Save as " & fileSaveName
ThisWorkbook.SaveAs fileSaveName
End If

Else
MsgBox "This sample request will not be saved", vbExclamation, "THARCO"
End If




Application.ScreenUpdating = False




Application.Quit

End Sub

VB.NET:
[/QUOTE]

But i cant remember if that is for excel or microsoft vb...
 
Generally speaking, you use a StreamWriter to write text to a file and a StreamReader to read text from a file. There are lots of examples around so feel free to search. Depending on your needs though, there are simpler options. For instance, you can call File.WriteAllText and .ReadAllText to write and read a single String to and from a file. There's also File.WriteAllLines and .ReadAllLines to write and read a String array containing the lines of text in the file.
 
Yeh, you could just try writing a textfile to start with as explained in the previous post. Best to keep it simple before trying to encrypt it etc? You could just make your own 'protocol' up, so a user would not really know what the data meant in the file unless they randomly changed numbers around..or just apply a very crude method to 'hide' whats in there... for example you could just get the character code and add 10 to it, then it would look a jumbled up mess to anybody who opened the file (obviously when you read the file you subtract 10 from it)...That was just something that sprung to mind, its not anything i've ever tried. And it's by no means as good as proper encryption algorithms.

Remember, whatever the filename is, if you output text to it, a user can still open it in notepad (or whatever) and view the data, regardless of what the extension is.
 
considering that a inventory can have lots of items, items types, different sizes, etc, (assuming a RPG) I'll suggest a different approach from a text file that can give you some really powerful tools.
Use a dataset. Within a dataset it's much easier to locate, modify, process and generate data than in text files. And to save it all you have to do is:
VB.NET:
DS.WriteXml(FileName)
where DS is the name of the dataset you're using and Filename is the path in disk.
After this file is in disk you follow jmcilhinney suggestion and use GZipStream to zip it or some encryption method to 'hide' your data.

to retrieve this you undo the encryption process and use:
VB.NET:
DS.ReadXML(FileName)
and you're ready to scan through the data
 
After this file is in disk you follow jmcilhinney suggestion and use GZipStream to zip it or some encryption method to 'hide' your data.
If you were going to use DataSet.WriteXml, which is a good idea, you wouldn't save the file first and then compress it. WriteXml is overloaded and accepts a Stream also. As such, you can write directly to a GZipStream, so you can compress the data and write to a file via a FileStream in one line.
 
If you were going to use DataSet.WriteXml, which is a good idea, you wouldn't save the file first and then compress it. WriteXml is overloaded and accepts a Stream also. As such, you can write directly to a GZipStream, so you can compress the data and write to a file via a FileStream in one line.

good point, I've only used as filepath, therefore I didn't know this better way.
I tried to sketch the code for it, could you confirm it?
VB.NET:
        ' Initialise the objects (probably OnLoad event)
        Dim FileName As String = "c:/temp/myConfig.dat"
        Dim myStream As New System.IO.FileStream(FileName, IO.FileMode.OpenOrCreate)
        Dim CompressZip As New GZipStream(myStream, CompressionMode.Compress, False)
        Dim DecompressZip As New GZipStream(myStream, CompressionMode.Decompress, False)

' ------------------------------------ 
        ' To save the data
        DS.WriteXml(CompressZip)

' ------------------------------------ 
        ' To read the data
        DS.ReadXml(DecompressZip)

and of course, it needs some checking if it's the 1st access to the inventory to allow a initial structure to be created in the dataset.


======== EDIT =======

I've tested for myself... this successfully saved and loaded the information from the dataset:
VB.NET:
    Dim Filename As String = Environment.GetFolderPath(SpecialFolder.DesktopDirectory) & "\temp.dat"

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim myStream = New System.IO.FileStream(Filename, IO.FileMode.Create)
        Dim CompressZip = New GZipStream(myStream, CompressionMode.Compress, True)

        Dim ds As New DataSet
        ds.Tables.Add("one")
        ds.Tables("one").Columns.Add("Col1")
        ds.Tables("one").Columns.Add("Col2")
        ds.Tables("one").Rows.Add("row_1_col_1", "row_1_col_2")
        ds.Tables("one").Rows.Add("row_2_col_1", "row_2_col_2")
        ds.Tables("one").Rows.Add("row_3_col_1", "row_3_col_2")

        ds.Tables.Add("two")
        ds.Tables("two").Columns.Add("Col1")
        ds.Tables("two").Columns.Add("Col2")
        ds.Tables("two").Rows.Add("row_1_col_1", "row_1_col_2")
        ds.Tables("two").Rows.Add("row_2_col_1", "row_2_col_2")
        ds.Tables("two").Rows.Add("row_3_col_1", "row_3_col_2")

        ds.WriteXml(CompressZip)
        CompressZip.Close()
        myStream.Close()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ds As New DataSet

        Dim myStream = New System.IO.FileStream(Filename, IO.FileMode.Open)
        Dim DecompressZip = New GZipStream(myStream, CompressionMode.Decompress, True)

        ds.ReadXml(DecompressZip)
        DecompressZip.Close()
        myStream.Close()
    End Sub
 
Last edited:
Back
Top