SPONSORS:






User Tag List

Thanks Thanks:  0
Likes Likes:  0
Dislikes Dislikes:  0
Results 1 to 4 of 4
  1. #1
    Junior Member
    Join Date
    Apr 2006
    Location
    Millington, IL
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    VBScript Class elements

    Does anyone know if there is a way to variablize the element names in a VBScript Class?

    So If I have a class defined as:

    Class PersonInfo
    ' Person Info
    Dim LName
    Dim FName
    Dim MName
    End Class

    Then I create a variable to use as follows:

    Dim PP

    Set PP to New PersonInfo

    Then I can use the the info like:

    PP.LName="Last"
    PP.FName="First"

    Is there any way to varablize the element name? Like this

    array(0)="LName"
    array(1)="FName"

    PP.array(0)="Last"
    PP.array(1)="First"

    This does not work, but it would be helpful if it did and I was wondering if anyone knows of a way to accomplish something like this?

    Also, Is there a way to get all the names if the elements of a class? What I would like to be able to do is pass the class "PP" to a function and get back a list of names "LName, FName and MName".

    Any help would be appreciated. This is the first time in many years I have come to this site and not been able to find the answer to my questions already solved. I am betting that I am not able to do what I want to do.

    Thanks
    John
    John M. Daly
    Software Test Automation Engineer
    DeVry Inc.

  2. #2
    Senior Member
    Join Date
    Jan 2006
    Location
    Bengaluru
    Posts
    494
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: VBScript Class elements

    Use 'Eval' or 'Execute' functions.
    Suraj

  3. #3
    Advanced Member cgrzelka's Avatar
    Join Date
    Feb 2005
    Location
    Bordeaux, France
    Posts
    750
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: VBScript Class elements

    [ QUOTE ]

    This does not work, but it would be helpful if it did and I was wondering if anyone knows of a way to accomplish something like this?


    [/ QUOTE ]
    You can use the Execute or ExecuteGlobal statements.
    <font class="small">Code:</font><hr /><pre>
    Class PersonInfo
    ' Person Info
    Dim LName
    Dim FName
    Dim MName
    End Class

    Dim PP: Set PP = New PersonInfo

    Dim array(1)
    array(0)="LName"
    array(1)="FName"

    Dim data (1)
    data(0) = "Last"
    data(1) = "First"

    quote = """"

    For i = LBound(array) To UBound(array)
    stmt = "PP." &amp; array(i) &amp; " = " &amp; quote &amp; data(i) &amp; quote
    Execute stmt
    Next

    MsgBox "LName = '" &amp; PP.LName &amp; "'"
    MsgBox "FName = '" &amp; PP.FName &amp; "'"
    </pre><hr />

    [ QUOTE ]

    Also, Is there a way to get all the names if the elements of a class?


    [/ QUOTE ]

    This is called "reflection". I do not know of any way to get the names of the public members of a VBScript class. If you were using a COM object (like Scripting.Dictionnary for instance), I know ways to get its members and methods from VBScript. But VBScript classes are generally not exposed.

    So, you need some "help" from the class to get its public members, like in the following code :
    <font class="small">Code:</font><hr /><pre>
    Class PersonInfo
    ' Person Info
    Dim LName
    Dim FName
    Dim MName
    Public Property Get members ()
    Dim array(2)
    array(0) = "LName"
    array(1) = "FName"
    array(2) = "MName"
    members = array
    End Property
    End Class

    Dim PP: Set PP = New PersonInfo

    Dim PersonInfoMembers
    PersonInfoMembers = PP.members

    For i = LBound(PersonInfoMembers) To UBound(PersonInfoMembers)
    MsgBox PersonInfoMembers(i)
    Next
    </pre><hr />

  4. #4
    Senior Member
    Join Date
    Jan 2006
    Location
    Bengaluru
    Posts
    494
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Total Downloaded
    0

    Re: VBScript Class elements

    Christian Grzelka,
    Nice work around for 'reflection' in VbScript.
    Suraj

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Resources saved on this page: MySQL 10.71%
vBulletin Optimisation provided by vB Optimise v2.6.4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.2.8 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominate (Lite) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 11:50 AM.

Copyright BetaSoft Inc.