Computing.Net > Forums > Programming > passing by reference in visual basic

passing by reference in visual basic

Reply to Message Icon

Original Message
Name: Dale
Date: June 18, 2002 at 13:41:55 Pacific
Subject: passing by reference in visual basic
Comment:

I am passing a control by reference like this

call doValues(form_preview.basic1.value)

and my function header is something like this

function doValues(byref bas)
'here i change the value of bas
and when i'm outside the function again, i output the new value, and it hasn't changed!!!!
what am i doing wrong?
someone..anyone? its driving me nuts


Report Offensive Message For Removal


Response Number 1
Name: Jeff J
Date: June 18, 2002 at 17:05:16 Pacific
Reply: (edit)

I'm not entirely clear about what basic1 and bas refer to, so it's hard to say. It seems like basic1 is a control, but I don't know what kind, so I can't tell what type value is. As it is, it looks like you're passing a property of a control called basic1, so I don't think the entire control is being passed either ByRef or ByVal.

One problem might be that bas is not defined as a specific type. Thus, VB will default to any named thing being contained in a Variant. For anything beyond the simplest program, that alone can drive you nuts. The history or Variants goes way back, but let's just say someone thought they would make programming easy, like a scripting language. Unfortunately, there are so many problems with Variants, that as of VB7, they no longer exist. Yup, they've been erradicated.

I recommend you put the line "Option Explicit" at the very top of every code file, and also check Tools>>Options>>Editor>>"Require Variable Declaration". Then VB will automatically add that line to all your new files. This makes VB act more like a C++ compiler (actually, it is for VB5/6), and automatically check types during compilation. This will make your life so much simpler in the long run.

Now, back to those calls. Let's assume for a moment, that basic1 is a checkbox. You can rewrite your function like this:

Function doValues(ByRef chkBox As CheckBox)
'code in here
chkBox.Value = 1 'whatever
End Function

The important thing is that the compiler knows chkBox is a CheckBox control, so it won't accept your accidentally passing an Integer or something; you have to pass a CheckBox. You'll have to call it like this:

Call doValues(form_preview.basic1)

If I'm interpreting your post right, then this should work. If not, let me know more details, so I can pin it down.

Cheers


Report Offensive Follow Up For Removal

Response Number 2
Name: thanx
Date: June 19, 2002 at 08:20:37 Pacific
Reply: (edit)

I figured something out, its just a txt box by the way. It isn't my function doing it. I tried replacing the function call with a simple value
like form_preview.basic1.value = 7
and it didn't update then either.
I called it from form_rap, and form_preview wasn't open yet...is that why it didn't change?
I found that if i clicked my preview button in my form_rap to open my form_preview, that my preview form didn't have the appropriate values that it should have. Then while still in the preview form, i switched to design view to check if i had the right control...When i went back, the number was suddenly there! I've tested it with the function call as well and the same thing is happening. Why does it not update until i save or switch views, and do you know if there is a way to fix this?


Report Offensive Follow Up For Removal

Response Number 3
Name: ok this is odd
Date: June 19, 2002 at 08:30:32 Pacific
Reply: (edit)

when i change the function to do only one value (it is currently doing 5) and return it instead of passing by reference, THEN it works in the way i said above only when switching views and back. But when i pass by reference....nothing.


Report Offensive Follow Up For Removal

Response Number 4
Name: Jeff J
Date: June 19, 2002 at 11:39:12 Pacific
Reply: (edit)

Regarding the forms, it doesn't matter in VB if you've opened the other form yet or not. As soon as you attempt to use something on any form, that form and all its controls are created automatically, before the called control is used. In fact, I'm not aware of any other language that will do that for you, though sometimes it is an unwanted thing.

So forget about the form loading sequence; it's still a matter of passing the appropriate stuff. I still get the feeling you're passing various values of the textbox, instead of the textbox itself. That could cause the problem. However, although this is definitely fixable, I can't really figure things out without seeing the code. Since there might be quite a bit, please feel free to email me some of it. Otherwise, I can't tell what's happening and what's not.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: passing by reference in visual basic

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes Today.
Discuss in The Lounge