Welcome Guest ( Log In | Click here to Register a free account now! )
Welcome to Bleeping Computer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.![]() ![]() |
Feb 16 2008, 10:55 PM
Post
#1
|
|
|
Multi Megaton Malware Munition ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Team Posts: 3,971 Joined: 17-January 08 From: Northfield, Ohio Member No.: 184,215 |
CODE Public Sub New(ByVal old As map) 'HACK HACK HACK 'Considering the # of times that this object is dealt with, it should not be a problem , but ' Dim str As New IO.MemoryStream Dim xml As New Xml.Serialization.XmlSerializer(GetType(map)) xml.Serialize(str, old) Dim tmp As map str.Seek(0, IO.SeekOrigin.Begin) tmp = CType(xml.Deserialize(str), map) str.Close() oses = tmp.oses selected = tmp.selected name = tmp.name discription = tmp.discription End Sub This seems pretty inefficient to me.. and I'd like to know if there is any better way to deal with this. Any ideas? -------------------- The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please say so in My Guestbook. ![]() |
|
|
|
Feb 17 2008, 07:51 AM
Post
#2
|
|
![]() Hail Groovicus! ![]() ![]() ![]() ![]() ![]() ![]() Group: Site Admin Posts: 5,991 Joined: 5-June 04 From: Vermillion, SD Member No.: 689 |
I am not sure what you are asking. The title of the thread has to do with copying a class, but then you ask about passing variables. One doesn't pass variables; one passes references or values. In general, primitive data types are passed by value, and non-primitives are passed by reference, and it makes sense to do it that way. Primitives take up very little memory, so there is not much overhead in passing values onto the stack. Non-primitives on the other hand is a completely different story. If for instance you are using a map, then you have the overhead of copying the map, and cleaning up the old map. This is precisely why the reference is passed, and not the actual data structure. Non-primitives can be enormous. Imagine the overhead if you had ten million nodes in your map. There is also the potential for data to become unsynchronized during the copy process.
There exists the byVal and byRef modifiers, which you are already using. How do you know that you are not passing by value? What do you hope to accomplish? -------------------- |
|
|
|
Feb 17 2008, 09:57 AM
Post
#3
|
|
|
Multi Megaton Malware Munition ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Team Posts: 3,971 Joined: 17-January 08 From: Northfield, Ohio Member No.: 184,215 |
Take an ArrayList for example, and copy it.
The problem is, that if I pass the values byVal, I will have a copy of the arraylist itself. But, I will have not copied all of the objects in the arraylist, simply thier refrence. The serialization copys the values for all subclasses of the current class. Sorry if that wasn't clear. Billy3 -------------------- The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please say so in My Guestbook. ![]() |
|
|
|
Feb 17 2008, 10:45 AM
Post
#4
|
|
![]() Hail Groovicus! ![]() ![]() ![]() ![]() ![]() ![]() Group: Site Admin Posts: 5,991 Joined: 5-June 04 From: Vermillion, SD Member No.: 689 |
A collection by definition is just a list of references. If you have an arraylist if objects, it is not the objects that are contained in the arraylist. That would be a nightmare to keep correlated, and inefficient. If it were, then there would need to be a contiguous block of memory to contain all of the objects. Every time the collection 'grew' out of it's memory space, the entire contents of the memory would have to be moved around in order to make room. While that might not seem like a big deal, consider that every single process loaded into memory would have to somehow store its state, rewrite its location in memory, and then somehow restore its state. Meanwhile the entire system would stop working while this was going on. If for some reason your collection grew too large to be contained in memory, then what? You would run out of memory, and your application would crash, or cause other applications to crash. Either way, it is not a satisfactory solution.
Languages are designed is to allow for flexibility and speed. By keeping a collection of references, the objects themselves can be written to any place that there is room without disturbing any other processes at all. If for some reason memory starts to run low, it is much quicker to write a small bit of data to the swap file than it is to have to write an entire collection of objects. Is this making things any clearer for you? -------------------- |
|
|
|
Feb 17 2008, 11:07 AM
Post
#5
|
|
|
Multi Megaton Malware Munition ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Team Posts: 3,971 Joined: 17-January 08 From: Northfield, Ohio Member No.: 184,215 |
Yes, I understand WHY a collection is kept as references. But I am in a situation where I need to copy the data, the references.
What I am doing is making a copy of a MAP object, and passing it to a form, which edits the MAP object. Then, I need to be able to revert any changes to the map if the edits are cancelled. So, what I have done is create a temporary copy of the class in ram, while the user edits it. If the changes are accepted, then the old map object is deleted from an ArrayList (in another class), and the temporary map is pushed into the arraylist. But, I need to be able to create the first temporary copy for the user to edit. Inside the MAP class, there are a bunch of OSOP classes. The OSOPs contain some arraylists of String objects, as well as a few boolean values. In fact, they are more like a struct than a class, in that the OSOPs have no methods. Anyway... I think Im just going to leave this as it is, given the ridiculous class structure under this one. By the time I call a ton of functions to get the values copied over, the serializer could have done its thing. Thanks for the help though. -------------------- The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please say so in My Guestbook. ![]() |
|
|
|
Feb 17 2008, 11:43 AM
Post
#6
|
|
![]() Hail Groovicus! ![]() ![]() ![]() ![]() ![]() ![]() Group: Site Admin Posts: 5,991 Joined: 5-June 04 From: Vermillion, SD Member No.: 689 |
It probably would have been helpful to explain what you were doing in the first place. The common way of doing that is to create a new Collection, creating new objects, copying the values from the old objects to the new objects, and then inserting them into the new collection. I have a huge map that I have to do the same thing with in a BioInformatics application (which I also have to keep synched with the database). I am not very familiar with .net, but in Java, all objects have built in copy methods. I am assuming by your comments that this probably isn't the case with your objects. -------------------- |
|
|
|
Feb 17 2008, 11:53 AM
Post
#7
|
|
|
Multi Megaton Malware Munition ![]() ![]() ![]() ![]() ![]() ![]() Group: HJT Team Posts: 3,971 Joined: 17-January 08 From: Northfield, Ohio Member No.: 184,215 |
Well.. the problem is, that I made all of these classes myself. There is probably some "Correct" way to do it..... but since I taught myself this VB thing I dont know.
Anyway, have a nice day! PS: Here is what Im working on..... its a backup program. The idea is to copy someones data from a non bootable windows installation. But set what to copy, and be able to repeat it at the press of a button. A la Fab's AutoBackup. http://billy-oneal.com/Projects/Automatic%...ta%20Copier.exe -------------------- The forum is always a busy place. In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please say so in My Guestbook. ![]() |
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 11th October 2008 - 08:04 AM |