Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ByVal, ByRef? variable passing

Status
Not open for further replies.

notuserfriendly

Programmer
May 7, 2004
82
0
0
SE
Anybody knows how many times a variable can be passed, if
there's a limit. I'm using a form in excel, recieve a string go to a function
where I check it, do stuff and then send a new string
over to another function as a string,
try to run a switch, but the switch doesn't
find it, allthough i see the value in debug mode, and it should be matched.
however it doesnt. If i call the function from main, it works otherwise no.
would appreciate if someone knows
[sadeyes]
I
 
If you post the code, it might be easier to identify the problem.

Hope this helps.

- Glen

Know thy data.
 
It's more than 500 rows, but i will try to show you the crucial parts and variable types and so:

Using a form with textboxes. called box1 and so on
After an box1_AfterUpdate event I get the value from one of the textboxes ie carNameBox.Value (like Volvos40 - cab)
send it to a function getCarname.

Code:
Public Function getCarname(carType as String) as String.

There I split do stuff, creates a new string with a joining of two arrays.
returning the string to the AfterUpdate sub.
using the string I got to another sub.

Code:
Public Sub UpdatePriceAndHours(CarType As Variant)

here's where it gets strange:
i have an integer column that is needed for a Vlookup
to get it i send it to a switch function
Code:
Public Function getStuff(CarType)as Integer
getStuff= Switch(CarType = "VOLVO", "2", _
             CarType = "CHRYSLER", "3")
end Function

this is where it all gets wrong, in the getStuff function i get a null value that puts column=0. As if it didn't find it. But debugging shows me that it is the correct value.

However If I call getStuff("VOLVO") from main I get the correct answer.

What am I doing wrong?
 
Debug your code step by step, watching your manies different CarType variables.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I am debugging step for step. Like I told you, it is the correct value but the switch doesn't take it. I'm just new with vba, normally i work with other languages.
Are all names global?
if I have like carname in function1, pass it on to another(function2), make a new string called something else, sending it to another function3,giving it the same name as the first is it really the same adress?

Would like to know if I can watch the stack or stuff, or how to se adresses.
 
One problem that you may be up against is that you're not consistent with your variable types, and the type changing is one area where you lose the by ref. Also, I would suggest that post the actual function call statements, because the syntax of that call can determine whether a variable is passed byref or byval.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Are all names global?
Only variables Dim'ed outside a Sub or Function body are globals for the module and for the whole project if explicitely stated.
First thing to check is the presence of this :
Option Explicit
as the first line of your code module and then compile the project.
Next please post how the getCarname function is called and how it's return value is set.
And you may insert this in the getStuff function, before the Switch() line:
Debug.Print "getStuff(" & CarType & ")"

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I know, from the start I had them all as String, then made them to variant. Sorry for not being consistent when showing you. When debugging I always se the correct name
even in the switch case. Tried without Cstr() and same effect.

Code:
Private sub carApp_AfterUpdate()
	Dim CarType as string
	...
	CarType = GetCarType(CStr(CarTypeBox.Value))
	Call getCarType(CarType) '1
	Call prices(CarType)     '2	
	...
end sub

function getCartype(car as string) as String
	...
	dim nameA()
	
	nameA = split(car,"-")
	nameA(1) = "tail"
	getCarType = Trim(Join(nameA))
	...
end function

Sub prices(cartype as string)
	dim colNo as integer
	...
	colNo = fun(cartype) 
	...
end sub


function getStuff(Cartype as string) as integer	

getStuff= Switch(CarType = "VOLVO", "2", _
             CarType = "CHRYSLER", "3")

	... 'error handling
end Function
 
CarType = GetCarType(CStr(CarTypeBox.Value))
Call getCarType(CarType)

Why calling getCarType twice, 1st as function and then as sub ?
Seems you don't have Option Explicit as I suggested.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
You are calling the function getCarType with a string, but you are not assigning the return value to anything?

What is the definition of the Sub/Function "fun"?

The snippet that you last posted does not contain a call to getStuff?

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Found it!!
gah this high resolution stuff isn't good for tired eyes
there was a blank in the carname, join doesn't seem to do what I want, will check on the trim function again.

thanx for the tip:
Debug.Print "getStuff(" & CarType & ")"
only used debugging with mouseover, didn't notice there was a blank in between. Now i think i can sleep again.

btw also found stack window.
I
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top