Hey,

I've looked every place I can think of ... is there a way to test to see
if a Sub or a Function has been defined? I'm working in ASP classic 3.0.

I need to do an IF statement and do something if something has been
defined otherwise ... not ...

Thanks for any pointers,

:rob

Re: function/sub isDefined? by McKirahan

McKirahan
Tue Sep 14 13:32:28 CDT 2004

"rob" <rob@pleaseremovespamcherny.com> wrote in message
news:R6mdnUaBEPHtrNrcRVn-rg@giganews.com...
>
> Hey,
>
> I've looked every place I can think of ... is there a way to test to see
> if a Sub or a Function has been defined? I'm working in ASP classic 3.0.
>
> I need to do an IF statement and do something if something has been
> defined otherwise ... not ...
>
> Thanks for any pointers,
>
> :rob
>

Will this help?

Option Explicit
Const cVBS = "sub_func.vbs"
Call Sub1()
On Error Resume Next
Call Sub2()
MsgBox Err.Number & " : " & Err.Description

Sub Sub1()
End Sub



Re: function/sub isDefined? by Christoph

Christoph
Tue Sep 14 15:22:55 CDT 2004

14.09.2004 19:59, rob schrieb:
> Hey,
>
> I've looked every place I can think of ... is there a way to test to see
> if a Sub or a Function has been defined? I'm working in ASP classic 3.0.
>
> I need to do an IF statement and do something if something has been
> defined otherwise ... not ...

You can try to use it as a L-Value (assign sth to it). If you get err 501
then it's a sub, function, const, class or internal vbs-function
If you then try to GetRef it and get err 0 then its a defined sub or
function (not a const, class or internal)
This way you *don't* *execute* the putative Sub/Function
Which is the case if u use Call or Typename.

Option Explicit
Dim b, Name

Dim dummy : dummy = 0
Const c = "c"
Class k : end class
Sub s : End Sub
Function f : f = 0 : End Function

For each Name in Array ("dummy", "err", "true", "null", "c", "k", _
"s", "f", "if", "isDefinedProc", "class", "WScript", "Now", "me")

b = isDefinedProc (Name)
If b Then
MsgBox "'" & Name & "' IS a defined sub or function"
Else
MsgBox "'" & Name & "' is NOT a defined sub or function"
End If
Next

Function isDefinedProc (byval procName)
On error resume next
ExecuteGlobal "Option Explicit:" & procName & " = 1"
If err.number = 501 Then
err.clear : Dim foo
Set foo = GetRef(procName)
If err.number = 0 Then
isDefinedProc = true
Exit Function
End If
End If
isDefinedProc = false
End Function


--
Gruesse, Christoph

Rio Riay Riayo - Gordon Sumner, 1979

Re: function/sub isDefined? by Michael

Michael
Tue Sep 14 22:25:32 CDT 2004

rob wrote:
> Hey,
>
> I've looked every place I can think of ... is there a way to test to
> see if a Sub or a Function has been defined? I'm working in ASP
> classic 3.0.
>
> I need to do an IF statement and do something if something has been
> defined otherwise ... not ...



msgbox IsProcedureDefined("foo")
msgbox IsProcedureDefined("bar")

function IsProcedureDefined(sName)

IsProcedureDefined = false
on error resume next
set oRef = getref(sName)
if err then exit function
IsProcedureDefined = true

end function

sub foo()
end sub

--
Michael Harris
Microsoft.MVP.Scripting
Sammamish WA US

Re: function/sub isDefined? by Al

Al
Tue Sep 14 23:01:23 CDT 2004


"McKirahan" <News@McKirahan.com> wrote in message
news:0PG1d.193089$mD.119124@attbi_s02...
> "rob" <rob@pleaseremovespamcherny.com> wrote in message
> news:R6mdnUaBEPHtrNrcRVn-rg@giganews.com...
> >
> > Hey,
> >
> > I've looked every place I can think of ... is there a way to test to see
> > if a Sub or a Function has been defined? I'm working in ASP classic 3.0.
> >
> > I need to do an IF statement and do something if something has been
> > defined otherwise ... not ...
> >
> > Thanks for any pointers,
> >
> > :rob
> >
>
> Will this help?
>
> Option Explicit
> Const cVBS = "sub_func.vbs"
> Call Sub1()
> On Error Resume Next
> Call Sub2()
> MsgBox Err.Number & " : " & Err.Description
>
> Sub Sub1()
> End Sub

I see a few practical issues with this approach:

- what if sub2 exists but has a parameter defined?
- what if sub2 exists, but performs some actions that you might otherwise
not want to be carried out?

/Al



Re: function/sub isDefined? by McKirahan

McKirahan
Wed Sep 15 05:10:38 CDT 2004

"Al Dunbar [MS-MVP]" <alan-no-drub-spam@hotmail.com> wrote in message
news:e70XsitmEHA.4024@TK2MSFTNGP12.phx.gbl...
>
> "McKirahan" <News@McKirahan.com> wrote in message
> news:0PG1d.193089$mD.119124@attbi_s02...
> > "rob" <rob@pleaseremovespamcherny.com> wrote in message
> > news:R6mdnUaBEPHtrNrcRVn-rg@giganews.com...
> > >
> > > Hey,
> > >
> > > I've looked every place I can think of ... is there a way to test to
see
> > > if a Sub or a Function has been defined? I'm working in ASP classic
3.0.
> > >
> > > I need to do an IF statement and do something if something has been
> > > defined otherwise ... not ...
> > >
> > > Thanks for any pointers,
> > >
> > > :rob
> > >
> >
> > Will this help?
> >
> > Option Explicit
> > Const cVBS = "sub_func.vbs"
> > Call Sub1()
> > On Error Resume Next
> > Call Sub2()
> > MsgBox Err.Number & " : " & Err.Description
> >
> > Sub Sub1()
> > End Sub
>
> I see a few practical issues with this approach:
>
> - what if sub2 exists but has a parameter defined?
> - what if sub2 exists, but performs some actions that you might otherwise
> not want to be carried out?
>
> /Al
>
Valid points but the OP asked "is there a way to test to see if a Sub or a
Function has been defined? ".



Re: function/sub isDefined? by Christoph

Christoph
Wed Sep 15 06:14:44 CDT 2004

14.09.2004 22:22, Christoph Basedau schrieb:
> 14.09.2004 19:59, rob schrieb:

>> I need to do an IF statement and do something if something has been
>> defined otherwise ... not ...
>
> You can try to use it as a L-Value (assign sth to it). If you get err 501
> then it's a sub, function, const, class or internal vbs-function

[Correcting myself]

That L-Value thing is only necessary, if you want find out if a name
is a variable, a constant, a 'normal' variable, a classname or undefined.
If you simply want to check if a name refers to a defined sub/function
then testing GetRef is sufficient (see M. Harris post)


q "q"
q "p"

Function isDefinedProc (procName)
On error resume next
With GetRef(procName) : End With
isDefinedProc = 0 = err.number
End Function

sub q(s):MsgBox s & " == Procedure ? " & isDefinedProc (s):end sub


--
Gruesse, Christoph

Rio Riay Riayo - Gordon Sumner, 1979

Re: function/sub isDefined? by Al

Al
Wed Sep 15 20:16:26 CDT 2004


"McKirahan" <News@McKirahan.com> wrote in message
news:yyU1d.180918$9d6.98858@attbi_s54...
> "Al Dunbar [MS-MVP]" <alan-no-drub-spam@hotmail.com> wrote in message
> news:e70XsitmEHA.4024@TK2MSFTNGP12.phx.gbl...
> >
> > "McKirahan" <News@McKirahan.com> wrote in message
> > news:0PG1d.193089$mD.119124@attbi_s02...
> > > "rob" <rob@pleaseremovespamcherny.com> wrote in message
> > > news:R6mdnUaBEPHtrNrcRVn-rg@giganews.com...
> > > >
> > > > Hey,
> > > >
> > > > I've looked every place I can think of ... is there a way to test to
> see
> > > > if a Sub or a Function has been defined? I'm working in ASP classic
> 3.0.
> > > >
> > > > I need to do an IF statement and do something if something has been
> > > > defined otherwise ... not ...
> > > >
> > > > Thanks for any pointers,
> > > >
> > > > :rob
> > > >
> > >
> > > Will this help?
> > >
> > > Option Explicit
> > > Const cVBS = "sub_func.vbs"
> > > Call Sub1()
> > > On Error Resume Next
> > > Call Sub2()
> > > MsgBox Err.Number & " : " & Err.Description
> > >
> > > Sub Sub1()
> > > End Sub
> >
> > I see a few practical issues with this approach:
> >
> > - what if sub2 exists but has a parameter defined?
> > - what if sub2 exists, but performs some actions that you might
otherwise
> > not want to be carried out?
> >
> > /Al
> >
> Valid points but the OP asked "is there a way to test to see if a Sub or
a
> Function has been defined? ".

Granted. I just hope he doesn't ask "is there a way to test to see if this
pistol is loaded", and assume that all of the answers are practical! :-)

/Al



Re: function/sub isDefined? by rob

rob
Thu Sep 16 09:48:25 CDT 2004


Thanks for all the ideas fellas!!