Type mismatch in class

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,913
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
In the code below, why am I getting a Type mismatch error on this line?

Rich (BB code):
a = rect.AreaItem(l, w)


Standard Module:

Rich (BB code):
Option Explicit

Sub Start()

    Dim l As Double
    Dim w As Double
    
    Dim rect As New clsRectangle
    
    l = 10
    w = 20
    
    rect.Area = l * w
    
    Dim a As Double
    
    a = rect.AreaItem(l, w)

End Sub


ClsRectangle:

Rich (BB code):
Option Explicit

    Private dblA As Variant

Public Property Let Area(ar As Variant)

    dblA = ar

End Property

Public Property Get AreaItem(lngth As Double, wdth As Double) As Variant

    AreaItem = dblA(lngth, wdth)

End Property


Thanks
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Because dblA is a Double and you're trying to access it as an array
 
Last edited:
Upvote 0
Because dblA is a Double and you're trying to access it as an array

Thanks

Is this the only way round?


ClsRectangle:

Code:
    Private dblA As Double

Public Property Let Area(lngth As Double, wdth As Double, ar As Double)

    dblA = ar

End Property

Public Property Get Area(lngth As Double, wdth As Double) As Double

    Area = dblA


End Property


Standard Module:

Code:
Sub clsRectangleRun()

    Dim l As Double

    Dim w As Double

    Dim rect As New clsRectangle

    l = 10
    w = 20

    rect.Area(l, w) = l * w

    Dim a As Double

    a = rect.Area(l, w)

End Sub
 
Last edited:
Upvote 0
What are you actually trying to do? Your code doesn't make much sense to me
 
Upvote 0
What are you actually trying to do? Your code doesn't make much sense to me

I am trying to figure out the correct syntax when you have arguments in a Get Property.

The following example works:


Sheet1 contains data, say 1, 2, 3 down to 100 in column A.

Standard Module:

Code:
Option Explicit


Sub WithClassItem()
    
    Dim myclass As Class1
        
    Set myclass = New Class1
    
    myclass.myarray = Range("A1").CurrentRegion.Value
    
    Dim myarrayrows As Long

    myarrayrows = UBound(myclass.myarray, 1)
    
    Dim newarray() As Variant
    
    ReDim newarray(1 To 10, 1 To 1) As Variant
    
    Dim Counter As Long
    
    For Counter = 1 To 10
    
        newarray(Counter, 1) = myclass.myarrayItem(Counter, 1)
    
    Next Counter
    
    Range("E1").Resize(myarrayrows, 1).Value = newarray
    
    Set myclass = Nothing
   
End Sub


Class1:

Code:
Option Explicit


    Private temparray As Variant

Property Get myarray() As Variant

    myarray = temparray

End Property

Property Let myarray(passedarray As Variant)

    temparray = passedarray

End Property

Property Get myarrayItem(RowIndex As Long, ColIndex As Long) As Variant

    myarrayItem = temparray(RowIndex, ColIndex)
    
End Property

so with the rectangle example, I was trying to replicate it but obviously couldn't figure out the exact syntax.
 
Upvote 0
Well you'd need to pass in an array (or generate one in your rectangle object) and access it as in the code above.
 
Upvote 0
Well you'd need to pass in an array (or generate one in your rectangle object) and access it as in the code above.

Thanks but can you post some code as to how it might be done?

I tried to adapt this code:

Rich (BB code):
http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=160:excel-vba-custom-classes-a-objects-class-modules-custom-events&catid=79&Itemid=475

for the rectangle example.

Rich (BB code):
Private dblA As Double Public Property Let Area(lngth As Double, wdth As Double, ar As Double) dblA = ar End Property Public Property Get Area(lngth As Double, wdth As Double) As Double Area = dblA End PropertySub clsRectangleRun() Dim l As Double Dim w As Double Dim rect As New clsRectangle l = 10 w = 20 rect.Area(l, w) = l * w a = rect.Area(l, w) End Sub

but this line seems pointless:

Rich (BB code):
a = rect.Area(l, w)


in that even if I wrote:

Rich (BB code):
Rich (BB code):
a = rect.Area(10000, 200000)

a still returns l*w, ie 200.
 
Last edited:
Upvote 0
It doesn't help that your example is rather contrived and illogical - one wouldn't normally pass arguments to a get property, however, an example would be:
Rich (BB code):
Option Explicit

Sub Start()

    
    Dim rect As New clsRectangle
    
    Dim matrix(2) As Variant
    
    matrix(0) = Array(10, 20)
    matrix(1) = Array(20, 30)
    matrix(2) = Array(30, 40)
    
    rect.Rectangles = matrix
    
    Debug.Print "Rectangle 1 has an area of: " & rect.AreaItem(0)
    Debug.Print "Rectangle 2 has an area of: " & rect.AreaItem(1)
    Debug.Print "Rectangle 3 has an area of: " & rect.AreaItem(2)

End Sub

clsRectangle:
Rich (BB code):
Option Explicit

    Private p_rectangles As Variant

Public Property Let Rectangles(items As Variant)

    p_rectangles = items

End Property

Public Property Get AreaItem(rectangleIndex As Long) As Variant
    
    Dim rectangle As Variant
    rectangle = p_rectangles(rectangleIndex)
    AreaItem = rectangle(0) * rectangle(1)

End Property
 
Upvote 0
It doesn't help that your example is rather contrived and illogical - one wouldn't normally pass arguments to a get property, however, an example would be:
Rich (BB code):
Option Explicit

Sub Start()

    
    Dim rect As New clsRectangle
    
    Dim matrix(2) As Variant
    
    matrix(0) = Array(10, 20)
    matrix(1) = Array(20, 30)
    matrix(2) = Array(30, 40)
    
    rect.Rectangles = matrix
    
    Debug.Print "Rectangle 1 has an area of: " & rect.AreaItem(0)
    Debug.Print "Rectangle 2 has an area of: " & rect.AreaItem(1)
    Debug.Print "Rectangle 3 has an area of: " & rect.AreaItem(2)

End Sub

clsRectangle:
Rich (BB code):
Option Explicit

    Private p_rectangles As Variant

Public Property Let Rectangles(items As Variant)

    p_rectangles = items

End Property

Public Property Get AreaItem(rectangleIndex As Long) As Variant
    
    Dim rectangle As Variant
    rectangle = p_rectangles(rectangleIndex)
    AreaItem = rectangle(0) * rectangle(1)

End Property


Thanks for your example.

As stated earlier, the rectangle example was not mine but I got it from here:

Rich (BB code):
http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=160:excel-vba-custom-classes-a-objects-class-modules-custom-events&catid=79&Itemid=475

Re the arrays example, it was necessary to have args passed to the Get Property, otherwise the whole array would be returned, meaning it would run very slowly, :)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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