R
raylopez99
I have an array that I wish to preserve as "read only". The array
holds references to variables myObjects instantiated by new that
change all the time.
The variables are part of a class that I used ICloneable on, namely
"Clone();" (deep and/or shallow copies worked the same for this
particular class).
Using ICloneable, I am able to successfully make a copy of the
variables like so:
myObjectsArray[j] = (MyObjects)X.Clone(); where X is foreach
(myObject X in myObjectsArray)
Everything is working fine except for one thing: I can only preserve
a copy of the array using IClone one time. After that, the array is
no good. But actually I'm quite happy even that this occurs, since my
purpose was to rollback the state of my program using myObjects just
once. So the program works. But I want to know for future reference
how to make the myObjectsArray persistent.
Do I make it 'readonly'? But this only seems to work for ints,
doubles, etc?
Do I use the template (found in a lot of books) of "memento design
pattern" (used in rollbacks)? It looks complicated, a bit of overkill
for this simple problem.
There is a chance that I'm messing up how I pass the 'read only"
array, myObjectsArray[].
Here is how I use it (perhaps I change it involuntarily?)
public void Rollback (myObjectsArray[] OASnapshot)
{
if (myObjectsList.Count != 0) { myObjectsList.Clear(); } //
clear list
myObjectsList = new
List<myObjects>(OASnapshot.Length); /
myObjectsList.AddRange(OASnapshot); //**
}
//** this works to repopulate the List with the 'old' values in
OASnapshot, which is exactly what I want. However, the second time I
call this method "Rollback", later on when the objects have changed
values, OASnapshot does not seem to have any of the 'old' information
of the objects, as I want, but in fact, OASnapshot seems to have
exactly the same information found in myObjectsList. Somehow
OASnapshot is not holding the old values (the second time around).
I tried using 'ref' for OASnapshot but same thing. I think what is
happening is that because of garbage collection, somehow OASnapshot is
not persistent.
Anyway, does this ring a bell with anybody?
RL
holds references to variables myObjects instantiated by new that
change all the time.
The variables are part of a class that I used ICloneable on, namely
"Clone();" (deep and/or shallow copies worked the same for this
particular class).
Using ICloneable, I am able to successfully make a copy of the
variables like so:
myObjectsArray[j] = (MyObjects)X.Clone(); where X is foreach
(myObject X in myObjectsArray)
Everything is working fine except for one thing: I can only preserve
a copy of the array using IClone one time. After that, the array is
no good. But actually I'm quite happy even that this occurs, since my
purpose was to rollback the state of my program using myObjects just
once. So the program works. But I want to know for future reference
how to make the myObjectsArray persistent.
Do I make it 'readonly'? But this only seems to work for ints,
doubles, etc?
Do I use the template (found in a lot of books) of "memento design
pattern" (used in rollbacks)? It looks complicated, a bit of overkill
for this simple problem.
There is a chance that I'm messing up how I pass the 'read only"
array, myObjectsArray[].
Here is how I use it (perhaps I change it involuntarily?)
public void Rollback (myObjectsArray[] OASnapshot)
{
if (myObjectsList.Count != 0) { myObjectsList.Clear(); } //
clear list
myObjectsList = new
List<myObjects>(OASnapshot.Length); /
myObjectsList.AddRange(OASnapshot); //**
}
//** this works to repopulate the List with the 'old' values in
OASnapshot, which is exactly what I want. However, the second time I
call this method "Rollback", later on when the objects have changed
values, OASnapshot does not seem to have any of the 'old' information
of the objects, as I want, but in fact, OASnapshot seems to have
exactly the same information found in myObjectsList. Somehow
OASnapshot is not holding the old values (the second time around).
I tried using 'ref' for OASnapshot but same thing. I think what is
happening is that because of garbage collection, somehow OASnapshot is
not persistent.
Anyway, does this ring a bell with anybody?
RL