Wrox Programmer Forums
|
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
 
Old August 22nd, 2003, 06:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default Recordset Style Class

In a Recordset you can do things like this:

Code:
Object.Fields.Count
Object.Fields(0).Name
How can I do this within my own Class Module?

From what I have read I need to create a Collection of Fields Classes. However if the user wants to add a new field to the collection, they must define their field and then call

Code:
Object.Fields.Add MyFieldsClass
I could of course write my own AddField method of the root class, however they will still be able to call Object.Fields.Add and add potentially a completely different class to the collection.

How also can I implement the facility of using Object.Fields(0).Name? If Fields is a public property of type Collection, then you can cycle through all the objects in the collection using For Each...Next, but you can not simply call the .Name part of the property.

Regards
Owain Williams
__________________
Regards
Owain Williams
 
Old August 22nd, 2003, 07:32 AM
Authorized User
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by owain
 How also can I implement the facility of using Object.Fields(0).Name? If Fields is a public property of type Collection, then you can cycle through all the objects in the collection using For Each...Next, but you can not simply call the .Name part of the property.
I don't understand your problem. You can call the .Name property of your Field object, as long as the property is public.

If you want to see the list of property when you have type this: Object.Fields(0). you will need to code a Fields Class.

Stéphane Lajoie
 
Old August 22nd, 2003, 07:56 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have not explained my problem very well. I am wanting to create my own class that acts in a similar way to a recordset object. I have created a recordset class and a field class. The field class has (at the moment) to public properties Name and Value. What do I have to do in the recordset class to add the ability for the user of my class to use it in the following ways:

Code:
Object.Fields.Count
Object.Fields(0).Name
Object.Fields(0).Value
Where do I declare my collection of Field's and how does the user access them. For example, is either of these correct:

Code:
Dim FieldsCollection                As New Collection

Public Property Get Fields(Index As Integer) As Field
    Set Fields = FieldsCollection(Index)
End Property

Or

Public Property Get Fields() As Collection

'What should go here?
Or am I going about it in completely the wrong way, or is it simply not possible in VB?

Regards
Owain Williams
 
Old August 22nd, 2003, 08:22 AM
Authorized User
 
Join Date: Jun 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well both solution work, It just that one requires more coding.
Code:
Public Property Get Fields() As Collection
    set Fields = FieldsCollection
End Property
With this solution is faster, but I don't really like it since you don't know what the collection will return.

With the other solution, you will have more control of what will be return. But I'll suggest that you create a new class Fields. In your Recordset class should look like this:
Code:
Private FieldsProperty As New Fields

Public Property Get Fields As Fields
    Set Fields = FieldsProperty
End Property
Your Fields class should look like this:
Code:
Private FieldsCollection As New Collection

Public Sub Add(Byval oField As Field)
    Call FieldsCollection.Add(oField)
End Sub

Public Property Get Count As Integer
    Count = FieldsCollection.Count
End Property

Public Property Get Item(Byval Index as Integer) As Field
    Set Item = FieldsCollection(Index)
End Property
Make sure to set the Item property to the Default property so that Object.Fields(0).Name will work

Stéphane Lajoie
 
Old August 22nd, 2003, 01:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jlick
Default

This gives you more features/functionality than some of the others supplied.

Option Explicit

' local collection
Private m_objFields As Collection

Public Sub Add(ByRef p_objField As clsField)

    ' Add the field to the local collection.
    m_objFields.Add p_objField, p_objField.Name

End Sub

Public Property Get Item(ByVal p_vntIndexKey As Variant) As clsField

    ' Return the specified field. This can be by index or key.
    ' index - The caller knows the field number they want, and pass it in.
    ' key - The caller knows the field name they want, and pass it in.
    Set Item = m_objFields(p_vntIndexKey)

End Property

Public Property Get Count() As Long

    ' Returns the number of fields.
    Count = m_objFields.Count

End Property

Public Sub Remove(ByVal p_vntIndexKey As Variant)

    ' Removes the field that the called requests. See Get for info about index vs. key.
    m_objFields.Remove vntIndexKey

End Sub

Public Property Get NewEnum() As IUnknown

    ' This is a little more complex to implement.
    ' 1) Add this code to your record class.
    ' 2) Go to the Tools Menu, and choose "Procedure Attributes...".
    ' The procedure attributes window will display.
    ' 3) Select the NewEnum entry in the Name dropdown list.
    ' 4) Click the Advanced button.
    ' The window will get bigger with more options.
    ' 5) In the Procedure Id combobox, type in "-4" (without the quotes).
    ' 6) Check the Hide This Member Checkbox.

    ' Returns each field in the following situation...
    ' For Each objField In objRecord
    ' {Do something with the field.}
    ' Next objField

    Set NewEnum = m_objFields.[_NewEnum]

End Property

Private Sub Class_Initialize()

    ' Instantiate the collection.
    Set m_objFields = New Collection

End Sub

Public Sub Clear()

    ' removes all the fields from the record.
    Dim lngCounter As Long

    For lngCounter = 1 To m_objFields.Count
        m_objFields.Remove 1
    End If

End Sub

Private Sub Class_Terminate()

    ' Destroy the collection.

    If Not m_objFields Is Nothing Then
        Call Clear
        Set m_objFields = Nothing
    End If

End Sub



John R Lick
[email protected]





Similar Threads
Thread Thread Starter Forum Replies Last Post
Switching back to class instead of style nerssi Javascript 4 September 26th, 2008 03:12 AM
LockType Property of the RecordSet Class dslagle2 Excel VBA 1 August 22nd, 2007 01:37 PM
Smalltalk-style class browser for VS? Raconteur Visual Studio 2005 3 May 3rd, 2007 11:10 AM
Custom style class oldSkool BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 0 January 9th, 2007 03:39 AM
Setting the Style Sheet class jacob ASP.NET 1.0 and 1.1 Basics 4 August 20th, 2003 01:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.