Results 1 to 5 of 5
  1. #1
    4 Star Lounger
    Join Date
    Aug 2002
    Location
    Dallas, Texas, USA
    Posts
    594
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Class Properties Property (VB 6.0)

    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 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
    Attached Files Attached Files

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Class Properties Property (VB 6.0)

    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...
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    4 Star Lounger
    Join Date
    Aug 2002
    Location
    Dallas, Texas, USA
    Posts
    594
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Class Properties Property (VB 6.0)

    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.

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Class Properties Property (VB 6.0)

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

  5. #5
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Class Properties Property (VB 6.0)

    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. <img src=/S/sad.gif border=0 alt=sad width=15 height=15>
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •