Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, 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.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Copy A Class In Visual Basic .net


  • Please log in to reply
6 replies to this topic

#1 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:13 PM

Posted 16 February 2008 - 10:55 PM

Hello... I am trying to get variables to copy between classes, and not the references to the variables, but the variables themselves. I am currently doing this:
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?
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:11:13 PM

Posted 17 February 2008 - 07:51 AM

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?

#3 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer

  • Topic Starter

  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:13 PM

Posted 17 February 2008 - 09:57 AM

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
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:11:13 PM

Posted 17 February 2008 - 10:45 AM

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?

#5 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer

  • Topic Starter

  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:13 PM

Posted 17 February 2008 - 11:07 AM

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.
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:11:13 PM

Posted 17 February 2008 - 11:43 AM

:thumbsup:

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.

#7 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer

  • Topic Starter

  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:13 PM

Posted 17 February 2008 - 11:53 AM

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
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users