0

I'm having some trouble of understanding on how to make my own type object. I have two different classes and I would like that my list type to be given the object of the data type.

data Class: (Be aware that this is just a snippet of the code)

Private index As Integer
Private id As String
Private nameID() As String
Private data() As String
Private cnt As Integer

Public Function constructor(value As Integer)
    index = 0
    cnt = value
    id = ""
    ReDim nameID(0 To cnt)
    ReDim data(0 To cnt)
End Function

Property Let setName(value As String)
    nameID(index) = value
End Property

Property Let setData(value As String)
    data(index) = value
    index = index + 1
End Property

Property Get getName()
    getName = nameID
End Property

Property Get getData()
    getData = data
End Property

list Class:

Private xArray() As dataStruct
Private index As Integer

Public Function constructor(cnt As Integer)
    ReDim xArray(1 To cnt)
    Dim num As Integer
    For num = 1 To cnt
    Set xArray(num) = New dataStruct
    'xArray(num).constructor(10)
    Next
    index = 1
End Function

So the problem I have occurs when I try to get the values from my data class to my list class. I have tried to set them equal each other and assign each value separately but nothing seems to work. I get the "Run-time error '438: Object doesn't support this property or method'. If I try to use the commented code I get past the setID but when entering setName it comes a type mismatch but it should be an array of type string.

Property Let addArray(value As dataStruct)
    xArray(index) = value
    'xArray(index).setID = value.getID
    'xArray(index).setName = value.getName
    'xArray(index).setData = value.getData
    'xArray(index).setCnt = value.getCnt
    index = index + 1
End Property

list

[Edited: Added example of get and set methods]

Community
  • 1
  • 1
Fredrik
  • 477
  • 4
  • 22
  • Not enough code - it would help to show at least one pair of get/set methods... – Tim Williams Jun 20 '18 at 05:57
  • `Set xArray(index) = value` since this is an object you're assigning here. No need to populate xArray with empty dataStruct's inside `constructor` – Tim Williams Jun 20 '18 at 05:59
  • @TimWilliams Added two examples of get and set – Fredrik Jun 20 '18 at 06:06
  • @TimWilliams Thanks! Thought I did that at the beginning but must have missed testing it again after changing the constructor. But it all works now! Do you know another way to create a constructor too, right now it feels primitive to call it with "dData.constructor(100)" but I don't know any other way to do it? – Fredrik Jun 20 '18 at 06:10
  • There's no built-in constructor which can have arguments (Initialize doesn't have any). I typically use a Sub called `Init()` if I need something like that. See also: https://stackoverflow.com/questions/15224113/pass-arguments-to-constructor-in-vba – Tim Williams Jun 20 '18 at 06:12

1 Answers1

0

Simply add a method into each Class that either outputs as the other class, or takes an input as the other class. You haven't listed any properties, so I am going to guess how you get values out.

For example, in Data Class

Sub ConvertFromList(values as List)
    Redim data (0 to values.data.count)
    For iterator = 0 to values.data.count
        data(itearator) = values.data(iterator)
    Next iterator
End Sub

My example is not pretty, nor complete - but not much information was provided to start with. But it does give you an idea what can be done.

A neat solution will be a Function within the Class that exports as Data/List as the case may be.

AJD
  • 2,400
  • 2
  • 12
  • 22