[allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => [ref] => [htmlstate] => on_nl2br [postusername] => vollja [ip] => webforumsuser@m [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 3 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> > > > > > > > > > > > > > > > > > > > > > > > > >[/quote] 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 [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => [ref] => [htmlstate] => on_nl2br [postusername] => mpwoodward *TMM* [ip] => mpwoodward@gmai [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 4 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> Scoping all variables is a good way to code and helps prevent you from inadvertenely making mistakes. [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => [ref] => [htmlstate] => on_nl2br [postusername] => BSterner [ip] => joenobody720@ex [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 6 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> return object instances - Coldfusion - Advanced Techniques

return object instances - Coldfusion - Advanced Techniques

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 ...

  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. #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[/email]
    Team Macromedia - ColdFusion
    mpwoodward *TMM* Guest

  3. #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

  4. #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[/email]
    Team Macromedia - ColdFusion
    mpwoodward *TMM* Guest

  5. #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

  6. #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

  7. #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

  8. #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[/email]
    Team Macromedia - ColdFusion
    mpwoodward *TMM* Guest

Similar Threads

  1. Multiple app instances using same shared object.
    By Berzy in forum Macromedia Flash Flashcom
    Replies: 0
    Last Post: February 18th, 11:26 AM
  2. Return object
    By Dmitry N Orlov in forum Ruby
    Replies: 4
    Last Post: November 10th, 08:18 PM
  3. Can a WebService return XmlDoent object?
    By Daniel in forum ASP.NET Web Services
    Replies: 1
    Last Post: July 14th, 02:38 PM
  4. Object Instances
    By Will Oram in forum Mac Programming
    Replies: 5
    Last Post: July 12th, 03:31 AM
  5. Object Instances: Never Mind
    By Will Oram in forum Mac Programming
    Replies: 0
    Last Post: July 8th, 05:14 PM

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
  •