473,651 Members | 2,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do you make an array persistent?

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.Cl one(); 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.C lear(); } //
clear list

myObjectsList = new
List<myObjects> (OASnapshot.Len gth); /

myObjectsList.A ddRange(OASnaps hot); //**
}

//** 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
Aug 9 '08 #1
12 2481
Well, I spoke too soon.

Turns out I didn't really have a solution after all (I assume so after
a quick check but it was premature).

Despite my best efforts tweaking the program, using "Clone" instead of
copy everywhere for arrays, using 'new' for arrays and doing a array
element by element clone copy from one array to another (even three
arrays in a row, A->B->C, in a vain attempt to get away from the
dreaded original reference), using Deep Copying for the Clone
function, and making every class I could think of as inheriting
ICloneable, I cannot get the array to be read more than once before it
changes state. That is, undo works just once.

I think the solution is like Marc Gravell said, and that involves
setting up a memento design pattern as I indicated in the OP.

Another solution, which is not really a solution, is to break down the
state you want to reconstruct into primitive data types (int, float,
etc), save this data, and 'reconstruct the state' using this data,
using functions in each of the interacting classes and as the program
dictates. But that 'solution' is hard work, just as hard or harder
than setting up a memento design pattern. In fact it nearly defeats
the purpose of 'undo' if you have to resort to such a drastic solution
(because after the undo you have to reconstruct the present state
too).

I consider this a defect of C#. You should be able to do this more
easily--there should be a library function that will preserve, in
immutable form, all references to mutable objects allowing you to
reconstruct the mutable objects as they were. Of course the memento
design pattern is such a solution, but what I'm saying is that this
memento design pattern should be built into C# as a language feature.
I heard the C# designers deliberately, unlike in C++, did not
introduce the 'const' (constant) keyword for reference objects/
variables, but this deliberate design choice is a defect IMO. A
'const' keyword would go a long way towards solving this problem,
though I'm sure some guru will point out that the IL engine working in
the background will be confused by a 'const' reference, since stuff
changes behind the scenes and nothing is constant save perhaps the
unboxed primitive data types (int, double, bool) on the stack.

RL

Aug 9 '08 #2
On Aug 9, 5:26*am, raylopez99 <raylope...@yah oo.comwrote:
Well, I spoke too soon.

Turns out I didn't really have a solution after all (I assume so after
a quick check but it was premature).
I found a 'solution' and it is rather inelegant--you just keep making
multiple copies of the state you want, then Push it into a Stack.
Then you Pop it from the stack when you want to "undo".

It's basically the same as what I said in my second post, but you
don't use arrays or lists, which were causing problems because of the
"Predicate" (this was a complicated class, and you needed to set up
the Predicate/Action <Tbecause the default apparently was not
working--too much bother, just use Stack)

Still, it would be nice to have something 'built into' the C# language
for readonly instead of depending on a inelegant programmers solution.

RL

Aug 9 '08 #3


Ray,

Serialize it and write it to disk.

Cor

"raylopez99 " <ra********@yah oo.comschreef in bericht
news:1e******** *************** ***********@m3g 2000hsc.googleg roups.com...
>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.Cl one(); 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.C lear(); } //
clear list

myObjectsList = new
List<myObjects> (OASnapshot.Len gth); /

myObjectsList.A ddRange(OASnaps hot); //**
}

//** 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
Aug 9 '08 #4
I consider this a defect of C#.
....
Still, it would be nice to have something 'built into' the C# language
Why? How many other similar languages offer this? C++? java? nope.
Arguably some *libraries* might offer this, but you need to think of
the language and the library as separate entities. Another approach is
to work with immutable objects; immutable objects tend to be more
common in java than C# - but by definition with an immutable object
you don't need to worry about changes... your downstream code might
have a slightly different *copy* of the data, but your upstream code
never, ever, has to care.

So perhaps consider using an immutable design...

Marc
Aug 9 '08 #5
On Sat, 09 Aug 2008 13:14:56 -0700, Marc Gravell <ma**********@g mail.com>
wrote:
>I consider this a defect of C#.
...
>Still, it would be nice to have something 'built into' the C# language

