views:

1065

answers:

6

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!

A: 

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.

Joel Coehoorn
+2  A: 

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.

John MacIntyre
John - You've exactly described my current solution. I'd just like to avoid the case statement which feels very redundant.
Cory House
+1  A: 

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.

TravisO
I'd agree that it's very easy to abuse, but calling it a 'hack' is a little strong. "Dynamic programming" is a legitimate discipline withing programming, it's just that many of us (myself included) prefer the safety of strong-typing and compiler checked code.
Joel Coehoorn
+2  A: 

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"))
AnthonyWJones
A: 

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
%>
Greg Ogle
+1  A: 

Use the "Execute" statement in ASP/VBScript.

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