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

发布于 2024-07-09 03:12:15 字数 501 浏览 7 评论 0原文

例如,我如何运行下面的 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")

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

如果您有更好的设置建议,我会洗耳恭听!

For example, how can I run me.test below?

myvar = 'test'
me.myvar

ASP looks for the method "myvar" and doesn't find it. In PHP I could simply say $me->$myvar but ASP's syntax doesn't distinguish between variables and methods. Suggestions?

Closely related to this, is there a method_exists function in ASP Classic?

Thanks in advance!

EDIT: I'm writing a validation class and would like to call a list of methods via a pipe delimited string.

So for example, to validate a name field, I'd call:

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

I like the idea of having a single line that shows all the ways a given field is being validated. And each pipe delimited section of the string is the name of a method.

If you have suggestions for a better setup, I'm all ears!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不疑不惑不回忆 2024-07-16 03:12:16

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

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

Dim myvar : myvar = "Test"
Dim x : Set x = GetRef(myvar)
Response.Write x("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"))

You can achieve this in VBScript by using the GetRef function:-

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

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

Will send "Thing has been tested" to the client.

So here is your validate requirement using 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

Having provided this I have to say though that if you are familiar with PHP then JScript would be a better choice on the server. In Javascript you can call a method like this:-

function test(val) { return val + " has been tested"; )
var myvar = "test"
Response.Write(this[myvar]("Thing"))
[旋木] 2024-07-16 03:12:16

如果您谈论的是 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 页面中对此进行测试,但我认为它已经足够接近了,只需稍加调整即可工作。

If you are talking about VBScript, it doesn't have that kind of functionality. (at least not to my knowledge) I might attempt it like this :

Select myvar
   case "test":
      test

   case "anotherSub":
      anotherSub

   else
      defaultSub

end select

It's been a while since I wrote VBScript (thank god), so I'm not sure how good my syntax is.

EDIT-Another strategy

Personally, I would do the above, for security reasons. But if you absolutely do not like it, then you may want to try using different languages on your page. I have in the past used both Javascript AND VBScript on my Classic ASP pages (both server side), and was able to call functions declared in the other language from my current language. This came in especially handy when I wanted to do something with Regular Expressions, but was in VBScript.

You can try something like

<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>

I didn't test this in a classic ASP page, but I think it's close enough that it will work with minor tweaks.

甜警司 2024-07-16 03:12:16

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

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

Use the "Execute" statement in ASP/VBScript.

Execute "Response.Write ""hello world"""
瀞厅☆埖开 2024-07-16 03:12:16

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

仅仅因为你能做某事,并不意味着它就是正确的或一个好主意。

PHP's ability to dynamically call or create functions are hacks that lead to poor programming practices. You need to explain what you're trying to accomplish (not how) and learn the correct way to code.

Just because you can do something, doesn't make it right or a good idea.

揪着可爱 2024-07-16 03:12:16

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

ASP does not support late binding in this manner. What are you trying to do, in a larger sense? Explain that, and someone can show you how to accomplish it in asp.

骄兵必败 2024-07-16 03:12:16

此外,您可能会考虑“对象化”验证功能。 在 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
%> 

Additionally, you might consider "objectifying" the validation functionality. Making classes is possible (though not widely used) in VB Script.

<%
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
%> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文