473,847 Members | 1,444 Online

# Set the value of a variable using a variable

There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be...

function Calculate_Somet hing(ByVal multiplyer as integer, ByVal variable as ___?)
variable = 5 * multiplyer
end function

What I would like this function to do is take the name of the incoming variable and assign a calculated value to it.

Any help would be greatly appreciated, TIA!!
Nov 20 '05 #1
10 2263
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.
"Blaxer" wrote:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be...

function Calculate_Somet hing(ByVal multiplyer as integer, ByVal variable as ___?)
variable = 5 * multiplyer
end function

What I would like this function to do is take the name of the incoming variable and assign a calculated value to it.

Any help would be greatly appreciated, TIA!!

Nov 20 '05 #2
Assuming this is done within a class you could use reflection,
have a look at FieldInfo.SetVa lue method

hth

guy

"Blaxer" wrote:
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.
"Blaxer" wrote:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be...

function Calculate_Somet hing(ByVal multiplyer as integer, ByVal variable as ___?)
variable = 5 * multiplyer
end function

What I would like this function to do is take the name of the incoming variable and assign a calculated value to it.

Any help would be greatly appreciated, TIA!!

Nov 20 '05 #3
Hi Blaxer,

\\\
Public Function calculate(ByVal variable As Double) As Double
Return variable * 5
End Function
Public Function calculate(ByVal variable As Integer) As Integer
Return variable * 5
End Function
///

I think that you can do it as well by sending and returning the variable as
object, however that will not give you less lines of code and is very much
less clean.

I hope this helps?

Cor
Nov 20 '05 #4
I think this is what I need, but I am having trouble making heads or tails of it, how do I set the value of the variable using fieldinfo, it looks as if it only gets attributes ??

"guy" wrote:
Assuming this is done within a class you could use reflection,
have a look at FieldInfo.SetVa lue method

hth

guy

"Blaxer" wrote:
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.
"Blaxer" wrote:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be...

function Calculate_Somet hing(ByVal multiplyer as integer, ByVal variable as ___?)
variable = 5 * multiplyer
end function

What I would like this function to do is take the name of the incoming variable and assign a calculated value to it.

Any help would be greatly appreciated, TIA!!

Nov 20 '05 #5
I think this is what I need but I am having trouble making it work, I am pretty novice, do you know of any code examples that might help me?

TIA

"guy" wrote:
Assuming this is done within a class you could use reflection,
have a look at FieldInfo.SetVa lue method

hth

guy

"Blaxer" wrote:
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.
"Blaxer" wrote:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be...

function Calculate_Somet hing(ByVal multiplyer as integer, ByVal variable as ___?)
variable = 5 * multiplyer
end function

What I would like this function to do is take the name of the incoming variable and assign a calculated value to it.

Any help would be greatly appreciated, TIA!!

Nov 20 '05 #6
Hmm, I don't see how this will "modify" the value of any variable you send to it, am I just missing it?

"Cor Ligthert" wrote:
Hi Blaxer,

\\\
Public Function calculate(ByVal variable As Double) As Double
Return variable * 5
End Function
Public Function calculate(ByVal variable As Integer) As Integer
Return variable * 5
End Function
///

I think that you can do it as well by sending and returning the variable as
object, however that will not give you less lines of code and is very much
less clean.

I hope this helps?

Cor

Nov 20 '05 #7
Hi Blaxer,

Yes I am thinking that or maybe I miss something?

How would you modify if you do not know what it has to be in your function?

Cor
Hmm, I don't see how this will "modify" the value of any variable you send to it, am I just missing it?
"Cor Ligthert" wrote:
Hi Blaxer,

\\\
Public Function calculate(ByVal variable As Double) As Double
Return variable * 5
End Function
Public Function calculate(ByVal variable As Integer) As Integer
Return variable * 5
End Function
///

I think that you can do it as well by sending and returning the variable as object, however that will not give you less lines of code and is very much less clean.

I hope this helps?

Cor

Nov 20 '05 #8
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

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.

Nov 20 '05 #9
Hi Blaxer,

Now I see what you try to do, VBNet is not a scripting language so a
variable name is nothing more than a mnemonic for the programmer what stends

You can try to simulate a scripting language, however it is easier to create
a table, array or whatever where you hold the name of that variable.

In your routine you can than decide based on that table what is the action
you want to take.

Just my though,

Cor
Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 3 8911 by: tornado | last post by: Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns a empty value instead of returning the browser type. Here is the line which i am using in my code and from manual: 7 6426 by: rickcheney | last post by: I just changed my Access 2002 database to a SQL Server ADP project. I had a form where the user entered a value into a text box and when a command button on the form was clicked a Report was opened. The reports record source is a query. The query uses the value from the form text box to restrict the query. Table name = EggsTable one of the columns in the table is named: EggColor Form name = EggColorForm Form text box name = ColorTextBox 15 3255 by: lawrence | last post by: Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone tell me why these DIVs don't drift to the left as they are supposed to?