Why? How many other similar languages offer this? C++? java? nope.
Arguably some *libraries* might offer this, but you need to think of
the language and the library as separate entities. Another approach is
to work with immutable objects; immutable objects tend to be more
common in java than C# - but by definition with an immutable object
you don't need to worry about changes... your downstream code might
have a slightly different *copy* of the data, but your upstream code
never, ever, has to care.

So perhaps consider using an immutable design...
Anyone considering that should probably read Eric Lippert's comments on
the topic:
http://blogs.msdn.com/ericlippert/ar...y/default.aspx

I especially recommend the eleven-part series, starting here:
http://blogs.msdn.com/ericlippert/ar...utability.aspx

(Eleven parts makes it long for something found on a blog, but each part
is a nice, bite-sized piece. It's not really as hard a read as one might
think).

He starts with the idea of an immutable Stack implementation, and
gradually works out the techniques one might apply to creating more
complex collection type classes that are also immutable, in each case
doing his best to preserve efficiency by minimizing copies of the data.
The extent to which this is possible varies, but it's a pretty cool
exercise.

Pete
Aug 9 '08 #6
On Aug 9, 9:51*am, "Cor Ligthert[MVP]" <notmyfirstn... @planet.nl>
wrote:
Ray,

Serialize it and write it to disk.

Cor

Turns out whether you do a deep copy (Serialization) or shallow copy
(MemberwiseClon e) the result is the same--success--if you follow the
protocol in this thread, and push/pop the state repeatedly, after
using :ICloneable as the base class for the stuff being cloned.

Trick stuff.

RL
Aug 9 '08 #7
On Aug 9, 1:14*pm, Marc Gravell <marc.grav...@g mail.comwrote:
>
So perhaps consider using an immutable design...

Marc
I'm still learning so I appreciate your feedback, and what you
consider (if it's encapsulated in a few words) as "immutable design".
I know strings are immutable, unless you use StringBuilder, but I'm
not sure what an immutable design is.

Anyway, for this project the problem was solved, after a whole day and
half the night beating out my brains trying different things (what
else is new, it's programming in C#).

RL

Aug 9 '08 #8
or shallow copy (MemberwiseClon e)

I suspect you might run into a few issues if you are using nested data
structures... you might have captured obj.Foo, but not necessarily
obj.Bar.Somethi ng.Whatever[2].TheOther (if you see what I mean). But
if you have solved your immediate issue, then fine...

Marc
Aug 10 '08 #9
Marc Gravell wrote:
Another approach is
to work with immutable objects; immutable objects tend to be more
common in java than C# - but by definition with an immutable object
you don't need to worry about changes... your downstream code might
have a slightly different *copy* of the data, but your upstream code
never, ever, has to care.
I am not so sure Java is more into immutable than C#.

In C# structs are bring the immutable discussion on the table.

Java comes with a few immutable classes and it has also
been recommended for programmers to create immutable classes.
But that recommendation has never caught on.

Arne
Aug 10 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
3306
by: Paul Rubin | last post by:
I've had this recurring half-baked desire for long enough that I thought I'd post about it, even though I don't have any concrete proposals and the whole idea is fraught with hazards. Basically I wish there was a way to have persistent in-memory objects in a Python app, maybe a multi-process one. So you could have a persistent dictionary d, and if you say d = Frob(foo=9, bar=23) that creates a Frob instance and stores it in d. Then if...
0
2481
by: obhayes | last post by:
Hi All, Im using classic ASP (3.0) and I have a web farm with 2 webservers (webserver A and webserver B, both windows server 2003). I do not want to store any client specific information on the webserver (therefore do not intend to use the session object- as you cannot gaurantee which server the user will go to). I want to store a small value (e.g. a Y/N value or an Id) on the client machine for the duration of their browsing session,...
8
29046
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If possible how could one pass these seven values in the array to a function that would check the values. I tried return y but it didn't work
3
4289
by: sanchita | last post by:
Hello everyone, I didn't get any response in "Security" forum hence posting here again. I am having problem with persistent cookies. Even after setting "CreatePersistentCookie" to true in "FormsAuthentication.SetAuthCookie" I'm being logged out after the specifed timeout provided in "forms" element of web.config. I read somewhere that lifetime of persistent cookies depend on the timeout
0
8357
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8277
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8581
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7298
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1588
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.