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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With