473,513 Members | 2,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Something(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 2227
My first post may not have been clear, here it is clarified a bit...

function Calculate_Something(ByVal multiplyer as integer, ByVal name_of_variable as ___?)
name_of_variable = name_of_variable * multiplyer
end function

Assuming that the variable I plug in to name_of_variable 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_Something(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
guy
Assuming this is done within a class you could use reflection,
have a look at FieldInfo.SetValue method

hth

guy

"Blaxer" wrote:
My first post may not have been clear, here it is clarified a bit...

function Calculate_Something(ByVal multiplyer as integer, ByVal name_of_variable as ___?)
name_of_variable = name_of_variable * multiplyer
end function

Assuming that the variable I plug in to name_of_variable 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_Something(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,

This is a typical sample of overloading

\\\
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.SetValue method

hth

guy

"Blaxer" wrote:
My first post may not have been clear, here it is clarified a bit...

function Calculate_Something(ByVal multiplyer as integer, ByVal name_of_variable as ___?)
name_of_variable = name_of_variable * multiplyer
end function

Assuming that the variable I plug in to name_of_variable 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_Something(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.SetValue method

hth

guy

"Blaxer" wrote:
My first post may not have been clear, here it is clarified a bit...

function Calculate_Something(ByVal multiplyer as integer, ByVal name_of_variable as ___?)
name_of_variable = name_of_variable * multiplyer
end function

Assuming that the variable I plug in to name_of_variable 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_Something(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,

This is a typical sample of overloading

\\\
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,

This is a typical sample of overloading

\\\
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.WriteLine(TestVar.ToString)
Calculate_Something(7,TestVar)
Console.Out.WriteLine(TestVar.ToString)
End Sub

Sub Calculate_Something(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_Something, 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.WriteLine(TestVar.ToString)
TestVar = Calculate_Something(7,TestVar)
Console.Out.WriteLine(TestVar.ToString)
End Sub

Function Calculate_Something(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(TestVar)+Calculate_Something(15,TestVa r)

Hope this helps.
--

Signed,
John-Michael O'Brien
Student, Urban Drainage and Flood Control District
"Blaxer" <Bl****@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
My first post may not have been clear, here it is clarified a bit...

function Calculate_Something(ByVal multiplyer as integer, ByVal name_of_variable as ___?) name_of_variable = name_of_variable * multiplyer
end function

Assuming that the variable I plug in to name_of_variable 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
for an adres.

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
Below is a code segment that would execute your intended behavior. However,
the nature of this application is difficult to maintain. It also requires
reflection security clearance.

I should note that this application could be easily rewritten using ByRef,
but I would assume that based on your response you are dynamically building
the variable name at runtime (For example loading the var names from a file
or some such runtime only approach.) Keep in mind - reflection is not for
the squeamish. You really shouldn't just jump into it without first fully
understanding the nature of the problem. And when using it to override the
normal variable access methods, understand that it is VERY slow and
difficult to debug. Only consider using reflection this way as a last
resort.

----------
Public Class DemonstrationClass
'Declare the variables we want to be able to change.
Public FirstVar As Integer
Public SecondVar As Integer
Public ThirdVar As Integer

Public Sub AlterVar(ByVal Multiplier As Integer, ByVal VarName As
String)
'Get a reference to the variable we want to change
Dim TargetFieldInfo As Reflection.FieldInfo =
Me.GetType.GetField(VarName)
Dim OldValue As Integer
Dim NewValue As Integer
'Get the old value from ourselves (me) and cast it from Object to
Integer
OldValue = CType(TargetFieldInfo.GetValue(Me), Integer)
'Calculate the new value.
NewValue = OldValue * Multiplier
'And then reassign the new value to our instance of
DemonstrationClass.
TargetFieldInfo.SetValue(Me, NewValue)
End Sub

Public Sub Go()
Dim TargetName As String
Dim tmpMultString As String
Dim tmpMultiplier As Integer

'Initalize our three variables.
FirstVar = 10
SecondVar = 20
ThirdVar = -10

'Display the current state.
Console.Out.WriteLine("Before AlterVar")
Console.Out.WriteLine("FirstVar: " + FirstVar.ToString)
Console.Out.WriteLine("SecondVar: " + SecondVar.ToString)
Console.Out.WriteLine("ThirdVar: " + ThirdVar.ToString)
Console.Out.WriteLine()

'Prompt the user for the var name.
Console.Out.Write("Which Var do you want to change? ")
TargetName = Console.In.ReadLine()
'Prompt the user for the multiplier.
Console.Out.Write("What do you want to multiply it by? ")
tmpMultString = Console.In.ReadLine()
'And get the integer value of what they entered
tmpMultiplier = Integer.Parse(tmpMultString)

'Call our routine.
AlterVar(tmpMultiplier, TargetName)

'And redisplay the state.
Console.Out.WriteLine("After AlterVar")
Console.Out.WriteLine("FirstVar: " + FirstVar.ToString)
Console.Out.WriteLine("SecondVar: " + SecondVar.ToString)
Console.Out.WriteLine("ThirdVar: " + ThirdVar.ToString)
Console.Out.WriteLine()

'Get one char from the buffer (Which won't be populated till the
user presses anyway.)
Console.Out.WriteLine("Press Enter to Continue.")
Console.In.Read()
End Sub

Public Shared Sub Main()
Dim MyDemoClass As New DemonstrationClass
MyDemoClass.Go()
MyDemoClass = Nothing
End Sub
End Class

----------

Below is a sample run of the program (remember, spelling and case are
IMPORTANT):

----------
Before AlterVar
FirstVar: 10
SecondVar: 20
ThirdVar: -10

Which Var do you want to change? FirstVar
What do you want to multiply it by? 22
After AlterVar
FirstVar: 220
SecondVar: 20
ThirdVar: -10

Press Enter to Continue.

----------

Hope this helps. ^.^

--

Signed,
John-Michael O'Brien
Student, Urban Drainage and Flood Control District
"Blaxer" <Bl****@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.com...
Wow, thank you for such a thorough reply, unfortunatly I cannot see how this helps me. Maybe I am not explaining what I need in enough detail or
maybe it is not possible to do this...?
To answer your question, yes the variables (there are 3 of them) are

available to the entire class (global). All I need to be able to do is pass
the name of the variable as a string to a function so that it's value can be
updated. Is that possible?

Nov 20 '05 #11

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

Similar topics

3
8862
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...
7
6391
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...
15
3193
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...
5
2077
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
19
2620
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
49
14431
by: matty | last post by:
Hi, I recently got very confused (well that's my life) about the "undefined" value. I looked in the FAQ and didn't see anything about it. On...
20
6930
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
29
5056
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
0
2020
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1592
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.