Assigning structures to arrays

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Assigning structures to arrays

    Can somebody explain to me why this is happening? I don't get it.... I'm
    trying to create an array of structures using the code below. Interestingly
    enough, clearing the structure in line 8 of the code clears the structure that
    was already assigned to position 1 of the array. I figured that, after
    assigning it as another variable, that clearing the structure would only affect
    the variables.field structure. I'm building a global data validation CFC and
    want to be able to pass it an array of structure with the data needed to
    validate each field. Can anybody help me out with another solution for this
    without having to create field1, field2, field3, etc. structures???

    <cfset arrFields = arrayNew(1)>
    <cfset field = structNew()>
    <cfset field.name = "fieldname">
    <cfset field.type = "numeric">
    <cfset field.minLen = "2">
    <cfset field.maxLen = "10">
    <cfset arrFields[1] = field>
    <cfset structClear(field)>
    <cfset field.name = "fieldname2">
    <cfset field.type = "date">
    <cfset arrFields[2]= field>
    <cfdump var="#arrFields#">

    JonathanBigelow Guest

  2. Similar Questions and Discussions

    1. Nesting Arrays within arrays.
      How do i nest an array inside another array? something like array(i).aray2(j); Can I do that?
    2. Arrays/Structures in CFLOOP
      From the code below you can see basically what I am trying to accomplish: to set up a CFLOOP from an earlier query, an insert some permutation of...
    3. Shopping cart with arrays and structures
      Hello all- I'm only on line 3 of building a shopping cart, and it already has issues. I'm following online instructions on how to build a shopping...
    4. Assigning Numbers
      I am relatively new to FM and am trying to figure out a solution to this problem; A database has x number of records all with the assigned number...
    5. assigning recordsource to tab
      "Jim" <creveille@excite.com> wrote in message news:0a3d01c35527$a4d95d60$a601280a@phx.gbl... Using the Change event of the TabControl and...
  3. #2

    Default Re: Assigning structures to arrays

    Instead of:

    <cfset arrFields[1] = field>

    Use:

    <cfset arrFields[1] = StructCopy(field)>

    When you assign the structure to that array location, you are just using
    pointer to the structure. What you really want is a separate copy, so that you
    are reuse it.


    blewis Guest

  4. #3

    Default Re: Assigning structures to arrays

    > <cfset arrFields[1] = StructCopy(field)>
    >
    > When you assign the structure to that array location, you are just using
    > pointer to the structure. What you really want is a separate copy, so that you
    > are reuse it.
    A caveat to this:
    structCopy() only deep copies the first "level" of structures, nested
    structs are still copied by reference.

    duplicate() deep copies the whole lot. Actually it does too good a job of
    doing this and is actually in itself buggy, but generally speaking it's
    what you want to use instead of structCopy().

    In the specificed example, structCopy() would indeed work fine, though, as
    the structs contain no other structs.

    --

    Adam
    Adam Cameron Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139