Structures, Classes for storing data

A

ArmsTom

I was using structures to store information read from a file. That was
working fine for me, but then I read that anything stored in a
structure is added to the stack and not the heap. So, I made a class
that stores the same information.

The user selects any number of records from the file when the program
loads & can then make changes. The records the user selects are added
to an array and changes are made to the records in that array and then,
when the user saves the information, I update the collection that
records were first pulled from (that is, the collection populated when
the program loads, using the info from the file).

However, since classes are reference types the information is updated
the moment a change is made. I don't really want this. Any thoughts?

Is there a better way of storing/editing the info pulled from the file
other than storing it all in a collection (or array)? Or maybe not
"better" but just someone else's preference.

Thanks.
 
B

Branco Medeiros

ArmsTom said:
I was using structures to store information read from a file. That was
working fine for me, but then I read that anything stored in a
structure is added to the stack and not the heap. So, I made a class
that stores the same information.
<snip>

It's not that stuctures are allways kept on the stack. Local
value-types variables (structures) are allocated on the stack. But if
you store the value in a class (such as an array) then the structure is
allocated on the heap, inside the data space of the containing class.
The user selects any number of records from the file when the program
loads & can then make changes. The records the user selects are added
to an array and changes are made to the records in that array and then,
when the user saves the information, I update the collection that
records were first pulled from (that is, the collection populated when
the program loads, using the info from the file).

However, since classes are reference types the information is updated
the moment a change is made. I don't really want this. Any thoughts?

One possible approach would be to create an immutable class, such as
the String class. In other words, all its properties would be read
only, the only way to alter one of then would be creating another
instance and passing new values to the members in the constructor:

<pseudo-code>
Class MyData
ReadOnly Property Name As String
ReadOnly Property Age As Integer
ReadOnly Property Color As Integer

Sub New(Name As String, Age As Integer, Color as Integer)

Function SetAge(NewValue As Integer) as MyData
Function SetColor(NewValue As Integer) as MyData
Function SetName(NewValue As String) As MyData
End Class
</psudo-code>

Of course, this may mean more trouble than you really need.

Another approach would be to hand out explicit copies of the original
data:

<pseudo-code>
Class MyClass
Sub New(Source As MyData)
...
Sub Assign(Other As MyData)
...
End Class

Dim Copies as New List(Of MyClass)
For Each OriginalValue As MyList In OriginalList
Copies.Add(New MyClass(OriginalValue)
Next

AllowUserToChange(Copies)

Dim I As Integer
For I = 0 to Copies.Count - 1
OriginalList(I).Assign(Copies(I))
Next
</pseudo-code>

And finally, you can allways use structures... =))

HTH.

Regards,

Branco.
 
A

ArmsTom

Well then, if the structure is made part of a collection or an array &
then made part of the heap, I'd say things were working out just fine
before I decided to change things around... ha (and really, given the
small scale of the program, I don't know if I would ever have a problem
with stack space).

You know, I do think what you described would be more trouble, but I
appreciate your efforts nevertheless. :)

Thanks for the reply,

Tom
 
P

Phill W.

ArmsTom said:
when the user saves the information, I update the collection that
records were first pulled from (that is, the collection populated when
the program loads, using the info from the file).

However, since classes are reference types the information is updated
the moment a change is made. I don't really want this. Any thoughts?

If you put references to each object into /both/ Collections then
updating any object "in" one Collection will update the /same/ object in
the /other/ Collection, as in:

Dim A As New Hashtable
Dim B As New Hashtable

Dim C As New DataClass( "key", 73 )
A.Add( C.Key, C )
B.Add( C.Key, C )

? A.Item( "key" ).Value
73
? B.Item( "key" ).Value
73

A.Item( "key" ).Value = 27
? B.Item( "key" ).Value
27

If you want to create a copy of an object, implement your own "Clone"
method. This creates a /new/ instance of the object, then copies the
data values across.

A.Add( C.Key, C )
B.Add( C.Key, C.Clone() )

A.Item( "key" ).Value = 27
? B.Item( "key" ).Value
73

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top