• Class Properties Property (VB 6.0)

    Author
    Topic
    #375996

    Edited by Charlotte to activate link

    I posted this to the AccessD List, but it is really a VB Add-in I wrote. I’ve found it pretty handy, so I just wanted to share it. You can download it at http://www.wolfwares.com[/url%5D in the VB section.

    It’s free, in fact, if you want the source, just give me a holler.

    So, now that I am on the third paragraph, maybe you’d like to know what it is. Simple. A Class module is a code ‘object’, right? When you deal with most objects (Class modules excluded), you usually get a ‘Properties’ property, which allows you to do many things. You can set and retrieve property values dynamically (by referring to their number or name), you can find out how many properties you have, you can find out the type of properties they are, etc. Now, when you write you’re own class, you don’t have a ‘Properties’ property built in. Let’s say we write this class:

    Option Explicit
    Public FirstName As String
    Public LastName As String
    Dim strInitial As String
    Property Let Initial(strEnter)
    strInitial = strEnter
    End Property
    Property Get Initial()
    Initial = strInitial
    End Property

    And let’s save that class as clsName. Then write the following code:

    Private Sub Command1_Click()
    Dim cn As clsName
    Set cn = New clsName
    cn.FirstName = “John”
    cn.LastName = “Doe”
    cn.Initial = “M.”
    Debug.Print “First Name: ” & cn.FirstName
    Debug.Print “Initial: ” & cn.Initial
    Debug.Print “Last Name: ” & cn.LastName
    Set cn = Nothing
    End Sub

    When we run that code, we get:

    First Name: John
    Initial: M.
    Last Name: Doe

    That’s pretty simple. However, the more properties a class has, the longer a routine like that would take to write. However, the Class Properties Property Add-in will turn our Class Module’s code into this:

    (For the sake of space, I’ve stripped the comments it puts. KEEP the comments in the class modules, so it can update your classes when necessary)(I am attaching the code to a text file.

    That’s beefed up our simple class quite a bit. However, it does this automatically. (It also adds another class to your project, but obviously it only needs to do that once). Now that we have a Properties property, we can change our Command1 code to this:

    Private Sub Command1_Click()
    Dim cn As clsName
    Dim i As Long
    Set cn = New clsName
    cn.FirstName = “John”
    cn.LastName = “Doe”
    cn.Initial = “M.”
    For i = 1 To cn.Properties.Count
    Debug.Print cn.Properties(i, intPropertyName) & “: ” & cn.Properties(i)
    Next i
    Set cn = Nothing
    End Sub

    And the Immediate Window displays:

    FirstName: John
    LastName: Doe
    Initial: M.

    Obviously in this example, we aren’t saving a lot of code behind our button, but I am sure you can see what this can do to help out in some situations. Anyway, enjoy, and let me know if you run into problems with it.

    Drew

    Viewing 0 reply threads
    Author
    Replies
    • #614417

      Drew,
      I don’t see anything that would make it not work in VB5 (Word 97 VBA). Am I missing something?

      BTW, thanks for posting this. I’m interested in putting it thru the paces…

      • #614431

        Well the Add-in adds a class to your project. That class is used to ‘represent’ the individual properties of each property (let allowed, get allowed, variable type, number of arguments, etc.). That class also has an Enum, which I use to retrieve a property of a property. (ie, MyClass.Properties(“MyProperty”,intLetAllowed) That intLetAllowed is displayed in a dropdown, because of the Enum. I am unable to get that to work in Access 97. Even though Enum shows as a keyword, it just doesn’t work. However, the rest of it should work just fine, you would just have to remove the enum. Oh, one other thing (going from memory, I wrote this months ago…before I signed up to post here.), when retrieving values from a property, I use a select case, which essentially says:

        Case i, “MyProperty”

        That’s doing a comparison between a number or a string. VB6 has no problems with it. Access 97’s VBA (5) doesn’t like it. At least it was the other item that kicked up an error.

        • #614547

          Access 97 didn’t support custom enums and I don’t think the rest of Office 97 did either.

          • #614576

            Charlotte is right. Office 97 doesn’t support custom enums. Well, I know Word 97 does not support enum. That fact gave me greif when I was trying to downgrade a Word Application. sad

    Viewing 0 reply threads
    Reply To: Class Properties Property (VB 6.0)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: