Gabe Demarco
Gabe Demarco

Reputation: 3

Accessing an object property from within another object that contains it

I'll try to explain it to the best of my ability with my limited knowledge... I have a square matrix, same amount of rows and columns, i need to loop thru it and store the information of each cell in a certain way, i've done it previously with arrays and got everything working, but i'm trying to improve on my coding skills, and i've known for a while that objects are the way to go usually... this is my second attempt after quite a while of just not trying due to being defeated...

The thing is... I have 2 objects, one named Proveedor, this guy will have 3 properties:
Unidad Decisoria_Prov: a string
Responsable_Prov: a string
Requerimientos: an array of Vinculo objects

Private pedidos_() As Vinculo
Private ud_P As String
Private responsable_P As String
Option Explicit
Public Property Let Requerimientos(index As Integer, str As Vinculo)
    If index > UBound(pedidos_) Then ReDim Preserve pedidos_(index)
    If pedidos_(index) Is Nothing Then
        Set pedidos_(index) = New Vinculo
    End If
    
    Set pedidos_(index) = str
End Property
Public Property Get Requerimientos(index As Integer) As Vinculo
    If index > UBound(pedidos_) Then Requerimientos = "El indice del get esta fuera de rango": Exit Property

    Requerimientos = pedidos_(index)
    
End Property

Public Property Let Unidad_Decisoria_Prov(str As String)
    ud_P = str
End Property
Public Property Get Unidad_Decisoria_Prov() As String
    Unidad_Decisoria_Prov = ud_P
End Property

Public Property Let Responsable_Prov(str As String)
    responsable_P = str
End Property
Public Property Get Responsable_Prov() As String
    Responsable_Prov = responsable_P
End Property

Private Sub class_initialize()
    ReDim pedidos_(0)
End Sub

The other object is named Vinculo, this one has 3 properties:
Unidad Decisoria_C: a string
Responsable_C: a string
Requerimiento: a string

Private ud_ As String
Private responsable_ As String
Private requerimiento_ As String
Option Explicit

Public Property Let Unidad_Decisoria_C(str As String)
    ud_ = str
End Property
Public Property Get Unidad_Decisoria_C() As String
    Unidad_Decisoria_C = ud_
End Property

Public Property Let Responsable_C(str As String)
    responsable_ = str
End Property
Public Property Get Responsable_C() As String
    Responsable_C = responsable_
End Property

Public Property Let Requerimiento(str As String)
    requerimiento_ = str
End Property

Public Property Get Requerimiento() As String
    Requerimiento = requerimiento_
End Property

Once I have the objects working as intended i'll loop thru the matrix and do what i need to with the data, but before wasting time on that, i'm trying to test it with the following code:

Sub testing_2_objetosjuntos()
Dim mi_Prov As Proveedor
Dim un_vin As Vinculo

Set mi_Prov = New Proveedor
Set un_vin = New Vinculo

mi_Prov.Unidad_Decisoria_Prov = "tarea del proveedor 1"
mi_Prov.Responsable_Prov = "responsable de la tarea del proveedor 1"

un_vin.Unidad_Decisoria_C = "tarea del cliente 1"
un_vin.Responsable_C = "responsable de la tarea 1 del cliente"
un_vin.Requerimiento = "hace tal cosa"
mi_Prov.Requerimientos(0) = un_vin

Debug.Print mi_Prov.Requerimientos(0).Requerimiento
Debug.Print mi_Prov.Requerimientos(0).Responsable_C
End Sub

Up to the debug.print command everything works fine as far as i can tell... watches up until the first debug.print line

however when i try to access the properties of the vinculo object stored in the first index of the Proveedor object i get a beautiful

Run-time error 91: Object variable or With block variable not set

The actual line that gives me that error is the get property of Requerimientos in the Proveedor class.

This is probably a silly question but not only i can't understand why it breaks, i'm unable to apparently ask the question properly in google to not have to bother you guys...

I expected to read the string stored in the vinculo.Requerimiento property which is in the first index of the array of Requerimientos property of the Proveedor object.

Upvotes: 0

Views: 64

Answers (1)

FunThomas
FunThomas

Reputation: 29276

The reason for your problem is easy to fix. As Vinculo is an object, you need to use the Set keyword in the Requerimientos-getter:

Public Property Get Requerimientos(index As Integer) As Vinculo
    Set Requerimientos = pedidos_(index)
End Property

However, your error handling will fail: If index > UBound(pedidos_), you are assigning a String as return value which is not allowed (remember, your function returns an object of type Vinculo, not a String).
You should raise an error instead

Public Property Get Requerimientos(index As Integer) As Vinculo
    Err.Raise vbObjectError + 1000, "El indice del get esta fuera de rango", "Sorry..."
    Set Requerimientos = pedidos_(index)
End Property

And a hint: You had problems to identify the error of your test routine because the VBA runtime didn't halt on the line of error. This is because the default setting of the VBA debugger is "Break on unhandled errors" which doesn't include to break within a class module (hint: Userforms are also classes). Change this setting to "Break in class modules" to see the exact line where the error occurs.

Tools->Options->General

enter image description here

Upvotes: 1

Related Questions