 |
| Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 9th, 2006, 07:37 PM
|
|
Registered User
|
|
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Classes (OOP) in VBScript 6
I know OOP in VB6 is nothing short of poor, but I have a situation where I'd like to use what little OOP support is available in VB6.
I'm trying to include a method in my user defined class that will write out all the properties of an object of that class.
Public Sub Print()
For Each x in this
'Access object properties here
Next
End Sub
Obviously, I can't use the word THIS to reference the object that called the method. Is this possible and what syntax would I use?
Is this possible without having to pass in a reference to the object to the method? What's a point of making the method a member of the class if I have to pass in a reference to the object I want member method to print!?!
Help!
|
|

March 10th, 2006, 09:24 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
hi there.. do you have a little example of what you need??
doesn't look hard to know your own properties, you dont have this but you have me...
HTH
Gonzalo
|
|

March 10th, 2006, 10:40 AM
|
|
Registered User
|
|
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the reply. Yes, I gave the important part of the code segment above in my first post. Here it is again with a sample class definition included with the "Print" method.
Class definition:
----
Class MyClass
' Example properties
Private m_strValue1
Private m_strValue2
Private m_lngValue1
' Example Let statement
Public Property Let Value1(ByVal varData)
m_strValue1 = varData
End Property
' This is the code in question
Public Sub Print()
'How to I reference the object that invoked this method
'so that I can iterate through the object's properties using
'the For...each statement and avoid having to manually access
'each property and print it out? I don't think that I can
'use "this" to reference the object...then how?
For Each x in ?
'Access object properties here? how?
Next
End Sub
End Class
----
Class implementation:
----
Dim myObj
Set myObj = New MyClass
myObj.Value1 = "hello world"
myObj.Print
Set myObj = Nothing
----
The "Print" method should print out all the property names and values of the object. As you can see by the class definition, all the properties are private, so they are not directly accessible outside the class definition. There are no "Get" properties either -- this is on purpose to provide a high-level of encapsulation.
The goal of the "Print" method is to essentially display all the property names and values (public or private) of the object without having to manually reference each property inside the "Print" method.
The "For...each" statement is good for iterating through arrays and objects, I've just not found any examples on how to use "For...Each" inside a class defition to iterate through the properties of the object invoking the "Print" method.
Make more sense?
|
|

March 10th, 2006, 10:50 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
yes.. you want your print code to automatic know the name of its class properties.. im rigth??
you cannot iterate a class unless you have an specific procedure that let you do that (i have an implementation of that, you want it??)
but that class that i use, use a collection also to store the names of the properties (so you have to fill it, it's something like put every properties on the print method).
HTH
Gonzalo
|
|

March 10th, 2006, 07:52 PM
|
|
Registered User
|
|
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Gonzalo (gbianchi),
Yes, I'd be interested in looking at the code that you've written. Thanks.
|
|

March 12th, 2006, 01:41 PM
|
|
Registered User
|
|
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Gonzalo,
Of course, having written the class definiton for MyClass, I know all the properties of the class, but one of the principles of object oriented programming is encapsulation (restricting access or hiding some of the objects data).
I want individuals who create an object of MyClass to be able to view all the properties and the property values. Also, it's handy to be able to write out the properties of an object and their values just for debugging purpose.
Most OOP languages have a mechanism to reference the object that invokes a method within the invoked method. If this is possible in VBScript, then it would also be possible to use the "For...each" language construct of to iterate the object and write out all it's properties and values.
In other words, this would allow a "Print" method to be written once that does not require future modification even if the MyClass itself is changed (i.e. adding properties or remving properties). Such a "Print" method would automatically scale with future property changes to the MyClass.
Here is the VBScript/Psuedo code again:
Public Sub Print()
For Each x in (a reference to the object that invoked this method)
'Code to access object properties and property values
Next
End Sub
Notice the "(a reference to the object that invoked the method)"... is there a mechanism in VBScript that will allow for this type of reference?
If so, what is it?
|
|

March 13th, 2006, 10:43 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
|
|
hi there..
first of all, VB is not OOP, sorry.. it's almost OOP, but more event driven...
here is the code of this little class...
Code:
Option Explicit
'AddGB: Implementaciones
Implements ICGen.ColIteradora 'this implementation has nothing important
'Objeto Coleccion
Private ColRecorrer As Collection 'Collection of properties
'just load the collection
Private Sub Class_Initialize()
Dim ObjCamposCol As ICGen.ColCampos
Set ColRecorrer = New Collection
Set ObjCamposCol = New ICGen.ColCampos
ObjCamposCol.CampoClase = "ACT"
ObjCamposCol.CampoBD = "ACT"
ObjCamposCol.Pk = True
ColRecorrer.Add ObjCamposCol
Set ObjCamposCol = New ICGen.ColCampos
ObjCamposCol.CampoClase = "ACT_ANT"
ObjCamposCol.CampoBD = "ACT_ANT"
ObjCamposCol.Pk = False
ColRecorrer.Add ObjCamposCol
Set ObjCamposCol = New ICGen.ColCampos
ObjCamposCol.CampoClase = "ACT_NUM"
ObjCamposCol.CampoBD = "ACT_NUM"
ObjCamposCol.Pk = False
ColRecorrer.Add ObjCamposCol
Set ObjCamposCol = New ICGen.ColCampos
ObjCamposCol.CampoClase = "DESC_ACT"
ObjCamposCol.CampoBD = "DESC_ACT"
ObjCamposCol.Pk = False
ColRecorrer.Add ObjCamposCol
Set ObjCamposCol = New ICGen.ColCampos
ObjCamposCol.CampoClase = "INFO_ACT"
ObjCamposCol.CampoBD = "INFO_ACT"
ObjCamposCol.Pk = False
ColRecorrer.Add ObjCamposCol
Set ObjCamposCol = Nothing
End Sub
Private Sub Class_Terminate()
Set ColRecorrer = Nothing
End Sub
'this method allow you to use a for each with the class
'the attributes of this method (in tools procedure attributes) should be -4
Public Property Get ColIteradora_NewEnum() As stdole.IUnknown
'this property allows you to enumerate
'this collection with the For...Each syntax
Set ColIteradora_NewEnum = ColRecorrer.[_NewEnum]
End Property
this is the implement class
Code:
Option Explicit
'''-'''
'''<summary>Clase que genera que una clase sea recorrida como si fuera una coleccion.
'''</summary>
'''-'''
Public Property Get NewEnum() As IUnknown
End Property
hope this help..
HTH
Gonzalo
|
|

March 13th, 2006, 10:58 AM
|
|
Registered User
|
|
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Yeah, I'm aware of VB's poor, poor implementation of OOP (if you can even call it that)...note my first post on this topic...
|
|

March 13th, 2006, 11:46 AM
|
|
Registered User
|
|
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks for the code...I'll give it whirl...
|
|

March 13th, 2006, 11:51 AM
|
|
Registered User
|
|
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Question...
Doesn't "ColRecorrer" just contain the values of the object created when the class is intialized? What if a property is changed thereafter? This collection will not contain the new value of the updated property, correct?
|
|
 |