Pass Element Of User-Defined Data Type

  • Hi
    I have an array with structure, i.e. User Defined Type. Assume the user defined type has two elements: Element1 and Element2 and array name is Array.


    So the definition is:


    Code
    dim Array(1 to 10) as UserType


    and access to elements is

    Code
    Array(5).Element1


    The problem is that I need to pass the whole set of Element1 or Element2 to a function. Should it have been two separate arrays, it would not be a problem. But because of the user defined structure I have no idea how to pass a single element.


    I hope there is another solution rather than to use loops. I have many arrays like this with complex structures. I simply can not replicate all of them.

  • Re: Transform Structured Array To One Row Array


    A UDF like

    Code
    Function udTypeToArray(inputUserType as UserType) as Variant
      udTypetoArray= Array(inputUserType.Element1, inputUserType.Element2)
    End Function

    might be useful.

  • Re: Transform Structured Array To One Row Array


    Quote from Parsnip

    Hi


    Couldn't you simply pass the entire array but only process the Element1 (or Element2) of the constituents?


    Richard


    not really, because I have many arrays to which I need to apply the same function, i.e. I need to apply this function to Element1 or 2 from this array or 5 and 6 from another one or 15 and 20 from even another one

  • Re: Pass Element Of User-Defined Data Type


    Does something like this work for you?



    filippo

  • Re: Pass Element Of User-Defined Data Type


    Hi,


    Could you please explain what your application is trying to achieve and what is the design decision behind using arrays of UDT's. It seems to me that this could be achieved much better by a collection of UDT's or a class object.

  • Re: Pass Element Of User-Defined Data Type


    Explain it yes, but this thread MUST stick the topic of the Thread Title: Pass Element Of User-Defined Data Type .


    This is why we INSIST on those asking questions to NOT assume their means to an end and cleary state what their ultimate aim is.

  • Re: Pass Element Of User-Defined Data Type


    filippo,


    thanks for the effort but it does not solve the problem


    I need to write these arrays back to excel and becasue they are pretty big going through each cell takes a lot of time. So I want to write them back via range but for this I need an array, i.e.


    Code
    Range("A1").resize(x,y) = Array


    So what would like to have as Array is the whole set of Element1 or 2 or whatever else.


    I hope it clarifies the problem

  • Re: Pass Element Of User-Defined Data Type


    The problem here is your architecture. Think of user-defined type as an object (UserType) that has properties (Element1, Element2). The objects are made from the template that is your UDT, but the objects created from that template don't bear any relation to each other.


    To speed up writing the information back into a sheet, what you could do without having to rewrite your entire logic is to loop through your array "Array", writing each Element1's (or Element2's) property into a variant array, which will enable you to write it all into the sheet at once.


    It'll be far from efficient, but better than looping the values directly into the sheet. If you are open to a complete rewrite, I can give you some pointers how to actually make a sensible infrastructure.

  • Re: Pass Element Of User-Defined Data Type


    Fencliff,


    I am quite confident and comfortable about architecture. These arrays are time-series and they group various but still related properties into several big arrays. And these related properties are defined as user types.


    I see that the only way to go from here is to redesign the output by replicating arrays before writing them back to excel.


    thnx everybody

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!