Google Answers Logo
View Question
 
Q: ASP function object ( Answered 5 out of 5 stars,   2 Comments )
Question  
Subject: ASP function object
Category: Computers
Asked by: jmt-ga
List Price: $25.00
Posted: 14 Jun 2004 21:58 PDT
Expires: 14 Jul 2004 21:58 PDT
Question ID: 361245
VB_UserMemId "asp" OR "active OR server OR pages" -.NET
Using ASP, I need to know how to create a default function in a
vbscript class object.
ie:
'vbscript
class x
  dim rs
  function class_initialize
    set rs = CreateObject("scripting.dictionary")
    rs("stuff") = "xyz"
  end function

  public default function Item(x)
     attribute x.VB_UserMemId = 0
     Item=rs(x)
   end function

set obj = new x
y = obj("stuff")
'y should equal "xyz"

Thanks for the help
Answer  
Subject: Re: ASP function object
Answered By: tox-ga on 14 Jun 2004 22:33 PDT
Rated:5 out of 5 stars
 
jmt-ga,

Before I start, if you have any questions regarding my answer, please
feel free to ask for clarifications before your rate me. I am willing
to work until you are completely satisfied.

To do this in ASP using VBScript:

<%
class x
  'declares a public variable rs
  dim rs
  
  'Class_Initialize is the constructor function name for ASP VBScript
  private sub Class_Initialize()
    set rs = CreateObject("scripting.dictionary")
    rs("stuff") = "xyz"
  end sub

end class

'creates an x object, setting x.rs("stuff") to "xyz"
set obj = new x

'dot notation is used in vbscript to access variables
y = obj.rs("stuff")
response.write(y)
%>

For more information regarding ASP functions and classes, see:
http://www.sloppycode.net/asp-components/tutorial/?page=3
http://www.devguru.com/Technologies/vbscript/quickref/Class.html

Cheers,
Tox-ga

Clarification of Answer by tox-ga on 14 Jun 2004 23:00 PDT
jmt-ga,

Sorry about that, please ignore the code above. I accidentally left
out a function. The following is the working code, with the default
function. Feel free to ask for clarifications if you don't understand
how it works.

<%
class x
  'declares a public variable rs
  dim rs

  'Class_Initialize is the constructor for VBScript
  private sub Class_Initialize()

    'creates the rs dictionary object
    set rs = CreateObject("scripting.dictionary")

    'initializes the rs("stuff") variable
    rs("stuff") = "xyz"

  end sub

  'the "default" keyword indicates that this function is default
  'the "test" word is the function name
  public default function test (inputstring)

    'returning things in VBScript is done by equating the function
    'name to whatever you wish to return
    test = rs(inputstring)

  end function

end class

set obj = new x
y = obj("stuff")

'prints "xyz"
response.write(y)
%>

Cheers,
Tox-ga

Request for Answer Clarification by jmt-ga on 14 Jun 2004 23:12 PDT
Sorry, but this isn't what I'm looking for.
I've been writing code for a decade now, and I've used vb and vbscript
quite a lot in the past several years.  I've tried many times to find
the answer, so I don't expect it to be easy to find.

>> 'dot notation is used in vbscript to access variables
>> y = obj.rs("stuff")
I know this, but it is what I'm trying to avoid.  In C++, an object
may be called as a function, this makes it a function object. 
Optionally, a parameter may be passed in.  In VB, an advanced option
may be set in a class object that causes the function to serve as the
default, but it doesn't seem to work in vbscript.

The underlying question may be phrased as, 'how do you set function
attributes in vbscript?'.  In particular, I need to set the following
attribute in class x:
     attribute x.VB_UserMemId = 0
The goal is to make x into a function object, so that I can call
x("stuff") and invoke the specified function to index into whatever
embedded or inherited list the object uses.


Thanks,
-Jmt
JackMThompson.com

Clarification of Answer by tox-ga on 14 Jun 2004 23:14 PDT
Sorry about that, I actually fixed the code if you look at my
clarification above. The way to do default functions is there.
Apologies again for the confusion.

Cheers,
tox-ga

Clarification of Answer by tox-ga on 14 Jun 2004 23:20 PDT
jmt-ga,

This is a copy of the clarification from above. If you have anymore
questions, feel free to ask.

<%
class x
  'declares a public variable rs
  dim rs

  'Class_Initialize is the constructor for VBScript
  private sub Class_Initialize()

    'creates the rs dictionary object
    set rs = CreateObject("scripting.dictionary")

    'initializes the rs("stuff") variable
    rs("stuff") = "xyz"

  end sub

  'the "default" keyword indicates that this function is default
  'the "test" word is the function name
  public default function test (inputstring)

    'returning things in VBScript is done by equating the function
    'name to whatever you wish to return
    test = rs(inputstring)

  end function

end class

set obj = new x
y = obj("stuff")

'prints "xyz"
response.write(y)
%>

Cheers,
Tox-ga

Clarification of Answer by tox-ga on 14 Jun 2004 23:48 PDT
jmt-ga,
If the line-by-line comments before seemed belittling or insulting, I
did not mean any offense. I merely wished to be clear. As you know,
VBScript and VB, while similar, have important differences
(http://www.hackerscenter.com/KnowledgeArea/guides/books/vbscript1/ch13fi.htm).
As a scripting language, VBScript has much less functionality when
compared to VB.

One of these key differences is the manner in which functions are
declared. The VBScript sytax for function declaration is
http://17.webmasters.com/caspdoc/html/vbscript_function_statement.htm.
However, even this extensive documentation leaves out that the way to
make a function default for the class is to have the "default" keyword
in the function header.

VB_UserMemId is actually a legal identifier in VBScript, while it is
not in VB, indicating that it serves no particular purpose in the
scripting form of the langauge.
(http://weblogs.asp.net/ericlippert/archive/2004/06/10/152831.aspx)

The default key word is the way to go.

I hope this clears things up for you.

Cheers,
tox-ga
jmt-ga rated this answer:5 out of 5 stars and gave an additional tip of: $5.00
It took a clarification to get the right answer, which was to be
expected.  The explanation was clear and consise.  The researcher did
the job exceedingly well and was well worth the cost.  Thank you.

Comments  
Subject: Re: ASP function object
From: ericlippert-ga on 16 Jun 2004 17:39 PDT
 
Writing a default function works, as you've seen.  However I suspect
that what you want is not a default FUNCTION at all.  It looks to me
like you want a default PROPERTY.

Class Foo
  Private InternalItem ' UNDONE: use dictionary trick, as above, if desired.
  Public Default Property Get Item(Index)
    Item = InternalItem
  End Property
  Public Property Let Item(Index, Value)
    InternalItem = Value
  End Property
End Class
Set F = New Foo
F("blah") = 123
print F("blah")

Does that make sense?

The documentation for the VBScript function syntax is here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vsstmfunction.asp

And you can find the syntax for the sub/property get/let/set statements here

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/vtoriStatements.asp
Subject: Re: ASP function object
From: jmt-ga on 16 Jun 2004 19:56 PDT
 
You're right.  I had already tried that but it didn't work.  I think
the problem was using 'Item' as the name of the property.

Here's how the class looks now...
	class RsHandler

		dim rshset
		dim blnEof
		dim All

		dim errormessages
		dim FieldReference
		dim Errored
		dim bOpen

		public function Errors
			if Errored then Errors = errormessages
		end function

		public property Get Ok
			if bOpen then 
				Ok = True
			else
				Ok = False
			end if
		end property

		public default property Get rsitem(name)
			rsitem = 0
			if not Ok then exit property
			if not CheckTag(name) then exit property
			rsitem = rshset (name)
		end property

		public property Let rsitem(name,val)
			if not Ok then exit property
			if not CheckTag(name) then exit property
			rshset (name) = val
		end property


		private sub Class_Initialize
			Errored = false
			bOpen = false
			errormessages = "Errors:<br>"&vbCrLf
			set FieldReference = CreateObject("scripting.dictionary")
			set All=Server.CreateObject("Scripting.Dictionary")
		end sub

		public function Exists(ExistName)
			Exists = FieldReference.Exists(ExistName)
		end function

		public sub Attach(rsin)
			FieldReference.RemoveAll
			set rshset = rsin
			x=0
			for each item in rshset.fields
				FieldReference(rshset.fields(x).name) = rshset.fields(x).type
				'response.write(rshset.fields(x).name &":"&
FieldReference(rshset.fields(x).name) &"<br>"&vbCrLf)
				x=x+1
			next
			bOpen = True
		end sub

		public function MoveNext
			if blnEof then 
				MoveNext = blnEof
				exit function
			end if
			MoveNext=rshset.MoveNext
			if rshset.eof then blnEof=true
		end function

	...
          end class

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy