[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

VBScript Anonymous Associative Arrays? (like in Javascript)

Posted on 2010-11-15
5
Medium Priority
?
862 Views
Last Modified: 2012-05-10
In Javascript, I can use a key-value association (associative arrays) as an argument in a function to make creating HTML tags easier.  For example,

function img(attr) {
    var prop, imgTag = document.createElement('img');
    for (prop in attr) {
        imgTag.setAttribute(prop, attr[prop]);
    }
    return imgTag;
}

Open in new window


And I can call it like this:
myElem.appendChild(img({
    src : "/images/img.jpg",
    alt : "Some alt text",
    class : "borderImage"
});

Open in new window


This way, I don't need to name each attribute of an image tag as an argument in the function. This is an oversimplified version of what I'm trying to do, and in fact it has nothing to do with HTML, but it's how I learned it in JS.

Is there a way to do something similar in VBScript?  I can create an associative array in VBScript like this:

Dim myArray
myArray("property1") = "value1"
myArray("property2") = "value2"
myArray("property3") = "value3"

Open in new window


But I don't know how to create an anonymous associative array like in Javascript. I need to be able to immediately plug it in as an argument in a function, without building a named array.  Any ideas?
0
Comment
Question by:jamesbcox1980
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 34139558
The closest analogue in VBScript would be a Dictionary object.

You may want to see this article about using Dictionaries; it was written from a VBA perspective, but the basics of the class apply as well to VBScript.
0
 
LVL 4

Author Comment

by:jamesbcox1980
ID: 34139973
I've seen the Dictionary before, but I'm not sure how this will help me.  Remember, my purpose here is for simplicity in formatting my code.  Using a dictionary, I'll still have to add each item to the dictionary individually, won't I?
0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 34140334
Yes, you would.

Like I said, it's the closest equivalent to an associative array in VBScript.  I did not say it was an exact match :)

Perhaps if you tried describing what you are actually doing I or another Expert can be more helpful.
0
 
LVL 4

Assisted Solution

by:jamesbcox1980
jamesbcox1980 earned 0 total points
ID: 34192319
I was able to figure out based on the Dictionary object what I needed to do. you were correct, it was a very simple fix.  I just had to be creative with how I passed the "associative array" to the function.

Function o(objArray)
        Dim param, val
        Set o = CreateObject("Scripting.Dictionary")

        For Each param In objArray
            val = Split(param, "::")
            If Ubound(val) = 1 Then
                o(val(0)) = val(1)
            End If
        Next
End Function

Open in new window


and I can use an anonymous array and this function to pass my parameters to a function

createNewFile(o(Array(_
    "type::ASCII", _
    "extension::txt", _
    "name::myTextFile", _
    "template::logFile" _
)))

Open in new window


It's not perfect but it works
0
 
LVL 4

Author Closing Comment

by:jamesbcox1980
ID: 34221301
Thanks a bunch.  See my half of the solution with the Scripting Dictionary in use.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question