return object instances

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

  1. #1

    Default return object instances

    hello,

    since I'm new to the world of object oriented programming I hope I use the
    right terms for the concerned things, but I'll try.
    I was wondering if returning instances of cfobjects as a return value of a
    method.
    What I did is create in a method of an object A an instance of another object
    B of a different type. Now I want to return that instance of A via <cfreturn>.
    I have seen that with other data-objects like query-objects there is a way of
    packing the data-object in an array, return the array and access the returned
    object from the returned array. This WORKS!
    But I run into another Problem. Later in the code I create an instance C of
    the same type as B. As soon as I set properties on instance C, instance B is
    changed and gets all properties from instance C although instance B is never
    touched after being returned with the array.
    Oh, and I forgot to mention, the problem arises not always but depending on
    what has been done to instance B inside the method of A.
    All that happens inside a component that itself is used as a web service.
    The underlying code is quite long and cryptic and I haven't created an example
    code with the same problem. Sorry about that. The real question is if what I'm
    doing is something one isn't supposed to do, if it is actually an error in the
    object-handling of CF 6.1 (which I'm using).

    Thanks
    Volker

    vollja Guest

  2. Similar Questions and Discussions

    1. Multiple app instances using same shared object.
      yeah you can Connect SharedObjects between instances of the same app and even instances of two diffrent apps. you need to use proxied sharedObjects...
    2. Object property with different return types
      I have a control 'MyControl' with a property of type 'MyObject'. MyObject has a property 'MyField' that is a enum type. I want the type of enum...
    3. Return object
      (Inspired by Test Driven Development of Kent Bek) How Can I return from a class method object of the class ? Like this: class Dollar attr_reader...
    4. Object Instances
      Another source told me a reason I'm goofing on my program (see 'Absurdly Easy Cocoa Question') is that I'm not calling instance methods on object...
    5. Object Instances: Never Mind
      Bleh, I'm a twit. After last post but before Mr. Ash's, I figured out my problem. I was referencing a class (ThermCalc is its name), not an outlet...
  3. #2

    Default Re: return object instances

    vollja wrote:
    > hello,
    >
    > since I'm new to the world of object oriented programming I hope I use the
    > right terms for the concerned things, but I'll try.
    > I was wondering if returning instances of cfobjects as a return value of a
    > method.
    > What I did is create in a method of an object A an instance of another object
    > B of a different type. Now I want to return that instance of A via <cfreturn>.
    > I have seen that with other data-objects like query-objects there is a way of
    > packing the data-object in an array, return the array and access the returned
    > object from the returned array. This WORKS!
    > But I run into another Problem. Later in the code I create an instance C of
    > the same type as B. As soon as I set properties on instance C, instance B is
    > changed and gets all properties from instance C although instance B is never
    > touched after being returned with the array.
    > Oh, and I forgot to mention, the problem arises not always but depending on
    > what has been done to instance B inside the method of A.
    > All that happens inside a component that itself is used as a web service.
    > The underlying code is quite long and cryptic and I haven't created an example
    > code with the same problem. Sorry about that. The real question is if what I'm
    > doing is something one isn't supposed to do, if it is actually an error in the
    > object-handling of CF 6.1 (which I'm using).
    >
    > Thanks
    > Volker
    >
    You'd really have to post some code for us to see what you're
    doing--based on what you're describing, however, I bet CF is behaving as
    it's supposed to and you're inadvertently doing things that are causing
    unexpected side effects.

    Matt

    --
    Matt Woodward
    [email]mpwoodward@gmail.com[/email]
    Team Macromedia - ColdFusion
    mpwoodward *TMM* Guest

  4. #3

    Default Re: return object instances

    Hi,

    here is some code. I haven't tried this one yet. Though I doubt it will
    actually show the effect. The original code has alltogether about 3000 lines,
    gets XML data as parameter of a web service and feeds a database. I doubt,
    anyone would be willing to read it!

    Volker


    <cfcomponent output="yes">
    <cffunction name="parent" access="public" returntype="string">
    <cfreturn 'not interesting'>
    <cfobject name="instA" component="A">
    <cfobject name="instC" component="B">
    <cfset arr_result = instA.foo()>
    <!--- So far everything all right --->
    <cfdump var="#arr_result#">
    <cfset instC.propB = 2>
    <!--- Now the result looks different --->
    <cfdump var="#arr_result#">
    <cfreturn 'not interesting'>
    </cffunction>
    </cfcomponent>

    <!--- Component A --->
    <cfcomponent output="yes">
    <cfset this.propA = 0>
    <cffunction name="foo" output="true" returntype="array">
    <cfobject name="instB" component="B">
    <cfset instB.propB = 1>
    <cfset array = arraynew(1)>
    <cfset result = arrayappend(array, instB)>
    <cfreturn array>
    </cffunction>
    </cfcomponent>

    <!--- Component B --->
    <cfcomponent output="yes">
    <cfset this.propB = 0>
    </cfcomponent>

    vollja Guest

  5. #4

    Default Re: return object instances

    vollja wrote:
    > Hi,
    >
    > here is some code. I haven't tried this one yet. Though I doubt it will
    > actually show the effect. The original code has alltogether about 3000 lines,
    > gets XML data as parameter of a web service and feeds a database. I doubt,
    > anyone would be willing to read it!
    >
    > Volker
    >
    >
    > <cfcomponent output="yes">
    > <cffunction name="parent" access="public" returntype="string">
    > <cfreturn 'not interesting'>
    > <cfobject name="instA" component="A">
    > <cfobject name="instC" component="B">
    > <cfset arr_result = instA.foo()>
    > <!--- So far everything all right --->
    > <cfdump var="#arr_result#">
    > <cfset instC.propB = 2>
    > <!--- Now the result looks different --->
    > <cfdump var="#arr_result#">
    > <cfreturn 'not interesting'>
    > </cffunction>
    > </cfcomponent>
    >
    > <!--- Component A --->
    > <cfcomponent output="yes">
    > <cfset this.propA = 0>
    > <cffunction name="foo" output="true" returntype="array">
    > <cfobject name="instB" component="B">
    > <cfset instB.propB = 1>
    > <cfset array = arraynew(1)>
    > <cfset result = arrayappend(array, instB)>
    > <cfreturn array>
    > </cffunction>
    > </cfcomponent>
    >
    > <!--- Component B --->
    > <cfcomponent output="yes">
    > <cfset this.propB = 0>
    > </cfcomponent>
    >
    Given the fact that there is nothing in this code (that I can see) that
    changes arr_result between the first dump and the second one, either
    there's something going on that isn't illustrated here or the variable
    actually doesn't get changed. The call to instA.foo() sets arr_result.
    Then you do instC.propB = 2. That has *nothing* to do with arr_result
    from what I can see here, so I don't see how the next time you output it
    it would be different.

    Matt

    --
    Matt Woodward
    [email]mpwoodward@gmail.com[/email]
    Team Macromedia - ColdFusion
    mpwoodward *TMM* Guest

  6. #5

    Default Re: return object instances

    Matt wrote:
    >Given the fact that there is nothing in this code (that I can see) that
    >changes arr_result between the first dump and the second one, either
    >there's something going on that isn't illustrated here or the variable
    >actually doesn't get changed. The call to instA.foo() sets arr_result.
    > Then you do instC.propB = 2. That has *nothing* to do with arr_result
    >from what I can see here, so I don't see how the next time you output it
    >it would be different.
    >
    >Matt
    well, that's exactly what I was wondering about as well. Everything is o.k.
    until something completely different happens which should not really interfere.

    Volker


    vollja Guest

  7. #6

    Default Re: return object instances

    Remember that you have 3 possible variable scopes from w/i your object.

    1) 'var' = private to function
    2) 'variables' = private to component
    3) 'this' = public to all

    Use the 'var' scope if nothing outside the function needs access to the
    variable. Use 'variables' if only other functions w/i the component need
    access. Use 'this'...well...um, I don't ever use it.

    eg) <cffunction name="parent" access="public" returntype="string">
    <!--- Only variables w/i my function can access this --->
    <cfset var instC = createObject("component", "A")>

    <!--- Only variables w/i my component can access this --->
    <cfset variables.instC = createObject("component", "A")>

    <!--- Everybody can access this --->
    <cfset this.instC = createObject("component", "A")>
    </cffunction>

    Scoping all variables is a good way to code and helps prevent you from
    inadvertenely making mistakes.


    BSterner Guest

  8. #7

    Default Re: return object instances

    I know there were some bugs w/the 'variables' scope and also using 'var' and then <cfinclude>, but from what I've read they were fixed in the newer versions of CF.
    BSterner Guest

  9. #8

    Default Re: return object instances

    BSterner wrote:
    > Remember that you have 3 possible variable scopes from w/i your object.
    >
    > 1) 'var' = private to function
    > 2) 'variables' = private to component
    > 3) 'this' = public to all
    >
    > Use the 'var' scope if nothing outside the function needs access to the
    > variable. Use 'variables' if only other functions w/i the component need
    > access. Use 'this'...well...um, I don't ever use it.
    Good points--I didn't want to get the discussion to get too complex too
    quickly, but that actually is the first thing that came to my mind as
    well. You need to var scope EVERYTHING inside your CFC functions, and
    I'm with BSterner, I never use the this scope for anything.

    Matt

    --
    Matt Woodward
    [email]mpwoodward@gmail.com[/email]
    Team Macromedia - ColdFusion
    mpwoodward *TMM* 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