将对象添加到另一个Objetct VBA

时间:2018-07-26 12:57:56

标签: vba excel-vba

我创建了两个类,第一个是流体类,第二个是热交换器。

热交换器必须包含2种流体,但是我不知道如何将流体添加到交换器中...

Sub test()
Dim water As cFluide
Set water = New cFluide
water.Temp_init = 49.1
water.Temp_out = 36.6
water.Flow_init = 124.4

Dim HX As cHX
Set HX = New cHX

Dim Fluide_1 As cFluide
Set Fluide_1 = New cFluide

HX.Fluide_1 = water
End Sub

我不断收到以下错误

  

'91':对象变量或未设置块变量

有人对此有任何想法吗?

这是cHX类的定义:

Dim HXSurface As Single
Dim HXh As Single
Dim HXType As String

Dim HXFluide_1 As cFluide
Dim HXFluide_2 As cFluide

Public Property Get Surface() As Single
    Surface = HXSurface
End Property

Public Property Let Surface(S As Single)
    HXSurface = Application.WorksheetFunction.Max(0, S)
End Property

Public Property Get h() As Single
    h = HXh
End Property

Public Property Let h(h As Single)
    HXh = Application.WorksheetFunction.Max(0, h)
End Property

Public Property Get Type_HX() As String
    Type_HX = HXType
End Property

Public Property Let Type_HX(t As String)
    HXType = Application.WorksheetFunction.Max(0, t)
End Property

Public Property Get Fluide_1() As cFluide
    Fluide_1 = HXFluide_1
End Property

Public Property Let Fluide_1(f_1 As cFluide)
    HXFluide_1 = f_1
End Property

Public Property Get Fluide_2() As cFluide
    Fluide_2 = HXFluide_2
End Property

Public Property Let Fluide_2(f_2 As cFluide)
    HXFluide_2 = f_2
End Property

2 个答案:

答案 0 :(得分:2)

分配对象引用时,必须始终使用Set

属性语法为Set,而不是Let

Public Property Set Fluide_1(f_1 As cFluide)
    Set HXFluide_1 = f_1
End Property

在客户端中:

Set HX.Fluide_1 = water

答案 1 :(得分:0)

我们在这里处理对象,因此需要对其进行“设置”。这是一个非常简单的示例,说明如何设置cHX类:

Option Explicit

Private oFluid1 As Object 'cFluid
Private oFluid2 As Object 'cFluid

Public Property Get Fluid1() As Object 'cFluid
    Set Fluid1 = oFluid1
End Property

Public Property Let Fluid1(oFld As Object) 'cFluid)
    Set oFluid1 = oFld 
End Property

Public Property Get Fluid2() As Object 'cFluid
    Set Fluid2 = oFluid2
End Property

Public Property Let Fluid2(oFld As Object) 'cFluid)
    Set oFluid2 = oFld 
End Property

请注意,可以将流体设置为单个数组变量。另外,如果需要,您可以声明它们AS cFluid

在项目中的某些地方,您将按以下方式引用这些类对象:

Sub a()
    Dim HX As cHX
    Dim water1 As cFluid
    Dim water2 As cFluid

    Set HX = New cHX
    Set water1 = New cFluid
    Set water2 = New cFluid
    '.....
    Set HX.Fluid1 = water1
    Set HX.Fluid2 = water2
End Sub

我希望这可以解决当前的困难。