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!

Dynamically build and execute code

Status
Not open for further replies.

titusone

Programmer
Oct 13, 2000
7
0
0
US
I would like to be able to build code on the fly and execute it (somewhat like you are able to build a SQL statement as a string and then execute it).

For instance, let's say I have the following:

strFieldName = "Quantity"
strTransactionType = "Sold"

I want to use the strFieldName and strTransactionType to help build the full name of another variable on the fly. The code I want to execute is:

dblQuantitySold = 5

So I'm looking for a way to build the variable name dblQuantitySold using something like:
strExecute = "dbl" + strFieldName+strTransactionType + " = 5"

and then some command that allows me to execute the code.

Any ideas?

The Rookie [sig]<p>John B<br><a href=mailto:dad2mads@home.com>dad2mads@home.com</a><br>[/sig]
 
Hi John,

NO, atleast not that i know of, however a select case statement or an if elseif construct with the required code to perform the required task will do the job.

something like

select case TransType

case = &quot;Sold&quot;
DoSoldTransaction (ItemQty)
case = &quot;Buy&quot;
DoBuyTransaction (ItemQty)
case Else
' Error trap neither Sold or Buy
YourErrorFunction

end select

Or

if TransType = &quot;Sold&quot; then
DoSoldTransaction (ItemQty)
elseif TransType = &quot;Buy&quot; then
DoBuyTransaction (ItemQty)
else
' Error trap neither Sold or Buy
YourErrorFunction
end if

where
DoSoldTransaction (ItemQty)
DoBuyTransaction (ItemQty)
YourErrorFunction

are functions to handle the transactions as required you could of course have your code in place of the &quot;Functions&quot;.

variable types need to be known ahead if being used,
the closest i can think of is C++ where a class type or variable type can be assigned at runtime but you still need a pointer of var type to hold the variable or or of base class to hold a class object

HTH
[sig]<p>Robert Dwyer<br><a href=mailto:rdwyer@orion-online.com.au>rdwyer@orion-online.com.au</a><br>[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top