locked
Function That Returns An Object RRS feed

  • Question

  • User646364117 posted

    I am writing a function that I want to return a two dimension variant array. However Visual Studio is changing the function declaration to object

    Public Function CellExtractionArray(ByRef OrigFormula As String) As Object(,)
    
            Dim vCellArray(,) As Object
    
                    'irrelevant code deleted
    
    
                    ReDim Preserve vCellArray(3, ValidCellCount)
                    vCellArray(0, 0) = ValidCellCount
                    vCellArray(1, ValidCellCount) = Mid(OrigFormula, PossibleStartPosition, PossibleEndingPosition -             PossibleStartPosition + 1)
                    vCellArray(2, ValidCellCount) = PossibleStartPosition
                    vCellArray(3, ValidCellCount) = PossibleEndingPosition
                End If
                ValidCell = False
            Next 'Candidate
            CellExtractionArray = vCellArray
    Return CellExtractionArray
    
    
        End Function

    I had coded the function as a variant and vCellArray as a variant but they were changed by VS to objects.

    Question 1: I am getting an intellisense error saying that near the end of above code saying that vCellArray is being used before it ha been assigned a value. How do I prevent this? How should I initialize vCellArray?

    In the procedure that calls this function, my variant declarations were changed to objects. see below. How do I convert the object returned by my function to a variant array? Do I have to use CType?

    Public Function PredictedFormula(ByVal OrigFormula As String) As String
    
            Dim CellExtractedArray(,) As Object
            Dim FormulaPieces(), NewFormula As String
            CellExtractedArray = CellExtractionArray(OrigFormula)
          
            Try
                For j = 1 To CInt(CellExtractedArray(0, 0)) 'Number of Cells in the formula
                    'Get Cells and Position in the formula
                    CellString = CStr(CellExtractedArray(1, j))
                    StartPos = CInt(CellExtractedArray(2, j))
                    EndPos = CInt(CellExtractedArray(3, j))

    Friday, September 9, 2016 1:15 AM

Answers

  • User36583972 posted

    Hi sg48asp,

    Question 1: I am getting an intellisense error saying that near the end of above code saying that vCellArray is being used before it ha been assigned a value. How do I prevent this? How should I initialize vCellArray?

    You can change your code as the below.

    Dim obj1 As Object
    Set obj1 = New Project1.YourClass
    
    

    In the procedure that calls this function, my variant declarations were changed to objects. see below. How do I convert the object returned by my function to a variant array? Do I have to use CType?

    You can use CType.

            Dim anArray As Array
            anArray = CType(declarations, Array)
    
    

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 9, 2016 11:38 AM