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))
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
sg48asp
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
105 Points
533 Posts
Function That Returns An Object
Sep 09, 2016 01:15 AM|sg48asp|LINK
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
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?
Star
11464 Points
2439 Posts
Re: Function That Returns An Object
Sep 09, 2016 11:38 AM|Yohann Lu|LINK
Hi sg48asp,
You can change your code as the below.
You can use CType.
Best Regards,
Yohann Lu