如何在函数中使用用户定义的类型?

时间:2022-11-30 15:34:46

The following trivial example doesn't work:

下面这个简单的例子不起作用:

Public Type MyType
    a As Double
    b As Integer
End Type

Function Test() As Variant
    Dim x As MyType
    Test = 2
End Function

Compile Error: User-defined type not defined

编译错误:未定义的用户定义类型

How do I "define" the type?

我如何定义类型?

1 个解决方案

#1


1  

your code compiles fine in my excel 2010

您的代码在我的excel 2010中编译良好

anyhow it'd be of no use for exploiting a user defined type

不管怎样,它对利用用户定义的类型没有用处

here follows a "complete" example

下面是一个“完整”的示例

Option Explicit

Public Type MyType
    a As Double
    b As Integer
End Type

Sub main()
    Dim x As MyType

    'initialize fields of your type
    x.a = 1.2
    x.b = 1

    MsgBox Test(x)
End Sub


Function Test(x As MyType) As Variant

    Test = x.a + x.b

End Function

#1


1  

your code compiles fine in my excel 2010

您的代码在我的excel 2010中编译良好

anyhow it'd be of no use for exploiting a user defined type

不管怎样,它对利用用户定义的类型没有用处

here follows a "complete" example

下面是一个“完整”的示例

Option Explicit

Public Type MyType
    a As Double
    b As Integer
End Type

Sub main()
    Dim x As MyType

    'initialize fields of your type
    x.a = 1.2
    x.b = 1

    MsgBox Test(x)
End Sub


Function Test(x As MyType) As Variant

    Test = x.a + x.b

End Function