Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Office XP, SP3 (VBA 6.0)
WIN XP, SP2 I have read over the text dealing with custom objects According to the text that I have read online from MS themselves, the keyword "NOTHING" should only destroy the object when there is no other variable refering to that object. However, as I have learned the hard way, that is not the case. Any time any variable is set to NOTHING, the object is destroyed, so any object variable referencing to that object is then also set to NOTHING. Example: PayCode collection has a set of objects containing properties. One of those PayCode objects is passed onto an Employee Object. It's passed into the employee object via a property with the object using the ByRef within the signature line, so if there's any changes to the PayCode object, it's not only changed within the PayCodes collection, but also within the Employee's PayCode reference to it. However, if the Employee Object is set to be destroyed, via the clean up code, the code should be to set the object variable within the Employee Object to NOTHING. However, doing that not only does that, but also set's that particular object within the PayCodes collection to NOTHING as if the object itself has been destroyed. How do I resolve this issue, so as I can have it reset only the variable, not destroy the actual object, but yet, also allow for changes to carry into the variable when they are done into the main object? -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Afraid I don't follow most of that, maybe a simple example might help.
One thing though, I think you are misunderstanding the difference of passing an object byref & byval. Either way, any changes to the object's properties will persist. The only difference is with byval a new pointer to the original object is created and passed. This means even if you destroy or reassign the byval object within the procedure, the original object variable will continue point to the same object. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... Office XP, SP3 (VBA 6.0) WIN XP, SP2 I have read over the text dealing with custom objects According to the text that I have read online from MS themselves, the keyword "NOTHING" should only destroy the object when there is no other variable refering to that object. However, as I have learned the hard way, that is not the case. Any time any variable is set to NOTHING, the object is destroyed, so any object variable referencing to that object is then also set to NOTHING. Example: PayCode collection has a set of objects containing properties. One of those PayCode objects is passed onto an Employee Object. It's passed into the employee object via a property with the object using the ByRef within the signature line, so if there's any changes to the PayCode object, it's not only changed within the PayCodes collection, but also within the Employee's PayCode reference to it. However, if the Employee Object is set to be destroyed, via the clean up code, the code should be to set the object variable within the Employee Object to NOTHING. However, doing that not only does that, but also set's that particular object within the PayCodes collection to NOTHING as if the object itself has been destroyed. How do I resolve this issue, so as I can have it reset only the variable, not destroy the actual object, but yet, also allow for changes to carry into the variable when they are done into the main object? -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
The way I understood the ByRef and ByVal are the following:
ByRef - References to the object, and any changes done to the object is done for any variable referencing to that object, rather it be done in the original code or in the code referencing to the object. ByVal - Creates a whole new replica of the object with all of the same settings as the object that is being passed onto it, but any changes done to the original object is not reflects in the new object, just like any changes done in the new object is not reflected in the original object. Though I haven't fully tested it to be sure with object variables, but simple data type variables does work like this. -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 "Peter T" <peter_t@discussions wrote in message ... Afraid I don't follow most of that, maybe a simple example might help. One thing though, I think you are misunderstanding the difference of passing an object byref & byval. Either way, any changes to the object's properties will persist. The only difference is with byval a new pointer to the original object is created and passed. This means even if you destroy or reassign the byval object within the procedure, the original object variable will continue point to the same object. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... Office XP, SP3 (VBA 6.0) WIN XP, SP2 I have read over the text dealing with custom objects According to the text that I have read online from MS themselves, the keyword "NOTHING" should only destroy the object when there is no other variable refering to that object. However, as I have learned the hard way, that is not the case. Any time any variable is set to NOTHING, the object is destroyed, so any object variable referencing to that object is then also set to NOTHING. Example: PayCode collection has a set of objects containing properties. One of those PayCode objects is passed onto an Employee Object. It's passed into the employee object via a property with the object using the ByRef within the signature line, so if there's any changes to the PayCode object, it's not only changed within the PayCodes collection, but also within the Employee's PayCode reference to it. However, if the Employee Object is set to be destroyed, via the clean up code, the code should be to set the object variable within the Employee Object to NOTHING. However, doing that not only does that, but also set's that particular object within the PayCodes collection to NOTHING as if the object itself has been destroyed. How do I resolve this issue, so as I can have it reset only the variable, not destroy the actual object, but yet, also allow for changes to carry into the variable when they are done into the main object? -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
ByVal - Creates a whole new replica of the object with all of the same
settings as the object that is being passed onto it No it doesn't. It creates a new (temporary) pointer to the original intrinsic object. As far as the original object's properties are concerned there is no difference between using ByRef and ByVal. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... The way I understood the ByRef and ByVal are the following: ByRef - References to the object, and any changes done to the object is done for any variable referencing to that object, rather it be done in the original code or in the code referencing to the object. ByVal - Creates a whole new replica of the object with all of the same settings as the object that is being passed onto it, but any changes done to the original object is not reflects in the new object, just like any changes done in the new object is not reflected in the original object. Though I haven't fully tested it to be sure with object variables, but simple data type variables does work like this. -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 "Peter T" <peter_t@discussions wrote in message ... Afraid I don't follow most of that, maybe a simple example might help. One thing though, I think you are misunderstanding the difference of passing an object byref & byval. Either way, any changes to the object's properties will persist. The only difference is with byval a new pointer to the original object is created and passed. This means even if you destroy or reassign the byval object within the procedure, the original object variable will continue point to the same object. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... Office XP, SP3 (VBA 6.0) WIN XP, SP2 I have read over the text dealing with custom objects According to the text that I have read online from MS themselves, the keyword "NOTHING" should only destroy the object when there is no other variable refering to that object. However, as I have learned the hard way, that is not the case. Any time any variable is set to NOTHING, the object is destroyed, so any object variable referencing to that object is then also set to NOTHING. Example: PayCode collection has a set of objects containing properties. One of those PayCode objects is passed onto an Employee Object. It's passed into the employee object via a property with the object using the ByRef within the signature line, so if there's any changes to the PayCode object, it's not only changed within the PayCodes collection, but also within the Employee's PayCode reference to it. However, if the Employee Object is set to be destroyed, via the clean up code, the code should be to set the object variable within the Employee Object to NOTHING. However, doing that not only does that, but also set's that particular object within the PayCodes collection to NOTHING as if the object itself has been destroyed. How do I resolve this issue, so as I can have it reset only the variable, not destroy the actual object, but yet, also allow for changes to carry into the variable when they are done into the main object? -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Where does it state this pointer bit within the documentations? All I see
in the documentations are by references for ByRef and a copy of the variable with ByVal looking at different places. Also, how is it that I know it will work out the same way with my custom objects similar to how things works out with the objects built into the application itself such as Worksheet object as the Worksheet object is directly tied to the WorkSheets collection object? -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 "Peter T" <peter_t@discussions wrote in message ... ByVal - Creates a whole new replica of the object with all of the same settings as the object that is being passed onto it No it doesn't. It creates a new (temporary) pointer to the original intrinsic object. As far as the original object's properties are concerned there is no difference between using ByRef and ByVal. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... The way I understood the ByRef and ByVal are the following: ByRef - References to the object, and any changes done to the object is done for any variable referencing to that object, rather it be done in the original code or in the code referencing to the object. ByVal - Creates a whole new replica of the object with all of the same settings as the object that is being passed onto it, but any changes done to the original object is not reflects in the new object, just like any changes done in the new object is not reflected in the original object. Though I haven't fully tested it to be sure with object variables, but simple data type variables does work like this. -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 "Peter T" <peter_t@discussions wrote in message ... Afraid I don't follow most of that, maybe a simple example might help. One thing though, I think you are misunderstanding the difference of passing an object byref & byval. Either way, any changes to the object's properties will persist. The only difference is with byval a new pointer to the original object is created and passed. This means even if you destroy or reassign the byval object within the procedure, the original object variable will continue point to the same object. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... Office XP, SP3 (VBA 6.0) WIN XP, SP2 I have read over the text dealing with custom objects According to the text that I have read online from MS themselves, the keyword "NOTHING" should only destroy the object when there is no other variable refering to that object. However, as I have learned the hard way, that is not the case. Any time any variable is set to NOTHING, the object is destroyed, so any object variable referencing to that object is then also set to NOTHING. Example: PayCode collection has a set of objects containing properties. One of those PayCode objects is passed onto an Employee Object. It's passed into the employee object via a property with the object using the ByRef within the signature line, so if there's any changes to the PayCode object, it's not only changed within the PayCodes collection, but also within the Employee's PayCode reference to it. However, if the Employee Object is set to be destroyed, via the clean up code, the code should be to set the object variable within the Employee Object to NOTHING. However, doing that not only does that, but also set's that particular object within the PayCodes collection to NOTHING as if the object itself has been destroyed. How do I resolve this issue, so as I can have it reset only the variable, not destroy the actual object, but yet, also allow for changes to carry into the variable when they are done into the main object? -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"Ronald R. Dodge, Jr." wrote in message
Where does it state this pointer bit within the documentations? All I see in the documentations are by references for ByRef and a copy of the variable with ByVal looking at different places. Without searching I have no idea. Let me throw the question back at you - where is something like the following documented: " ByVal - Creates a whole new replica of the object with all of the same settings as the object that is being passed onto it" I'd be surprised if you find that anywhere. Also, how is it that I know it will work out the same way with my custom objects similar to how things works out with the objects built into the application itself such as Worksheet object as the Worksheet object is directly tied to the WorkSheets collection object? I don't know anything about your custom obects but I would have thought you are best placed to demonstrate for yourself. Maybe the following will demonstrate that ByVal and ByRef work with the same intrinsic object, and that in the case of ByVal merely a new pointer to the same object is created. Sub test4() Dim c As Class1 Set c = New Class1 proc c, c MsgBox c.x & vbCr & c.y & vbCr & ObjPtr(c) ' all same End Sub Sub proc(ByRef cr As Class1, ByVal cv As Class1) cr.x = ObjPtr(cr) cv.y = ObjPtr(cv) Set cv = New Class1 cv.y = 123 MsgBox cv.y End Sub '' in class1 Public x As Long, y As Long Regards, Peter T "Peter T" <peter_t@discussions wrote in message ... ByVal - Creates a whole new replica of the object with all of the same settings as the object that is being passed onto it No it doesn't. It creates a new (temporary) pointer to the original intrinsic object. As far as the original object's properties are concerned there is no difference between using ByRef and ByVal. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... The way I understood the ByRef and ByVal are the following: ByRef - References to the object, and any changes done to the object is done for any variable referencing to that object, rather it be done in the original code or in the code referencing to the object. ByVal - Creates a whole new replica of the object with all of the same settings as the object that is being passed onto it, but any changes done to the original object is not reflects in the new object, just like any changes done in the new object is not reflected in the original object. Though I haven't fully tested it to be sure with object variables, but simple data type variables does work like this. -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 "Peter T" <peter_t@discussions wrote in message ... Afraid I don't follow most of that, maybe a simple example might help. One thing though, I think you are misunderstanding the difference of passing an object byref & byval. Either way, any changes to the object's properties will persist. The only difference is with byval a new pointer to the original object is created and passed. This means even if you destroy or reassign the byval object within the procedure, the original object variable will continue point to the same object. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... Office XP, SP3 (VBA 6.0) WIN XP, SP2 I have read over the text dealing with custom objects According to the text that I have read online from MS themselves, the keyword "NOTHING" should only destroy the object when there is no other variable refering to that object. However, as I have learned the hard way, that is not the case. Any time any variable is set to NOTHING, the object is destroyed, so any object variable referencing to that object is then also set to NOTHING. Example: PayCode collection has a set of objects containing properties. One of those PayCode objects is passed onto an Employee Object. It's passed into the employee object via a property with the object using the ByRef within the signature line, so if there's any changes to the PayCode object, it's not only changed within the PayCodes collection, but also within the Employee's PayCode reference to it. However, if the Employee Object is set to be destroyed, via the clean up code, the code should be to set the object variable within the Employee Object to NOTHING. However, doing that not only does that, but also set's that particular object within the PayCodes collection to NOTHING as if the object itself has been destroyed. How do I resolve this issue, so as I can have it reset only the variable, not destroy the actual object, but yet, also allow for changes to carry into the variable when they are done into the main object? -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 |
#7
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
As far as I can tell, what you are saying only works with intrinsic objects.
Given my custom objects are within a project, not part of the application itself, but the very definition of intrinsic objects, my custom objects are NOT intrinsic objects, thus your argument for how the ByVal works would not work out. -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 "Peter T" <peter_t@discussions wrote in message ... ByVal - Creates a whole new replica of the object with all of the same settings as the object that is being passed onto it No it doesn't. It creates a new (temporary) pointer to the original intrinsic object. As far as the original object's properties are concerned there is no difference between using ByRef and ByVal. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... The way I understood the ByRef and ByVal are the following: ByRef - References to the object, and any changes done to the object is done for any variable referencing to that object, rather it be done in the original code or in the code referencing to the object. ByVal - Creates a whole new replica of the object with all of the same settings as the object that is being passed onto it, but any changes done to the original object is not reflects in the new object, just like any changes done in the new object is not reflected in the original object. Though I haven't fully tested it to be sure with object variables, but simple data type variables does work like this. -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 "Peter T" <peter_t@discussions wrote in message ... Afraid I don't follow most of that, maybe a simple example might help. One thing though, I think you are misunderstanding the difference of passing an object byref & byval. Either way, any changes to the object's properties will persist. The only difference is with byval a new pointer to the original object is created and passed. This means even if you destroy or reassign the byval object within the procedure, the original object variable will continue point to the same object. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... Office XP, SP3 (VBA 6.0) WIN XP, SP2 I have read over the text dealing with custom objects According to the text that I have read online from MS themselves, the keyword "NOTHING" should only destroy the object when there is no other variable refering to that object. However, as I have learned the hard way, that is not the case. Any time any variable is set to NOTHING, the object is destroyed, so any object variable referencing to that object is then also set to NOTHING. Example: PayCode collection has a set of objects containing properties. One of those PayCode objects is passed onto an Employee Object. It's passed into the employee object via a property with the object using the ByRef within the signature line, so if there's any changes to the PayCode object, it's not only changed within the PayCodes collection, but also within the Employee's PayCode reference to it. However, if the Employee Object is set to be destroyed, via the clean up code, the code should be to set the object variable within the Employee Object to NOTHING. However, doing that not only does that, but also set's that particular object within the PayCodes collection to NOTHING as if the object itself has been destroyed. How do I resolve this issue, so as I can have it reset only the variable, not destroy the actual object, but yet, also allow for changes to carry into the variable when they are done into the main object? -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 |
#8
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Strange, I didn't see this when I followed up to your earlier message.
Anyway I trust the example in the follow-up clarifies. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... As far as I can tell, what you are saying only works with intrinsic objects. Given my custom objects are within a project, not part of the application itself, but the very definition of intrinsic objects, my custom objects are NOT intrinsic objects, thus your argument for how the ByVal works would not work out. -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 "Peter T" <peter_t@discussions wrote in message ... ByVal - Creates a whole new replica of the object with all of the same settings as the object that is being passed onto it No it doesn't. It creates a new (temporary) pointer to the original intrinsic object. As far as the original object's properties are concerned there is no difference between using ByRef and ByVal. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... The way I understood the ByRef and ByVal are the following: ByRef - References to the object, and any changes done to the object is done for any variable referencing to that object, rather it be done in the original code or in the code referencing to the object. ByVal - Creates a whole new replica of the object with all of the same settings as the object that is being passed onto it, but any changes done to the original object is not reflects in the new object, just like any changes done in the new object is not reflected in the original object. Though I haven't fully tested it to be sure with object variables, but simple data type variables does work like this. -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 "Peter T" <peter_t@discussions wrote in message ... Afraid I don't follow most of that, maybe a simple example might help. One thing though, I think you are misunderstanding the difference of passing an object byref & byval. Either way, any changes to the object's properties will persist. The only difference is with byval a new pointer to the original object is created and passed. This means even if you destroy or reassign the byval object within the procedure, the original object variable will continue point to the same object. Regards, Peter T "Ronald R. Dodge, Jr." wrote in message ... Office XP, SP3 (VBA 6.0) WIN XP, SP2 I have read over the text dealing with custom objects According to the text that I have read online from MS themselves, the keyword "NOTHING" should only destroy the object when there is no other variable refering to that object. However, as I have learned the hard way, that is not the case. Any time any variable is set to NOTHING, the object is destroyed, so any object variable referencing to that object is then also set to NOTHING. Example: PayCode collection has a set of objects containing properties. One of those PayCode objects is passed onto an Employee Object. It's passed into the employee object via a property with the object using the ByRef within the signature line, so if there's any changes to the PayCode object, it's not only changed within the PayCodes collection, but also within the Employee's PayCode reference to it. However, if the Employee Object is set to be destroyed, via the clean up code, the code should be to set the object variable within the Employee Object to NOTHING. However, doing that not only does that, but also set's that particular object within the PayCodes collection to NOTHING as if the object itself has been destroyed. How do I resolve this issue, so as I can have it reset only the variable, not destroy the actual object, but yet, also allow for changes to carry into the variable when they are done into the main object? -- Thanks, Ronald R. Dodge, Jr. Production Statistician Master MOUS 2000 |
#9
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
ByVal - Creates a whole new replica of the object with all of the same
settings as the object that is being passed onto it, Nope. Peter is correct that ByVal and ByRef specify only how the pointer to the object is passed, not whether the object itself, or any sort of clone of the object, is passed. It would tremendous overhead to create a clone or replica of the object and then pass that. You can pass the Application object around using ByVal and certainly no replica of the Application is created. That would mean creating replicas of all worksheets in all open workbook and all that. That's just not how it works. ByVal and ByRef work the same way regardless of whether the object is defined in the typelib, such as a Range, or whether it is a custom object like Class1. Internal to VBA's "compiled" code, I don't think such as distinction is even made. You can see this quite simply with the following code. First, create a class name Class1 containing Public Text As String Then in a normal module, use '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''' Dim C1 As Class1 Dim C2 As Class1 Sub AAA() Set C1 = New Class1 Set C2 = New Class1 C1.Text = "111" C2.Text = "222" Debug.Print "#1 Before PassByVal: C1: " & C1.Text & " C2: " & C2.Text ' pass ByVal PassByVal C1 Debug.Print "#2 After PassByVal: C1: " & C1.Text & " C2: " & C2.Text ' reset C1.Text = "111" C2.Text = "222" Debug.Print "#3 Before PassByRef: C1: " & C1.Text & " C2: " & C2.Text ' pass ByRef PassByRef C1 Debug.Print "#4 After PassByRef: C1: " & C1.Text & " C2: " & C2.Text End Sub Sub PassByVal(ByVal C As Class1) Set C = C2 End Sub Sub PassByRef(ByRef C As Class1) Set C = C2 End Sub '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''' The immediate window will show: #1 Before PassByVal: C1: 111 C2: 222 #2 After PassByVal: C1: 111 C2: 222 #3 Before PassByRef: C1: 111 C2: 222 #4 After PassByRef: C1: 222 C2: 222 Line #1 shows that C1 has text 111 and C2 has text 222, as one would expect. After C1 is passed ByVal to PassByVal, line #3 shows that its Text value is still 111. This indicates that the original object C1 was not changed to point to another object. *WITHIN PassByVal*, C is changed to point to C2, but since the pointer was ByVal, the object is unchanged back in Sub AAA. Line #3 is just from the reset to original values. Line #4 shows that BOTH C1 and C2 now have a Text value of 222, which is so because in PassByRef, the parameter C (pointing to C1) is changed to point to C2. Cordially, Chip Pearson Microsoft MVP Excel Product Group Pearson Software Consulting, LLC www.cpearson.com (email on web site) On Fri, 12 Dec 2008 11:53:30 -0500, "Ronald R. Dodge, Jr." wrote: The way I understood the ByRef and ByVal are the following: ByRef - References to the object, and any changes done to the object is done for any variable referencing to that object, rather it be done in the original code or in the code referencing to the object. ByVal - Creates a whole new replica of the object with all of the same settings as the object that is being passed onto it, but any changes done to the original object is not reflects in the new object, just like any changes done in the new object is not reflected in the original object. Though I haven't fully tested it to be sure with object variables, but simple data type variables does work like this. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Dimmed Keyword as Variable | Excel Programming | |||
If Range Variable isn't Empty | Excel Programming | |||
assign objects into array or object variable? | Excel Programming | |||
empty variable range | Excel Discussion (Misc queries) | |||
Resetting a variable to nothing | Excel Programming |