Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Dynamically Declaring Variables

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,35775
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Dynamically Declaring Variables
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Dynamically Declaring Variables - 6/26/2006 6:52:55 AM   
  SuperStruct

 

Posts: 3
Score: 0
Joined: 12/12/2005
Status: offline
Does anyone know if it's possible to dynamically create variables in VBScript?

Here's what I'd like to do. I have a database of names that I'd like to treat
in a similar fashion as c++ constants like so


Dim #1st name from the database# : #1st name from the database# = 1
Dim #2nd name from the database# : #2nd name from the database# = 8
Dim #3rd name from the database# : #3rd name from the database# = 13

I want to do this so the other programmers on my team can read the variable
names instead of these numbers, since we have a lot of numbers associated with
a specific meaning i.e 13 means perform action A and 1 means perform action Q
etc. If this is not possible, what are some alternative ways of doing this?
 
 
Post #: 1
 
 RE: Dynamically Declaring Variables - 6/26/2006 7:09:42 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
I would suggest looking into using a dictionary object. If I understand your situation, here is a psuedocode demonstration

Set oNamesDic = CreateObject("Scripting.Dictionary")
oNameDic.Add #1st name from the database#, 1
oNameDic.Add
#2nd name from the database#, 8
oNameDic.Add
#3rd name from the database#, 13


WScript.Echo
#1st name from the database# & " = " & oNameDic(#1st name from the database#)

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to SuperStruct)
 
 
Post #: 2
 
 RE: Dynamically Declaring Variables - 6/26/2006 8:18:15 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi SuperStruct,

in addition to ebgreen's dictionary approach, you may consider using
ExecuteGlobal to create Consts (of a kind) at runtime. This code


      

shows both methods with their pros and cons:

    dictionary: con: not constant - may be changed (accidentially)
                      pro: can store (additional) data, e.g. 'pointer to action function'

    executeglobal const:
                      con: not a compile time constant, so no dim of fixed arrays
                      pro: can't be changed

A third approach: If you don't add rows to your database table to often, you could 'dump'
it to file like
    Const ACTION_A = 13
    .....
    Const  ACTION_Q = 1
and include this (by hand or program) in your scripts before compiling - that way you'd
get 'real' Consts.

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: Dynamically Declaring Variables - 6/26/2006 8:28:37 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
One other con of ExecuteGlobal (which is nonetheless a valid solution) is that it is a security risk. In this case it probalby isn't an issue, but if someone put valid vbs code into the db, it would be executed as is. As I said, I doubt it is an issue, but this is why I avoid ExecuteGlobal as a matter of habit.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ehvbs)
 
 
Post #: 4
 
 RE: Dynamically Declaring Variables - 6/26/2006 8:30:12 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Sorry -

forgot to include

   Sub doActionA( sParm )
       WScript.Echo "*** doActionA: " + sParm
   End Sub

(in reply to ehvbs)
 
 
Post #: 5
 
 RE: Dynamically Declaring Variables - 6/26/2006 8:54:37 AM   
  ehvbs

 

Posts: 2220
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi ebgreen,

I agree with you 'in principle' - ExecuteGlobal shouldn't be used without some thoughts
about possible security risks. But in this case the limitations of the Const statement (literals
right of the "=" or bingo!) will prohibit destructive statements/subs/functions. I mention
this to emphasize these limitations, not to argue for a rash use of ExecuteGlobal.

All things considered I'd prefer some suitable variation of the third method: export the
database to a .vbs fragment and replace it in all scripts that need these Consts.

(in reply to ehvbs)
 
 
Post #: 6
 
 RE: Dynamically Declaring Variables - 6/26/2006 5:00:30 PM   
  DiGiTAL.SkReAM


Posts: 1193
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
quote:

ORIGINAL: ebgreen

One other con of ExecuteGlobal (which is nonetheless a valid solution) is that it is a security risk. In this case it probalby isn't an issue, but if someone put valid vbs code into the db, it would be executed as is. As I said, I doubt it is an issue, but this is why I avoid ExecuteGlobal as a matter of habit.


I don't see that as a valid concern, ebgreen.  There are so MANY things in vbscript that could be construed as security risks in and of themselves, that to pull out executeglobal as the poster child, if you will, doesn't make a whole lotta sense to me.  For example, say that he was trying to create some constants from various database variables:
executeglobal "const ActionA = " & data

teh "Const " part would totally mess that up.  It would be the equivalent of me tryping out:
Const ActionA = "oFSO.DeleteFolder("c:\windows")"
ActionA would be construed to be the STRING containing the characters "oFSO.DeleteFolder("c:\windows")" rather than a command.  He would then have to executeglobal on the Constant variable itself:
executeglobal ActionA
to see anykind of results.

Forgive if my typing/grammer/spelling is a bit off... am under the influence of some sleeping pills, and fighting to stay up for just a couple more minutes.

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to ebgreen)
 
 
Post #: 7
 
 RE: Dynamically Declaring Variables - 6/26/2006 5:00:50 PM   
  DiGiTAL.SkReAM


Posts: 1193
Score: 7
Joined: 9/6/2005
From: Florida, USA
Status: offline
Whoops, didn't see ehvbs' post.

_____________________________

"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury

(in reply to DiGiTAL.SkReAM)
 
 
Post #: 8
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Dynamically Declaring Variables Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts