Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Each x in VBScript Class - Is this possible?

Status
Not open for further replies.

billyrob

Programmer
Jul 13, 2004
3
GB
Hi All,

Been playing with Classes in VBScript and am now trying to develop a Class that will be used to Serialize objects.

What I want to do ibe able to loop through each property of an object and discover the value and name of each property.

I have tried the following but this is only a guess:

Code:
<%
  Class Test  [COLOR=green]' some class[/color] 

    Public msg1  [COLOR=green]' some properties[/color]
    Public msg2

    Private sub Class_Initialize()   [COLOR=green]' a method[/color]	
      msg1= "Hi"
      msg2= "By"
    End Sub

  End Class

  Set myTest = new Test   [COLOR=green]' an instance of the class[/color] 

  [COLOR=green]' loop through each property of the myTest object[/color]
  [COLOR=green]' and display the property ?[/color]
  For each x in myTest    
    Response.write x & "<br>"
  Next
%>

Can anyone help me on this one?
 
I dont think you can actually do it that way, you may find it works if you create a public array and loop though that, for example

<%
Class Test ' some class

Public messages(2)

Private sub Class_Initialize() ' a method
messages(0)= "Hi"
messages(1)= "By"
End Sub

End Class

Set myTest = new Test ' an instance of the class

' loop through each property of the myTest object
' and display the property ?
For each x in myTest.messages
Response.write x & "<br>"
Next
%>
 
Thanks for getting back to me schatch.

I know about this method of iterating through the collection of an array, but what I wanted to do was the VBScript equivealent of:

Code:
<script language="JScript">

[COLOR=green]// constructor function[/color]
function JStest() {
	this.msg1 = "Hi";
	this.msg2 = "Bye";

	this.getMsg1 = function() {
		return this.msg1;
	}
	this.getMsg2 = function() {
		return this.msg2;
	}
}

var myTest = new JStest();

for (x in myTest) { [COLOR=green]// loop through object properties[/color]
	this.document.writeln ( x + " = " + myTest[x] + "<br>" ) 
}

</script>

which results in the following output to screen:

[tt]msg1 = Hi
msg2 = Bye
getMsg1 = function() { return this.msg1; }
getMsg2 = function() { return this.msg2; }[/tt]

Is there any other way to try and serialize a VBScript object other than the way you suggested? Is it really necessary to hand-code each object to allow the state of an object to be presaerved in an ASP OO-based application?

Say it ain't so!!! :-(
 
In ASP that sort of this is done using dictionary objects, try the following, i hope its what you are after.

<%

Class Test ' some class

Public messages
Private sub Class_Initialize() ' a method
set messages=CreateObject("Scripting.Dictionary")
messages.Add "msg1", "One"
messages.Add "msg2", "Two"
End Sub

End Class


Set myTest = new Test ' an instance of the class

' loop through each property of the myTest object
' and display the property ?
For each x in myTest.messages
Response.write x & " " & myTest.messages(x) & "<br>"
Next
%>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top