开发者

VBScript Anonymous Associative Arrays? (like in Javascript)

Those of you proficient in Javascript and PHP know how to use the Object constructor to reference basically what amounts to an anonymous associative array like this:

myFunction({
    "param1" : "value1",
    "param2" : "value2"
});

The benefit is not having to name each parameter of your target function and being able to set defaults. Does anyone know how to build statements similarly in VBScript? I was looking into the Dictionary class, but I don't 开发者_运维问答think I'll have a firm grasp on how to use that to my advantage here until I see an example.

Thanks,


The Dictionary object is exactly what you're looking for. I've used it successfully for multi-language skinning of sites. It's not difficult to use.

see: http://www.devguru.com/technologies/vbscript/13992.asp


You were right, it wasn't difficult to use. It was just a matter of getting creative with a normal array. Here's what I did:

<%
    Function Img(aParamArray)
        Dim oImageTag,aImageTagKeys, val, param, key, output
        Set oImageTag = CreateObject("Scripting.Dictionary")

        For Each param In aParamArray
            val = Split(param, "::")
            If Ubound(val) = 1 Then
                oImageTag(val(0)) = val(1)
            End If
        Next

        aImageTagKeys = oImageTag.Keys
        Img = "<img "
        For Each key in aImageTagKeys
            If oImageTag(key) <> "" Then
                Img = Img & key & "=""" & oImageTag(key) & """ "
            End If
        Next

        If iDocType = 0 Or iDocType = 1 Or iDocType = 6 Then
            Img = Img & ">"
        Else
            Img = Img & "/>"
        End If
    End Function
%>

--OR I can set defaults and only output supported attributes--

<%
Function Img(aParamArray)
    Dim oImageTag,aImageTagKeys, val, param, key, output
    Set oImageTag = CreateObject("Scripting.Dictionary")
    oImageTag("src") = ""
    oImageTag("alt") = ""
    oImageTag("class") = ""
    oImageTag("id") = ""
    oImageTag("width") = ""
    oImageTag("height") = ""
    oImageTag("usemap") = ""
    oImageTag("title") = ""
    oImageTag("style") = ""
    oImageTag("dir") = ""
    oImageTag("lang") = ""
    oImageTag("ismap") = ""
    oImageTag("onabort") = ""
    oImageTag("onclick") = ""
    oImageTag("ondblclick") = ""
    oImageTag("onmousedown") = ""
    oImageTag("onmouseout") = ""
    oImageTag("onmouseover") = ""
    oImageTag("onmouseup") = ""
    oImageTag("onkeydown") = ""
    oImageTag("onkeypress") = ""
    oImageTag("onkeyup") = ""

    For Each param In aParamArray
        val = Split(param, "::")
        If Ubound(val) = 1 Then
            If oImageTag.Exists(val(0)) Then
                oImageTag(val(0)) = val(1)
            End If
        End If
    Next

    aImageTagKeys = oImageTag.Keys
    Img = "<img "
    For Each key in aImageTagKeys
        If oImageTag(key) <> "" Then
            Img = Img & key & "=""" & oImageTag(key) & """ "
        End If
    Next

    If iDocType = 0 Or iDocType = 1 Or iDocType = 6 Then
        Img = Img & ">"
    Else
        Img = Img & "/>"
    End If
End Function
%>

And call it like this:

<% =Img(Array(_
                "src::http://www.domain.com/img.jpg",_
                "alt::Some alt text",_
                "width::30",_
                "height::30",_
                "class::noborder"_
)) %>

Now, I can easily control the output of image tags no matter what the doctype, and outputting an image from an SQL server will be much easier now that I can make a Pseudo-associative array out of a normally indexed one.

The point to figuring this out wasn't to make image tags, it was actually to build entire views of data and forms based on several factors and data from an SQL server, but I needed to simplify it to figure it out. Now it's working perfectly.

Thanks for your suggestion!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