VBBR |
Posted - Feb 10 2005 : 09:21:53 AM So I'm defining a lot of Types being that some include members which are other types themselves. But I would like to know something... Here are some of my types:
Public Type ZotImage Name As String File As String Rotate As Integer FlipX As Byte FlipY As Byte End Type Public Type ZotImages Count As Long Images() As ZotImage End Type Public Type ZotTile Symbol As Byte Name As String Image As ZotImage End Type
Suppose the following code in the program:
dim im as ZotImages (...) Dim t as ZotTile t.Image = im.Images(5) t.Image.Name = "foo"
My question is... will im.Images(5).Name change as well? |
hotrodx |
Posted - Feb 20 2005 : 10:10:06 PM Byref Only. VB6 does not have a copy constructor of sorts.
However, you can use property bags if you want to copy an instance's variables.
EDIT: Also, you can pass an instance of the same class, then copy all the Public or Friend variables. |
VBBR |
Posted - Feb 15 2005 : 10:05:22 AM So, are the classes in VB6 passed by reference or are they copied? |
Lachlan87 |
Posted - Feb 14 2005 : 6:45:36 PM Those are good points. I have a habit of not really thinking about what it is exactly I'm telling the compiler to do, and I suspect it is harmful to my coding. Seeing as ByVal only copies the pointer, I'm probably only hurting my execution speed by allowing my functions to default to ByVal.
These are the kind of things I was hoping to learn whilst making this game! |
Eric Coleman |
Posted - Feb 14 2005 : 2:36:14 PM Well, I was wondering if things had changed in .NET. I'm not really a .NET guru, so that's why I was curious. You can have classes where the values of properties depend on the order in which you set the properties or call methods. Imagine a simple class with a Value property and two methods Multiply and Add. Assuming Value defaults to 0, then Multiply 2 and then Add 1 results in a value of 1, and Add 1: Multiply 2 would result in a value of 2. Simple, but you get the idea. If a class is to be copied, then it's internal state needs to be copied as well. Code that copies a class can't simply enumerate all properties and then assign to a newly created class because of the problem of order dependance I described above. However, copying classes by copying it's internal data can cause problems as well. Imagine a class that allocates memory internally and stores the pointer to the data in a private variable. A copy of that class would inadvertantly copy the pointer. This would result in the first class to be destroyed freeing the memory, and the second class would generate a general protection fault for either accessing or freeing memory that doesn't exist anymore. So, you can see why I was interested in how a class could possibly be passed by value instead of by reference.
The only reason I see of passing a class by value is so that you don't accidently set it to nothing. ByVal essentially increases the reference count by 1, and ByRef does not. |
Lachlan87 |
Posted - Feb 13 2005 : 6:33:35 PM Eh, once again I have learned that I am not as knowledgeable as I thought I was. When you asked your question, I went back and double-checked my code. Lo and behold, I do pass my classes ByVal---but not the way I thought I was. I was passing them ByVal as though I was really passing them ByRef! I created a knew project and started fiddling around, and the results puzzled me. It seemed as though there was no difference between ByVal and ByRef.
A search on Google lead me to thsi page, which explains. I doubt any of this will surprise you, though. You seemed to be hinting that I wasn't necessarily doing what I thought I was.
Oh, well. I was wrong. At least I learned something though.
|
Eric Coleman |
Posted - Feb 13 2005 : 09:07:50 AM quote: Originally posted by Lachlan87
Sure! I do it in my game.
How does that work? Is all of it's memory copied or is only property values copied or are they assigned? |
VBBR |
Posted - Feb 10 2005 : 4:27:54 PM Actually I'm using VB6, I just commented about how passing classes in .NET works
Anyway I've found another way to do what I needed. Thanks for the reply. |
Lachlan87 |
Posted - Feb 10 2005 : 3:03:01 PM Sure! I do it in my game. |
Eric Coleman |
Posted - Feb 10 2005 : 11:36:26 AM Can you pass a class by value in .NET? |
Lachlan87 |
Posted - Feb 10 2005 : 11:00:49 AM Yeah, you could use a class and pass it by reference in VB.NET. I didn't realize you were using .NET. |
VBBR |
Posted - Feb 10 2005 : 09:37:22 AM That's the problem, I know that in C it'd be easy... what if I change ZotImage to a class? (in VB.NET if you just use "=" it passes a reference...) |
Lachlan87 |
Posted - Feb 10 2005 : 09:24:34 AM No, I don't think so. You are just changing t.image's value, not passing a pointer. |