473,765 Members | 2,001 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_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
10 2255
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 DemonstrationCl ass
'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.Fiel dInfo =
Me.GetType.GetF ield(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(TargetFie ldInfo.GetValue (Me), Integer)
'Calculate the new value.
NewValue = OldValue * Multiplier
'And then reassign the new value to our instance of
DemonstrationCl ass.
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.Wri teLine("Before AlterVar")
Console.Out.Wri teLine("FirstVa r: " + FirstVar.ToStri ng)
Console.Out.Wri teLine("SecondV ar: " + SecondVar.ToStr ing)
Console.Out.Wri teLine("ThirdVa r: " + ThirdVar.ToStri ng)
Console.Out.Wri teLine()

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

'Call our routine.
AlterVar(tmpMul tiplier, TargetName)

'And redisplay the state.
Console.Out.Wri teLine("After AlterVar")
Console.Out.Wri teLine("FirstVa r: " + FirstVar.ToStri ng)
Console.Out.Wri teLine("SecondV ar: " + SecondVar.ToStr ing)
Console.Out.Wri teLine("ThirdVa r: " + ThirdVar.ToStri ng)
Console.Out.Wri teLine()

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

Public Shared Sub Main()
Dim MyDemoClass As New DemonstrationCl ass
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****@discuss ions.microsoft. com> wrote in message
news:EE******** *************** ***********@mic rosoft.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
8899
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: <?php echo $_SERVER; ?>
7
6421
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
3248
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? <script language="javascript">
5
2100
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 object, is still created on the stack, I would say, so I don't understand this inline business. Apart from the fact that it is my understanding that "inline" as it exists in C++ doesn't exist in C#. Could someone please shed some light on this...
19
2658
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 = Color.Empty then..... or if mycolor is Color.Empty then .......
49
14520
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 http://www.webreference.com/programming/javascript/gr/column9/ they say: <snip> The undefined property A relatively recent addition to JavaScript is the undefined property.
20
7000
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 = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
29
5115
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: 65,535. But i'm trying to write a program to test this, assuming I didn't know this number in advance. I came up with the following but have two questions. Maybe someone can help? using System; using System.Collections.Generic; using System.Text;
0
2048
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. Ok my problem is the following. I have a class that contains a "MakeByteArray" function. I have many objects of that class. Inside that function, I have a private variable, that is NOT static. It seems that when I put all these objects in...
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9398
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8831
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7378
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
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 we have to send another system

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.