Using pointers and passing ByRef in Visual Basic
-
I'm trying to build a card game (who didn't do that once). And I'm struggling with the images. Obviously all of the card face images are different, but the card back images are all the same. Why would you make 52 copies of the same image in memory? So I'm trying to hold the card back image in a common place that all of the cards can access, and just use a pointer to it. I'm having some trouble using pointers in vb. they seem to be relatively easy to use sometimes and I can't figure them out other times. One other question that I had is about passing ByRef. If you pass a reference and assign it to a variable, what is being assigned? Is it the reference or another copy of what the reference points to? I appreciate any help on this. I'm sure they're common questions but I'm having a hard time finding answers.:)
-
I'm trying to build a card game (who didn't do that once). And I'm struggling with the images. Obviously all of the card face images are different, but the card back images are all the same. Why would you make 52 copies of the same image in memory? So I'm trying to hold the card back image in a common place that all of the cards can access, and just use a pointer to it. I'm having some trouble using pointers in vb. they seem to be relatively easy to use sometimes and I can't figure them out other times. One other question that I had is about passing ByRef. If you pass a reference and assign it to a variable, what is being assigned? Is it the reference or another copy of what the reference points to? I appreciate any help on this. I'm sure they're common questions but I'm having a hard time finding answers.:)
On the first part, what version of vb are you using? VB and vb.net have a different way of handling graphics. Also vb.net doesn't have pointers because they aren't really necisary and would screw up the garbage collector. Here's the easiest way to think of byval and byref: if you pass something byval then the called function/sub makes a new copy of the variable and copies the value into it. If you make any changes to that variable it won't effect the original variable. With byref it is exactly the same variable so any changes you make effect the original. For instance:
public sub Sub1 ()
dim x as integerx = 2
Sub2 (x)'if passed byval x = 2
'if passed byref x = 4
end subprivate sub Sub2 (x as integer)
x = 4
end sub
The only tricky part if arrays and objects because they are reference types not value types. In those cases the name of the array or object is really a pointer and when you pass them byval you are making a new pointer to the items they contain not new copies of all the items so a change in one can effect the other in some cases.
-
On the first part, what version of vb are you using? VB and vb.net have a different way of handling graphics. Also vb.net doesn't have pointers because they aren't really necisary and would screw up the garbage collector. Here's the easiest way to think of byval and byref: if you pass something byval then the called function/sub makes a new copy of the variable and copies the value into it. If you make any changes to that variable it won't effect the original variable. With byref it is exactly the same variable so any changes you make effect the original. For instance:
public sub Sub1 ()
dim x as integerx = 2
Sub2 (x)'if passed byval x = 2
'if passed byref x = 4
end subprivate sub Sub2 (x as integer)
x = 4
end sub
The only tricky part if arrays and objects because they are reference types not value types. In those cases the name of the array or object is really a pointer and when you pass them byval you are making a new pointer to the items they contain not new copies of all the items so a change in one can effect the other in some cases.
Thank you for responding. Visual Basic is tricky when it comes to this stuff. What i'm finding is that the runtime takes care of most of these fine details. And you should only spend so much energy on it. Especially when you are talking about making 52 different copies of something. From what I've found VB won't let that happen. But as far as the byval and byref information... It is a very good explaination. Thanks for helping me clear all that up.