如何从ASP Classic中的变量调用方法?

时间:2008-12-02 22:14:05

标签: asp-classic vbscript late-binding

例如,我如何在下面运行me.test?

myvar = 'test'
me.myvar

ASP查找方法“myvar”但找不到它。在PHP中我可以简单地说$ me-> $ myvar,但ASP的语法不区分变量和方法。建议?

与此密切相关,ASP Classic中是否有method_exists函数?

提前致谢!

编辑:我正在编写验证类,并希望通过管道分隔字符串调用方法列表。

例如,要验证名称字段,我会调用:

validate("required|min_length(3)|max_length(100)|alphanumeric")

我喜欢有一条线显示给定字段的所有验证方式。字符串的每个管道分隔部分都是方法的名称。

如果您有更好的设置建议,我会全力以赴!

6 个答案:

答案 0 :(得分:6)

您可以使用GetRef函数在VBScript中实现此目的: -

Function Test(val)
  Test = val & " has been tested"
End Function

Dim myvar : myvar = "Test"
Dim x : Set x = GetRef(myvar)
Response.Write x("Thing")

将向客户发送“Thing已经过测试”。

所以这是使用GetRef的验证要求: -

validate("Hello World", "min_length(3)|max_length(10)|alphanumeric")


Function required(val)
    required = val <> Empty
End Function


Function min_length(val, params)
    min_length = Len(val) >= CInt(params(0))
End Function


Function max_length(val, params)
    max_length = Len(val) <= CInt(params(0))
End Function


Function alphanumeric(val)
    Dim rgx : Set rgx = New RegExp
    rgx.Pattern = "^[A-Za-z0-9]+$"
    alphanumeric = rgx.Test(val)
End Function


Function validate(val, criterion)

    Dim arrCriterion : arrCriterion = Split(criterion, "|")
    Dim criteria

    validate = True

    For Each criteria in arrCriterion

        Dim paramListPos : paramListPos = InStr(criteria, "(")

        If paramListPos = 0 Then
            validate = GetRef(criteria)(val)
        Else
            Dim paramList
            paramList = Split(Mid(criteria, paramListPos + 1, Len(criteria) - paramListPos - 1), ",")
            criteria = Left(criteria, paramListPos - 1)
            validate = GetRef(criteria)(val, paramList)
        End If
        If Not validate Then Exit For
    Next

End Function

提供了这个我不得不说,如果你熟悉PHP,那么JScript将是服务器上更好的选择。在Javascript中,您可以调用这样的方法: -

function test(val) { return val + " has been tested"; )
var myvar = "test"
Response.Write(this[myvar]("Thing"))

答案 1 :(得分:2)

如果您正在谈论VBScript,它没有这种功能。 (至少我不知道)我可能会这样尝试:

Select myvar
   case "test":
      test

   case "anotherSub":
      anotherSub

   else
      defaultSub

end select

我写VBScript(感谢上帝)已经有一段时间了,所以我不确定我的语法有多好。

编辑 - 另一种策略

就个人而言,出于安全考虑,我会这样做。但如果你绝对不喜欢它,那么你可能想尝试在你的页面上使用不同的语言。我过去在经典ASP页面(服务器端)上都使用了Javascript和VBScript,并且能够用我当前的语言调用另一种语言声明的函数。当我想用正则表达式做一些事情时,这特别方便,但是在VBScript中。

您可以尝试类似

的内容
<script language="vbscript" runat="server">
    MyJavascriptEval myvar
</script>
<script language="javascript" runat="server">
    function MyJavascriptEval( myExpression)
    {
        eval(myExpression);
    }

    /* OR
    function MyJavascriptEval( myExpression)
    {
        var f = new Function(myExpression);
        f();
    }
    */
</script>

我没有在经典的ASP页面中对此进行测试,但我认为它足够接近它可以进行微调。

答案 2 :(得分:1)

PHP动态调用或创建函数的能力是导致编程实践不佳的黑客行为。你需要解释你想要完成的事情(而不是如何)并学习正确的编码方法。

仅仅因为你可以做某事,不能使它成为正确或好主意。

答案 3 :(得分:1)

在ASP / VBScript中使用“Execute”语句。

Execute "Response.Write ""hello world"""

答案 4 :(得分:0)

ASP不支持这种方式的后期绑定。从更广泛的意义上说,你想做什么?解释一下,有人可以告诉你如何在asp中完成它。

答案 5 :(得分:0)

此外,您可能会考虑“客观化”验证功能。在VB脚本中创建类是可能的(尽管没有广泛使用)。

<%
Class User
' declare private class variable
Private m_userName

' declare the property
Public Property Get UserName
  UserName = m_userName
End Property
Public Property Let UserName (strUserName)
  m_userName = strUserName
End Property

' declare and define the method
Sub DisplayUserName
  Response.Write UserName
End Sub

End Class
%> 
相关问题