Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating objects within another class in Excel VBA

I'm working on a project using Excel VBA where I have a number of datasets, each populated with a number of 'patients' which have a number of parameters (such as treatment, outcome etc.). To handle this, I intend to create a class called 'patient', with properties such as treatment and outcome. Then create a class called 'dataset', with a public property of 'patient'. I've created the classes, and I can instantiate a dataset object. But how do I go about instantiating patient object, or ideally an array of patient objects within the dataset object?

Dataset class module:

Private pNumber As Integer
Public Patient As Patient

Public Property Get Number() As Integer
    Number = pNumber
End Property
Public Property Let Number(p As Integer)
    pNumber = p
End Property

Patient class module:

Private pID As Integer
Private pTreatment As Boolean
Private pResponse As Single

Public Property Get ID() As Integer
    ID = pID
End Property
Public Property Let ID(p As Integer)
    pID = p
End Property

Public Property Get Treatment() As Boolean
    Treatment = pTreatment
End Property
Public Property Let Treatment(p As Boolean)
    pTreatment = p
End Property

Public Property Get Response() As Single
    Response = pResponse
End Property
Public Property Let Response(p As Single)
    pResponse = p
End Property

Main Module

Sub main()

Dim data1 As Dataset
Set data1 = New Dataset

'code to instantiate array of patient within data1 here

End Sub
like image 504
Alex Wilding Avatar asked Oct 30 '22 09:10

Alex Wilding


1 Answers

The dataset will be better appointed something like this.

I would call the class what it is intended to be, so Patients:

private colPatients as new collection

public function add(aPatient as patient)
  colPatients.add aPatient, aPatient.Id
end function

public property get count() as long
  count = colPatients.count
end property 

public property get items() as collection
  set items = colPatients
end property 

public property get item(vItem as variant) as patient
  set item = colPatients(vItem)
end property 

public sub remove(vItem as variant)
  colPatients.remove vItem
end sub 

So to use:

dim patientCollection as patients
Sub main()

Set patientCollection = New patients

'code to instantiate array of patient within data1 here
IDs = Array(1,2,3)
treats = array("x","y","z")

dim x as integer
dim p as patient
for x = lbound(IDs) to Ubound(IDs)
   set p= new patient
   p.id = IDs(x)
   p.treatment = treats(x)
   patientCollection.add p
   set p = nothing
next x


End Sub
like image 168
whytheq Avatar answered Nov 09 '22 11:11

whytheq