2011-02-23 16 views
12

Tengo un módulo de clase, llamada Normal, en VBA con el siguiente código:propiedad Conjunto de clase de VBA con referencia a un objeto

Private mLine As LineElement 

Public Property Get Line() As LineElement 
    Line = mLine 
End Property 

Public Property Set Line(ByRef vLine As LineElement) 
    mLine = vLine 
End Property 

Esta clase se utiliza el siguiente código:

Sub Run 
    Dim Line As LineElement 
    Set Line = New LineElement 

    Dim Norm As Normal 
    Set Norm = New Normal 
    Set Norm.Line = Line 'FAILS here with "Object Variable or With Block Variable not set"' 
End Sub 

además, si se cambia el código en el módulo de clase a Normal:

Private mLine As LineElement 

Public Property Get Line() As LineElement 
    Line = mLine 
End Property 

Public Sub SetLine(ByRef vLine As LineElement) 'changed from property to sub' 
    mLine = vLine 
End Property 

y la failin g línea a

Norm.SetLine(Line) 

Aparece el error "El objeto no admite esta propiedad o método". ¿Qué estoy haciendo exactamente mal en ambos casos?

Respuesta

18

Prueba esto:

+4

d'oh tan simple! Gracias por la ayuda. (Odio al editor de VBA que no proporciona más ayuda para cosas simples como esta) –

+0

double d'oh! Las horas perdidas no pueden asignarse a ActiveX. Gracias. –

3

Ambas propiedades deben tener "Ajuste" palabra clave

Private mLine As LineElement 

Public Property Get Line() As LineElement 
    Set Line = mLine 'Set keyword must be present 
End Property 

Public Property Set Line(vLine As LineElement) ' ByRef is the default in VBA 
    Set mLine = vLine 'Set keyword must be present 
End Property 
0

la instrucción SET se utiliza para hacer referencia de un objeto a una variable de objeto. No tiene que usar la palabra clave Set, si está tratando con tipos primitivos y nativos integrados como entero, doble, cadena, etc. Aquí se trata de un objeto de tipo LineElement.

0

probar este

Private mLine As new LineElement 

Public Property Get Line() As LineElement 
    Set Line = mLine 
End Property 

Public Property Set Line(ByRef vLine As LineElement) 
    Set mLine = vLine 
End Property 
Cuestiones relacionadas