Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving an Arraylist
#1
Where else could I ask?

Hello.

I'm currently using VB.NET for a small project I have interest on. I have created which is a "basic" equipment and inventory window, my next step is creating the shop window, which I can't do until I'm done with the save/load system.

The save/load system here is essencially important, since I pretend to let the players create their own items, so to accomplish it, I'm using an ArrayList, which as base has 1 string and 1 integer-type array.

The problem is that I don't find a way to save the full Arraylist, since, I got to admit, I'm not used enough into save/load programming (I was a .ini bitch back on VB6).

Speaking about VB6, I would use it, but I pretty much don't have it (instead, I already had Visual C++, C#, and VB.NET installed, which this last one was my choose since I'm not confident enough with the C family).

Code:
SaveGame.Add("MyPlayerName")
        'Total number of pieces
        InputData(0) = 0
        'Total money
        InputData(1) = 100
        'Total pieces of each kind
        InputData(2) = 0 'Head
        InputData(3) = 0 'Body
        InputData(4) = 0 'Left Arm
        InputData(5) = 0 'Right Arm
        InputData(6) = 0 'Left Foot
        InputData(7) = 0 'Rigth Foot
        'Battle stuff
        InputData(8) = 0 ' Total matches
        InputData(9) = 0 ' Won Matches
        InputData(10) = 0 ' Lost matches
        InputData(11) = 0 ' Multiplayer Matches
        SaveGame.Add(InputData)

All those are defined. It currently creates the file, but doesn't really write inside it. Ideas?

PD: I'm using some functions I found on the internet, licenseless:

Code:
Public Sub SaveArrayList(ByVal path As String, ByVal arrayList As ArrayList)

        Try
            Dim writer As New StreamWriter(path, False, System.Text.Encoding.Unicode)

            For Each s As String In arrayList
                writer.WriteLine(s.ToString)
            Next

            writer.Close()
        Catch ex As Exception
            Debug.Write(ex.ToString, "SaveArrayList")
        End Try

    End Sub

    Public Function LoadArrayList(ByVal path As String) As ArrayList

        Try
            If Not File.Exists(path) Then
                Return New ArrayList
            End If

            Dim reader As New StreamReader(path, True), s As String, list As New ArrayList

            Do
                s = reader.ReadLine
                If s = vbNullString Then Exit Do
                list.Add(s)
            Loop

            reader.Close()

            Return list

        Catch ex As Exception
            Debug.Write(ex.ToString)
            Return New ArrayList
        End Try

    End Function
Reply
#2
Lea Wrote:VB.Net has built in serialization features, just like I suspected because VB.Net is like java.

http://www.devcity.net/Articles/113/1/d ... ation.aspx
Thanks, loads. *hugs* ^w^
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)