The first question that really needs to be asked in this situation is: how
do you plan to address the scoping issue?
Based on your post, you're suggesting that you want to pass the name of a
variable (Which implies that you want to pass it as a string) into your
function (for reasons I am not wholly clear on.) But none the less, just the
name of the variable is not super useful, since you provide no scoping
context to go with it. This means that the only variables that you would be
able to affect would be globally scoped ones. It's more than possible to
have two variables with the same name in different scopes, and the only
variables that stay in scope during a function call anyway are module
globals, and just plain ol' global variables.
However, if you already know the value of the variable, and know the name of
the variable at design time (i.e. you're not trying to build this variable
name at runtime) the you can use a pass by reference routine and pass the
variable directly.
For example:
-----------------------
Sub Main()
Dim TestVar as integer
TestVar = 6
Console.Out.Wri teLine(TestVar. ToString)
Calculate_Somet hing(7,TestVar)
Console.Out.Wri teLine(TestVar. ToString)
End Sub
Sub Calculate_Somet hing(ByVal multiplyer as integer, ByRef InputVar as
Integer)
InputVar = InputVar * multiplyer
End Sub
-----------------------
Which yields the output (If compiled in a command line application):
6
42
Note: There are two important things here. Number one, it is NOT a function.
This is a subroutine with a special thing called a side effect. And number
two, the value is passed ByRef. ByRef tells the compiler that you want to
pass the variable itself to the function, not a copy of the variable (Be
careful here, with reference types, ByVal still passes a copy of the
variable, but the variable itself is really just a pointer, so edits made to
the value of a reference type's members even when passed ByVal are actually
made to the variable itself.)
This will allow you to change the value of the variable from within the
subroutine. However, this can make code maintenance difficult, so make sure
to clearly document your use of side effects. Also, in your originally
proposed Calculate_Somet hing, since it doesn't really need to produce a side
effect the following code snippet would be far simpler to use:
-----------------------
Sub Main()
Dim TestVar as integer
TestVar = 6
Console.Out.Wri teLine(TestVar. ToString)
TestVar = Calculate_Somet hing(7,TestVar)
Console.Out.Wri teLine(TestVar. ToString)
End Sub
Function Calculate_Somet hing(ByVal multiplyer as integer, ByVal InputVar as
Integer) as Integer
Return InputVar * multiplyer
End Function
-----------------------
This method is especially appealing as it allows you to either reassign the
input variable (without the use of side effects) as well as assign a new
variable or use it in a larger expression like:
TestVar = 72+Math.Abs(Tes tVar)+Calculate _Something(15,T estVar)
Hope this helps.
--
Signed,
John-Michael O'Brien
Student, Urban Drainage and Flood Control District
"Blaxer" <Bl****@discuss ions.microsoft. com> wrote in message
news:4F******** *************** ***********@mic rosoft.com...
My first post may not have been clear, here it is clarified a bit...
function Calculate_Somet hing(ByVal multiplyer as integer, ByVal
name_of_variabl e as ___?) name_of_variabl e = name_of_variabl e * multiplyer
end function
Assuming that the variable I plug in to name_of_variabl e is an integer
variable, I should be able to use this function to change the value of any
variable I want. Or at least that is what I want to be able to do.