[Run-time error '13': Type Mismatch] Returning New Custom Object

trentt

New Member
Joined
Jun 28, 2018
Messages
7
I had this working yesterday and earlier this morning so I'm not sure what changed. ConveyExtract and BillingValidationExtract are both custom objects I've created that both implement a class called Extract. I have the following line getting slapped with a Type Mismatch error:


Code:
Function GetExtractModel(eExtractEnum As ExtractEnums)
    Select Case eExtractEnum
    
    Case ExtractEnums.Convey
        Set GetExtractModel = New ConveyExtract
         
    Case ExtractEnums.BillingValidation
[COLOR=#ff0000][B]        Set GetExtractModel = New BillingValidationExtract[/B][/COLOR]
    
    End Select
    
End Function

The follow line of code is calling this function:

Set eExtract = Extracts.GetExtractModel(eExtractEnum)
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Sorry for the double post, I can't seem to find the edit button.

I also get the same error if I do this:

Dim testy As BillingValidationExtract
Set testy = New BillingValidationExtract
 
Upvote 0
Have you checked the code for the class/object BillingValidationExtract?

Perhaps it's something in there that's causing the error but isn't being highlighted because of your Error Trapping settings.
 
Upvote 0
Have you checked the code for the class/object BillingValidationExtract?

Perhaps it's something in there that's causing the error but isn't being highlighted because of your Error Trapping settings.

I narrowed it down to this:

Code:
Private vHeaderRows() As Integer

Private Sub Class_Initialize()

[COLOR=#ff0000][B]vHeaderRows = Array(1, 2)[/B][/COLOR]
End Sub

I'm learning that initializing typed arrays is not anywhere near as simple in VBA as it is in other languages. Is there a concise way for me to assign integer values to vHeaderRows? I could go the quick way out and just make it variant instead of integer(), but then I lose my type safety. I would like to enforce that vHeaderRows can only receive integer values. Since all classes that implement Extract will need to have vHeaderRows assigned, and the length of the array could be variable, what's the best way for me to do that?
 
Upvote 0
You can't assign directly to an array like that in VBA.

If you are insistent on having an array of integers rather than variant/integers you could try something like this,
Code:
Dim vHeaderRows() As Integer
Dim arr As Variant
Dim I As Long

    arr = Array(1, 2)
    
    ReDim vHeaderRows(LBound(arr) To UBound(arr))

    For I = LBound(arr) To UBound(arr)
        vHeaderRows(I) = arr(I)
    Next I
 
Upvote 0

Forum statistics

Threads
1,214,907
Messages
6,122,183
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top