Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Caching / June 2004

Tip: Looking for answers? Try searching our database.

Custom class loaded from cache returns an object by reference

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Thomas Christensen - 18 Jun 2004 23:26 GMT
The item was not posted first time - trying again...

Hi,

I have a problem with a custom class of mine. My situation is this: I have a class

public class Something
{
   public int Id;
   public string Text;
   public ArrayList arrList;

   New(int IdToRetrieve)
  {
      if(Cache[IdToRetrive.ToString()]!=null)
     {
            Something s = (Something) Cache[IdToRetrieve.ToString()];
            this.Id = s.Id;
            this.Text = s.Text;
            this.arrList = s.arrList;
     }
     else
     {
           this.Id = IdToRetrive;
           this.Text = "SomeText";
           this.arrList = new ArrayList();
     }
  }
}

my problem is that when I on an aspx page do:
Something s = new Something(1);

I get a reference to the cache object; what I really want is a local copy, so when I change the properties of the s object, it does not get updated in the Cache item.
Like when I do eg.
s.arrList.Add("new String object");

the property arrList is empty next time I instatiate the "Something object". I have tried implementing the ICloneable interface in my class and do a
Something s = new Something(1).Clone();

but end up with the same result if I add an item to arrList.

Can anyone please help.

}
Anders Borum - 21 Jun 2004 19:06 GMT
That's basically the way .NET works. Objects are always passed by reference.
You could implement the ICloneable interface, and in that implementation
create a new instance of the object and "transfer" state from the local
object to the fresh instance.

Look into the ICloneable interface implementations online - google can help
you here :-)

Signature

venlig hilsen / with regards
anders borum
--

Scott M. - 22 Jun 2004 01:30 GMT
All objects are passed ByRef by default (as you said), but you can change
that by indicating that you want it to pass ByVal (at least in VB .NET, you
can).

Now, if you do this with a reference type, you'll get a copy of the pointer
that points to the object in memory (as opposed to a reference to the
original pointer).  In either case, a change to the object being passed will
result in a change to the original, but when you pass a reference type
ByVal, you can change the passed object's pointer to point to something else
without affecting the original pointer.  It is a subtle difference.

> That's basically the way .NET works. Objects are always passed by reference.
> You could implement the ICloneable interface, and in that implementation
[quoted text clipped - 3 lines]
> Look into the ICloneable interface implementations online - google can help
> you here :-)
Anders Borum - 22 Jun 2004 11:22 GMT
Hello!

Well, I was thinking about getting into that, but hey - thanks for pointing
it out.

Signature

venlig hilsen / with regards
anders borum
--


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.