CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2003
    Posts
    322

    Is it legal VB to return a collection as a property from a class?

    I've created a collection in a class, now want to return it via a property in the class.

    The invocation (below) to return the class is giving me "Compile Error Argument not optional"

    There are no arguments- why is the compiler complaining?

    Code:
    Sub CreateLabReport()
    
    Dim filereader As New clsReadTextFile
    Dim AllTestNames As New Collection
     
        AllTestNames = filereader.getAllTestNames() ' this line causes error msg
    
    end sub
    The AllTestNames collection is defined in another class as follows

    Code:
    Private colAlltests As New Collection
    Property Get getAllTestNames() As Collection
        getAllTestNames = colAlltests
    End Property
    
    Private Sub Class_Initialize()
        testNamesMasterList = "testnames.txt"
        Call ReadTestNames
    End Sub
    
    Private Sub SeparateTestnames(listofTests As String)
    
    Dim newtest As clsTestDescription
    Dim teststr() As String
    Dim x, lastTestNum As Integer
    Dim testNumber As Long
    
        teststr() = Split(listofTests, vbCrLf)
        lastTestNum = UBound(teststr)
        testNumber = 1 ' these are arbitrary test numbers
        For x = 0 To lastTestNum
            'If teststr(x) <> "" Then
            If LenB(teststr(x)) <> 0 Then ' don't add to collection, if string is empty
                Set newtest = New clsTestDescription
                newtest.letTestname = Trim(teststr(x))
                newtest.letTestNumber = testNumber    ' arbitrary test number
                colAlltests.Add newtest
                testNumber = testNumber + 1
            End If
        Next x
    End Sub
    Last edited by cappy2112; July 2nd, 2007 at 01:39 PM.

  2. #2
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Is it legal VB to return a collection as a property from a class?

    I think you must use the Set directive to assign a collection
    Try this:
    Code:
    Property Get getAllTestNames() As Collection
        Set getAllTestNames = colAlltests
    End Property
    ...
    Sub CreateLabReport()
    
    Dim filereader As New clsReadTextFile
    Dim AllTestNames As Collection
     
        Set AllTestNames = filereader.getAllTestNames() ' this line causes error msg
    
    end sub
    Seems to work for me...

  3. #3
    Join Date
    Apr 2003
    Posts
    322

    Re: Is it legal VB to return a collection as a property from a class?

    Quote Originally Posted by WoF
    I think you must use the Set directive to assign a collection
    Try this:
    Code:
    Property Get getAllTestNames() As Collection
        Set getAllTestNames = colAlltests
    End Property
    ...
    Sub CreateLabReport()
    
    Dim filereader As New clsReadTextFile
    Dim AllTestNames As Collection
     
        Set AllTestNames = filereader.getAllTestNames() ' this line causes error msg
    
    end sub
    Seems to work for me...
    It works for me too- thanks

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured