Type mismatch in class

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
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:
Not sure what you mean.

When I say arrays example, I mean post 5.
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
In that example that you referred to, it was simply pointing out that it is possible to pass multiple arguments to a Property Let procedure. So in your example, it might make a bit more sense to include two additional member variables to hold the length and width. Then these values could also be retrieved in addition to the area. So your example could be amended as follows...

Class Module:

Code:
Option Explicit

Private dblA As Double
Private pLength As Double
Private pWidth As Double


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


    pLength = lngth
    pWidth = wdth
    dblA = ar


End Property


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


    lngth = pLength
    wdth = pWidth
    Area = dblA


End Property

Regular Module:

Code:
Option Explicit

Sub clsRectangleRun()


    Dim rect As New clsRectangle
    Dim l As Double
    Dim w As Double
    Dim a As Double


    l = 10
    w = 20


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


    a = rect.Area(l, w)
    
    Debug.Print "Length = " & l & "; Width = " & w & "; Area = " & a


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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