+ Reply to Thread
Results 1 to 3 of 3

"Type" statement and arrays

  1. #1

    "Type" statement and arrays

    I am having difficulty getting a user defined data type to function
    with arrays. What I would like to do is create a data type that is
    comprised of four 1-D arrays. Then I would like to load the individual
    arrays in another module so I can trasnfer all four arrays as a single
    argument (the new data type) in a function. Here's what I have done so
    far

    In one module;

    Option Base 1

    Public Type PlotScalars

    Xscalar(1 To 4) As Integer
    Yscalar(1 To 4) As Integer
    Zscalar(1 To 4) As Integer

    End Type

    In another module

    Dim MyPlotScalars As PlotScalars

    MyPlotScalars.Xscalar = Array(1, 0, 1, 1)
    MyPlotScalars.Yscalar = Array(1, 1, 0, 1)
    MyPlotScalars.Zscalar = Array(1, 1, 1, 0)

    I get an error "Compile error: can't assign to array." I would prefer
    not to have to load each element of the data type arrays one element at
    a time. Is there an easy way around this?

    My main motivation for doing this is to cut down on the number of
    arguments passed to a user defined function (which are limited to 30).

    TIA

    dan


  2. #2
    AA2e72E
    Guest

    RE: "Type" statement and arrays

    Try it like this:

    Public Type PlotScalars

    Xscalar As Variant
    Yscalar As Variant
    Zscalar As Variant

    End Type

    Sub xx()
    Dim MyPlotScalars As PlotScalars

    MyPlotScalars.Xscalar = Array(1, 0, 1, 1)
    MyPlotScalars.Yscalar = Array(1, 1, 0, 1)
    MyPlotScalars.Zscalar = Array(1, 1, 1, 0)
    End Sub

    Then you can refer to the indivisual elements of each member as follows:

    myplotscalars.Xscalar(1)

    The assignment must be in block ie. not by element of member

    "[email protected]" wrote:

    > I am having difficulty getting a user defined data type to function
    > with arrays. What I would like to do is create a data type that is
    > comprised of four 1-D arrays. Then I would like to load the individual
    > arrays in another module so I can trasnfer all four arrays as a single
    > argument (the new data type) in a function. Here's what I have done so
    > far
    >
    > In one module;
    >
    > Option Base 1
    >
    > Public Type PlotScalars
    >
    > Xscalar(1 To 4) As Integer
    > Yscalar(1 To 4) As Integer
    > Zscalar(1 To 4) As Integer
    >
    > End Type
    >
    > In another module
    >
    > Dim MyPlotScalars As PlotScalars
    >
    > MyPlotScalars.Xscalar = Array(1, 0, 1, 1)
    > MyPlotScalars.Yscalar = Array(1, 1, 0, 1)
    > MyPlotScalars.Zscalar = Array(1, 1, 1, 0)
    >
    > I get an error "Compile error: can't assign to array." I would prefer
    > not to have to load each element of the data type arrays one element at
    > a time. Is there an easy way around this?
    >
    > My main motivation for doing this is to cut down on the number of
    > arguments passed to a user defined function (which are limited to 30).
    >
    > TIA
    >
    > dan
    >
    >


  3. #3
    Myrna Larson
    Guest

    Re: "Type" statement and arrays

    When you use the ARRAY statement, the variable on the left side of the
    assignment statement must be a VARIANT, and you end up with a VARIANT
    containing an array, i.e. TypeName returns Variant(). Even though you access
    the elements of this array with syntax identical to what you use with a
    "normal" array (created with Dim X() As Integer, etc), they are not the same
    data type.

    And even though the elements of that array contain integers, they are in fact
    stored as variants.

    If you want to use the TYPE definition that you posted, then in the other sub
    you must write

    Dim x(1 to 4) AS Integer
    X(1) = 1
    X(2) = 0
    X(3) = 1
    X(4) = 1
    MyPlotScalers.XScalar = X()

    then repeat the 4 assignment statements for the next set of values. i.e. you
    have to assign the values the "old fashioned" way, with a loop, not with an
    Array statement.

    The less painful "fix" is to change the type statement to make the 3 arrays
    variants, as the other responder suggests, i.e.

    Public Type PlotScalars
    Xscalar As Variant
    Yscalar As Variant
    Zscalar As Variant
    End Type


    On 11 Feb 2005 06:14:16 -0800, [email protected] wrote:

    >I am having difficulty getting a user defined data type to function
    >with arrays. What I would like to do is create a data type that is
    >comprised of four 1-D arrays. Then I would like to load the individual
    >arrays in another module so I can trasnfer all four arrays as a single
    >argument (the new data type) in a function. Here's what I have done so
    >far
    >
    >In one module;
    >
    >Option Base 1
    >
    >Public Type PlotScalars
    >
    > Xscalar(1 To 4) As Integer
    > Yscalar(1 To 4) As Integer
    > Zscalar(1 To 4) As Integer
    >
    >End Type
    >
    >In another module
    >
    >Dim MyPlotScalars As PlotScalars
    >
    >MyPlotScalars.Xscalar = Array(1, 0, 1, 1)
    >MyPlotScalars.Yscalar = Array(1, 1, 0, 1)
    >MyPlotScalars.Zscalar = Array(1, 1, 1, 0)
    >
    >I get an error "Compile error: can't assign to array." I would prefer
    >not to have to load each element of the data type arrays one element at
    >a time. Is there an easy way around this?
    >
    >My main motivation for doing this is to cut down on the number of
    >arguments passed to a user defined function (which are limited to 30).
    >
    >TIA
    >
    >dan



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1