Post

 Resources 

Console

Home | Profile | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 VBGamer
 VBGamer
 Type references... how does it work?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

VBBR
Moderator

Brazil
617 Posts

Posted - Feb 10 2005 :  09:21:53 AM  Show Profile  Reply with Quote
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  
  
(...) 'now suppose I add some images to the list... then  
  
Dim t as ZotTile  
t.Image = im.Images(5)  
  
t.Image.Name = "foo"  
  

My question is... will im.Images(5).Name change as well?

Whatever. Who knows...

Lachlan87
Moderator

USA
160 Posts

Posted - Feb 10 2005 :  09:24:34 AM  Show Profile  Reply with Quote
No, I don't think so. You are just changing t.image's value, not passing a pointer.

Go to Top of Page

VBBR
Moderator

Brazil
617 Posts

Posted - Feb 10 2005 :  09:37:22 AM  Show Profile  Reply with Quote
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...)

Whatever. Who knows...

Edited by - VBBR on Feb 10 2005 09:38:37 AM
Go to Top of Page

Lachlan87
Moderator

USA
160 Posts

Posted - Feb 10 2005 :  11:00:49 AM  Show Profile  Reply with Quote
Yeah, you could use a class and pass it by reference in VB.NET. I didn't realize you were using .NET.
Go to Top of Page

Eric Coleman
Gladiator

USA
811 Posts

Posted - Feb 10 2005 :  11:36:26 AM  Show Profile  Visit Eric Coleman's Homepage  Reply with Quote
Can you pass a class by value in .NET?
Go to Top of Page

Lachlan87
Moderator

USA
160 Posts

Posted - Feb 10 2005 :  3:03:01 PM  Show Profile  Reply with Quote
Sure! I do it in my game.
Go to Top of Page

VBBR
Moderator

Brazil
617 Posts

Posted - Feb 10 2005 :  4:27:54 PM  Show Profile  Reply with Quote
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.


Whatever. Who knows...
Go to Top of Page

Eric Coleman
Gladiator

USA
811 Posts

Posted - Feb 13 2005 :  09:07:50 AM  Show Profile  Visit Eric Coleman's Homepage  Reply with Quote
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?
Go to Top of Page

Lachlan87
Moderator

USA
160 Posts

Posted - Feb 13 2005 :  6:33:35 PM  Show Profile  Reply with Quote
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.
Go to Top of Page

Eric Coleman
Gladiator

USA
811 Posts

Posted - Feb 14 2005 :  2:36:14 PM  Show Profile  Visit Eric Coleman's Homepage  Reply with Quote
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.
Go to Top of Page

Lachlan87
Moderator

USA
160 Posts

Posted - Feb 14 2005 :  6:45:36 PM  Show Profile  Reply with Quote
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!

Edited by - Lachlan87 on Feb 14 2005 6:45:55 PM
Go to Top of Page

VBBR
Moderator

Brazil
617 Posts

Posted - Feb 15 2005 :  10:05:22 AM  Show Profile  Reply with Quote
So, are the classes in VB6 passed by reference or are they copied?

Whatever. Who knows...
Go to Top of Page

hotrodx
Squire

43 Posts

Posted - Feb 20 2005 :  10:10:06 PM  Show Profile  Reply with Quote
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.

Edited by - hotrodx on Feb 20 2005 10:20:20 PM
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
VBGamer © Go To Top Of Page
This page was generated in 0.14 seconds. Snitz Forums 2000

Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.