VBA类成员函数和类定义问题继续

问题描述 投票:0回答:1

我之前发送了有关班级成员功能的问题。 现在我面临一个新问题,如果成员函数返回值是定义该函数的类。

成员函数

fReadData
可以工作(我之前提出了一个与此相关的问题),成员函数
fReadData2
不起作用。

或者:问题出在测试函数中吗?

'ClassOne

Option Explicit

Private pOne As Double


Private Sub Class_Initialize()
    
End Sub


Private Sub Class_Terminate()
    
End Sub


Public Property Get One() As Double
    One = pOne
End Property


Public Property Let One(lOne As Double)
    pOne = lOne
End Property

' this works, return value is double
Public Function fReadData(alue As Range) As Double

    pOne = alue.Cells(1)
    fReadData = pOne
    
End Function

' this don't like to work, return value is ClassOne
Public Function fReadData2(alue As Range) As ClassOne

    pOne = alue.Cells(1)
    fReadData2.One = pOne
    
End Function

测试如下:两个函数都读取单元格值并将其放在电子表格上

' main module

Option Explicit


Dim oOne As New ClassOne


' this works
Function Test(alue As Range) As Double

oOne.fReadData alue

Test = oOne.One

End Function


' this not
Function Test2(alue As Range) As Double

oOne.fReadData2 alue

Test2 = oOne.One

End Function

Test2 fails

函数

Test2
应将C1的值放入单元格C3,就像函数
Test
对单元格B3所做的那样

excel vba class member-functions
1个回答
0
投票
fReadData2.One = pOne

fReadData2
是 ffReadData2 函数内部的保留字,用于通过赋值的方式从函数返回值:
fReadData2 = pOne
。因此,成员访问运算符(点)不能应用于它,因为 fReadData2 为空(之前没有任何赋值)。

© www.soinside.com 2019 - 2024. All rights reserved.