使用变量引用类属性?

时间:2016-03-17 15:58:37

标签: vba access-vba

我有一个包含employeesnameidoffice字段的员工集合(officeto)。 我需要在等间距列中打印出这些信息。所以我需要找到最长的字符串名称,office,officeto ...的长度,并添加空格以使列间距相等。
我知道如何使用记录集将字段名称发送到函数中。 所以我的问题是......是否可以通过使用变量(类似于rst![fieldname])来引用类属性(name,office,officeto)。 我试着把它设置成一个字段上的记录集循环,但它没有编译。错误是class.property未定义。

Public Function PropertyLen(ByVal Property As String, ByRef Employees As colEmployees) As Integer

'This function uses a passed in class property, and returns the len of the longest class property in collection

On Error GoTo ErrorHandler:

Dim Emp As clsEmployee
Dim intLen As Integer 
Dim lngCount As Long

For lngCount = 1 To Employees.Count

       Set Emp = Employees.Item(lngCount)

       If Len(Trim(Emp.Property)) > intLen Then
            intLen = Len(Trim(Emp.Property))
       End If

       Set Emp = Nothing  
Next

    FieldLen = intLen

ExitFunc:
'clean up
    Set Emp = Nothing
    Exit Function

ErrorHandler:
    modErrorHandler.DisplayUnexpectedError Err.Number, Err.Description
    Resume ExitFunc

End Function

3 个答案:

答案 0 :(得分:6)

有一个用于测试的示例类模块#include "openvdb/io/Stream.h" #include "openvdb/openvdb.h" #include "openvdb/tools/Composite.h" #include "openvdb/tools/GridTransformer.h" #include "openvdb/tools/Interpolation.h" #include "openvdb/util/Util.h" #include <cmath> using namespace openvdb; int main(int argc, char **argv) { openvdb::initialize(); openvdb::io::File file(argv[1]); file.open(); GridBase::Ptr baseGrid; for (openvdb::io::File::NameIterator nameIter = file.beginName(); nameIter != file.endName(); ++nameIter) { baseGrid = file.readGrid(nameIter.gridName()); } file.close(); FloatGrid::Ptr gridA = gridPtrCast<FloatGrid>(baseGrid); FloatGrid::Ptr gridB = gridA->copy(CP_NEW); gridB->setTransform(gridA->transform().copy()); gridB->transform().postRotate(M_PI / 4.0f, math::Y_AXIS); tools::resampleToMatch<tools::BoxSampler>(*gridA, *gridB); FloatGrid::Ptr result = gridA->deepCopy(); FloatGrid::Ptr gridB2 = gridB->deepCopy(); tools::csgUnion(*result, *gridB); openvdb::io::File file_out(argv[2]); GridPtrVec grids; grids.push_back(gridA); grids.push_back(gridB2); grids.push_back(result); file_out.write(grids); file_out.close(); return 0; }

clsSample

您可以使用本机VBA函数Public Prop1 Public Prop2 Public Prop3 Public Prop4 按名称获取属性值:

CallByName()

如果您不想使用Sub TestGetProperty() Set objSample = New clsSample objSample.Prop1 = "TEST" Debug.Print CallByName(objSample, "Prop1", VbGet) ' TEST End Sub ,则可以使用jscript语法CallByName()

object[property]

BTW还有另一个允许evaluate a string into an objectcreate a new class instance by the class name的类似解决方案。

答案 1 :(得分:2)

您可以创建一个包装器函数,该函数接受属性的对象和字符串名称,并返回具有该名称的对象属性。像这样:

Function GetProperty(O As Object, property As String) As String
    Dim s As String
    property = LCase(property)
    Select Case property
        Case "name"
            s = O.Name
        Case "id"
            s = O.ID
        Case "office"
            s = O.Office
        Case "officeto"
            s = O.officeto
    End Select
    GetProperty = s
End Function

这大部分都是未经测试的(因为我没有想要实例化你班级的成员)但是它可以例如在评估GetProperty(Sheets(1), "name")

时返回Sheet1的名称

答案 2 :(得分:1)

其他答案对我不起作用。

我成功使用了Eval()语句,例如:

Debug.Print Eval("objSample." & Prop1NamesArray(i))

Prop1NamesArray(i)是要返回的属性名称的string array