-
Jun 29th, 2020, 06:33 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Strange Class Clone problem
Something very strange going on, which used to work!
I have a class with references to it.
myClass.cls
Code:
LeftClass(5) = new myClass
RightClass(5) = new myClass
When i do:
Code:
LeftClass(2) = RightClass(4).Clone
LeftClass does NOT get copied. All properties have EMPTY STRINGS!!!
VB Mixing up address pointers or something?
Code:
Public Function Clone() As myClass
'Return a copy of this instance
'Create a blank copy
Set Clone = New myClass
With Clone
.Name = xName
.pID = xID
.Channels = xChannels
End With
End Function
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
-
Jun 29th, 2020, 07:00 AM
#2
Re: Strange Class Clone problem
Shouldn't that be "Set LeftClass(2)=RightClass(4).Clone"?
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jun 29th, 2020, 07:09 AM
#3
Thread Starter
Frenzied Member
Re: Strange Class Clone problem
Originally Posted by Zvoni
Shouldn't that be "Set LeftClass(2)=RightClass(4).Clone"?
Nope. That throws an error 438: Object doesn't support this property or method
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
-
Jun 29th, 2020, 07:31 AM
#4
Re: Strange Class Clone problem
Well you do not call functions using . unless of course the function is part of the class
seems like it should be
Code:
Set LeftClass(2)=Clone(RightClass(4))
And of course the function would need to accept the class as a parameter.
Last edited by DataMiser; Jun 29th, 2020 at 07:35 AM.
-
Jun 29th, 2020, 07:37 AM
#5
Re: Strange Class Clone problem
Looking further you are using different variable names for your assignments so I really have no idea what you are doing there, not enough code given, do not know where the function is or what those variables may hold.
-
Jun 29th, 2020, 08:16 AM
#6
Thread Starter
Frenzied Member
Re: Strange Class Clone problem
I've now also tried: rightClass(5).Clone(RightClass(4))
I think there's something wrong with VB AddressSpace ?
Because it used to be working! it's just NOT copying the properties across.
Last edited by some1uk03; Jun 29th, 2020 at 08:22 AM.
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
-
Jun 29th, 2020, 09:10 AM
#7
Re: Strange Class Clone problem
Create a basic sample program which shows your problem.
If use these kind of methods a lot and never ran into problems
-
Jun 29th, 2020, 09:31 AM
#8
Re: Strange Class Clone problem
Originally Posted by Arnoutdv
Create a basic sample program which shows your problem.
If use these kind of methods a lot and never ran into problems
ditto
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jun 29th, 2020, 10:55 AM
#9
Re: Strange Class Clone problem
Originally Posted by some1uk03
When i do:
Code:
LeftClass(2) = RightClass(4).Clone
LeftClass does NOT get copied. All properties have EMPTY STRINGS!!!
If that "worked" in the past without the Set statement ( Set LeftClass(2) = RightClass(4).Clone ) you are doing something strange.
Perhaps the class has some default property defined and that's why you don't get an error message, but the class members values were in fact copied somewhere else (that now it is not in the code any more).
-
Jun 29th, 2020, 11:55 AM
#10
Re: Strange Class Clone problem
You are going to have to post a play project. I'd bet you are doing something wrong and VB is not the reason.
In its simplest form, this does not cause an error on:
Code:
Private Sub Command1_Click()
Dim lc As New Class1 ' pointless if going to set it to some clone anyway
Dim rc As New Class1
rc.Name = "Hello World"
Set lc = rc.Clone
MsgBox lc.Name
End Sub
simple class1
Code:
Dim xName As String
Dim xID As Long
Dim xChannels As Long
Public Function Clone() As Class1
'Return a copy of this instance
'Create a blank copy
Set Clone = New Class1
With Clone
.Name = xName
.pID = xID
.Channels = xChannels
End With
End Function
Public Property Get Name() As String
Name = xName
End Property
Public Property Let Name(v As String)
xName = v
End Property
Public Property Get pID() As Long
pID = xID
End Property
Public Property Let pID(v As Long)
xID = v
End Property
Public Property Get Channels() As Long
Channels = xChannels
End Property
Public Property Let Channels(v As Long)
xChannels = v
End Property
Last edited by LaVolpe; Jun 29th, 2020 at 12:23 PM.
-
Jun 29th, 2020, 12:23 PM
#11
Thread Starter
Frenzied Member
Re: Strange Class Clone problem
LaVolpe: The code is exactly as you quickly summed up there, except the msgbox returns ""
the project is quite huge.. so I'm still looking..
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
-
Jun 29th, 2020, 12:30 PM
#12
Re: Strange Class Clone problem
In the sample code above, the msgbox does not return ""
I assume you meant that in your project, you are not getting things copied. And since the sample I posted works and yours does not, there is a key difference. We'll need a stripped down example to look at.
By the way: key difference between Set and not using Set
Set Me.Picture = Me.Icon ' calls Picture Set and Icon Get
Me.Picture = Me.Icon ' calls Picture Let and Icon Get
the last example would cause an error because a Set is required to assign objects. However, we coders can get creative and have our Let properties call a Set internally. This is why an error isn't generated. Picture Let property is calling a Picture Set method internally.
-
Jun 29th, 2020, 12:56 PM
#13
Re: Strange Class Clone problem
If your project is huge, maybe it is using the Implements keyword and the class you are trying to clone is an implementation? In that case, it is possible that you think the class is another class and the properties you expect are not in the implemented class but the class that is implementing it?
However if the variable on the LHS is being set to the same class type returned by the Clone method, you shouldn't be having any issues. I'd recommend placing a breakpoint on the clone method and walking the code to see where the error occurs.
-
Jun 29th, 2020, 01:08 PM
#14
Re: Strange Class Clone problem
Originally Posted by some1uk03
LaVolpe: The code is exactly as you quickly summed up there
Originally Posted by LaVolpe
Originally Posted by some1uk03
Code:
LeftClass(2) = RightClass(4).Clone
....
-
Jun 29th, 2020, 01:11 PM
#15
Re: Strange Class Clone problem
Originally Posted by some1uk03
Originally Posted by Zvoni
Shouldn't that be "Set LeftClass(2)=RightClass(4).Clone"?
Nope. That throws an error 438: Object doesn't support this property or method
No...
-
Jun 29th, 2020, 01:18 PM
#16
Re: Strange Class Clone problem
Have you compared the ObjPtr of both after assignment?
Meaning:
Code:
LeftClass(2)=RightClass(4).Clone
Debug.Print ObjPtr(LeftCLass(2))
Debug.Print ObjPtr(RIghtClass(4).Clone)
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jun 29th, 2020, 01:35 PM
#17
Re: Strange Class Clone problem
Originally Posted by Zvoni
Have you compared the ObjPtr of both after assignment?
Meaning:
Code:
LeftClass(2)=RightClass(4).Clone
Debug.Print ObjPtr(LeftCLass(2))
Debug.Print ObjPtr(RIghtClass(4).Clone)
In that code the ObjPtr will be different because every time you call Clone it instantiate a new object.
But a better test would be:
Code:
Debug.Print ObjPtr(LeftCLass(2))
LeftClass(2)=RightClass(4).Clone
Debug.Print ObjPtr(LeftCLass(2))
That must print the same ObjPtr because the object of LeftClass(2) is never replaced due to the lack of the Set statement.
(BTW, there is no possibility to have a Let anywere there, they are just variables)
-
Jun 29th, 2020, 01:56 PM
#18
Re: Strange Class Clone problem
Code:
LeftClass(2)=RightClass(4).Clone
That line should error: Object doesn't support this property or method
Why? Because the .Clone method is returning an object and Set is not used. If it doesn't error then default properties are in play. And only the default properties are being cloned, not the entire class.
here's a modified example with default properties in play. In effect, the clone call ends up like this:
lc.Instance = rc.Clone.Instance, no other clone properties set
Code:
Private Sub Command1_Click()
Dim lc As New Class1 ' pointless using NEW if going to set it to some clone anyway
Dim rc As New Class1
rc.Name = "Hello World"
rc.Instance = ObjPtr(rc)
lc = rc.Clone
' notice that the left hand clone has no name but has Instance. This is because without Set above, only the default property is updated
MsgBox lc.Name & vbCrLf & rc.Name
MsgBox lc.Instance & vbCrLf & rc.Instance
End Sub
Class1
Code:
' you must use IDE menu: Tools | Procedure Attributes
' select Instance property, then click Advanced, then set Procedure ID to Default
Dim xName As String
Dim xID As Long
Dim xChannels As Long
Dim xInstance As Long
Public Property Get Instance() As Long
Instance = xInstance
End Property
Public Property Let Instance(v As Long)
xInstance = v
End Property
Public Function Clone() As Class1
'Return a copy of this instance
'Create a blank copy
Set Clone = New Class1
With Clone
.Name = xName
.pID = xID
.Channels = xChannels
.Instance = xInstance
End With
End Function
Public Property Get Name() As String
Name = xName
End Property
Public Property Let Name(v As String)
xName = v
End Property
Public Property Get pID() As Long
pID = xID
End Property
Public Property Let pID(v As Long)
xID = v
End Property
Public Property Get Channels() As Long
Channels = xChannels
End Property
Public Property Let Channels(v As Long)
xChannels = v
End Property
Last edited by LaVolpe; Jun 29th, 2020 at 02:16 PM.
-
Jun 29th, 2020, 02:09 PM
#19
Re: Strange Class Clone problem
some1uk03, the first block of code you gave us was:
Originally Posted by some1uk03
Code:
LeftClass(5) = new myClass
RightClass(5) = new myClass
Personally, I don't even understand how that could work.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jun 29th, 2020, 02:26 PM
#20
Re: Strange Class Clone problem
@Elroy, obviously he is setting RightClass(5) properties elsewhere in code.
@some1uk: If you run my modified example a couple posts up, I think that is what you are seeing. If SET is not used, LET is called and if no property is being queried, i.e., just the class name, no dot + method name, then the class' default property is returned.
ex: MsgBox Class1 will display its default property if it has one else it will error
leftClass = rightClass.Clone is same as leftClass.[_default] = rightClass.Clone.[_default]
Set leftClass = rightClass.Clone is creating a new class
In object explorer (F2) within the IDE, default properties have that hand icon with a little checkmark in icon's top left corner. You can look at your classes in object explorer and see which have default properties and which do not.
-
Jun 29th, 2020, 02:40 PM
#21
Re: Strange Class Clone problem
IDK, but I think we're all taking pot-shots in the dark with entirely too little information. IMHO, some1uk03 is also struggling with the concepts of instantiation and creating additional references, as well as creating "clones" (which, the way he's got it, should be another instantiation and not another reference to the same object). But even that's a guess at this point.
Also, the fact that some1uk03 made this statement...
Originally Posted by some1uk03
I think there's something wrong with VB AddressSpace ?
...makes me think he's not examining his own code closely enough. Unless explicit manipulation of object pointers is performed, or explicit manipulation of QueryInterface, Addref, or Release is performed, I've never seen VB6 have any problems with instantiating or un-instantiating objects (or copying their properties).
Until some1uk03 is willing to provide a small "complete" example that illustrates the failure, I'm not sure there's much we can do. Also, the act of putting together those types of examples often illustrates the programming error.
EDIT: And by the way, even the title of the thread illustrates a weakness in understanding. It should be "Strange Object Clone problem". The class is what is used to instantiate the object, and not the object itself.
EDIT2: And some1uk03, I apologize if I'm coming off as harsh. Maybe I'm just getting a bit stir-crazy being in lock-down for so long. I do hope you get it figured out.
Last edited by Elroy; Jun 29th, 2020 at 02:46 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jun 29th, 2020, 02:44 PM
#22
Re: Strange Class Clone problem
I think it's a simple matter of not using Set. I think the cloned class has a default property. That explains everything IMO.
In addition, depending on how complex those classes are, a class default property can be referencing another class with its own default property and so on. Not using Set when wanting to return an object has unexpected consequences when default properties are in play. Maybe that's a reason why converting VB code to .Net can fail when default properties are used implicitly vs. explicitly.
But yes, a learning curve seems to be in play too.
Last edited by LaVolpe; Jun 29th, 2020 at 03:05 PM.
-
Jun 29th, 2020, 03:23 PM
#23
Re: Strange Class Clone problem
Originally Posted by LaVolpe
Code:
LeftClass(2)=RightClass(4).Clone
That line should error: Object doesn't support this property or method
Originally Posted by LaVolpe
@Elroy, obviously he is setting RightClass(5) properties elsewhere in code.
Originally Posted by LaVolpe
I think it's a simple matter of not using Set.
That's what I said in post#9
-
Jun 29th, 2020, 04:02 PM
#24
Re: Strange Class Clone problem
Yeah, I also suspect the problem is failure to Set (using implied Let instead) along with default Properties.
Slightly OT:
Normally a "Clone" method would make use of a Friend Sub call to copy everything in one go. Less overhead than copying each Property one by one and additional internal-state values can be copied as well.
I suspect Friend tends to confuse people because they have fallen out of the habit of writing separately-compiled DLLs, OCXs, and ActiveX EXEs. When used within the same compilation unit they're globally visible there.
VB can confuse newbs because it is unusual. Sort of a Big Wheel equipped with a shop full of power tools. You find naive coders trying to propel it with the bench grinder, lube the wheels with shellac, pound nails with a screwdriver, etc. etc. That's why after VB5 there was no longer a "kid's edition" ("Standard"), only Pro and Ent. You're supposed to have an education.
-
Jun 29th, 2020, 04:04 PM
#25
Thread Starter
Frenzied Member
Re: Strange Class Clone problem
Guys thanks for all the input....
The issue has been resolved now.
#Believe it or not closed/opened VB a few times and everything was back to normal!
Now both options work again!
Code:
LeftClass(2)=RightClass(4)
and
LeftClass(2)=RightClass(4).Clone
Code:
SET LeftClass(2)=RightClass(4).Clone is raising an error 438
--------------
Then again I've now re-written the workflow to update the class as ref
i/e
Code:
RightClass(4).clone LeftClass(2)
'//in class
Public Sub Clone(tmpOBJ as myClass)
With tmpOBJ
.Name = xName
.pID = xID
.Channels = xChannels
End With
End Function
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
-
Jun 29th, 2020, 04:44 PM
#26
Re: Strange Class Clone problem
Originally Posted by dilettante
Slightly OT:
Normally a "Clone" method would make use of a Friend Sub call to copy everything in one go. Less overhead than copying each Property one by one and additional internal-state values can be copied as well.
I don't understand what you mean with "to copy everything in one go", do you mean using CopyMemory or what?
-
Jun 29th, 2020, 05:34 PM
#27
Re: Strange Class Clone problem
Originally Posted by some1uk03
Code:
SET LeftClass(2)=RightClass(4).Clone is raising an error 438
That makes no sense. if you get the error on that line, it could only happen if the Clone method name is misspelled, but you would have to get the same error without Set.
If the error happens inside the Clone method, you also would have to get the same error without Set.
Could you make a small sample project to show that error?
PS: try to always post "working" sample projects to show what you mean with your questions. While doing that, probably half of the times you'll find the problem yourself (and we don't have to guess too).
-
Jun 29th, 2020, 06:06 PM
#28
Re: Strange Class Clone problem
Originally Posted by Eduardo-
I don't understand what you mean with "to copy everything in one go", do you mean using CopyMemory or what?
No, just a subroutine call passing data names to be copied into ByRef. One call is more efficient than 5 or 10 or 50 Property calls.
-
Jun 29th, 2020, 06:31 PM
#29
Re: Strange Class Clone problem
-
Jul 1st, 2020, 06:52 AM
#30
Re: [RESOLVED] Strange Class Clone problem
[RESOLVED] Strange Class Clone problem
Wait, suddenly it's resolved? What was the issue? I NEED CLOSURE! (not to be confused with clojure, which is a wholy different skill set.)
I feel like I just read a who-dunnit novel and the last chapter is missing.
-tg
-
Jul 1st, 2020, 08:03 AM
#31
Re: [RESOLVED] Strange Class Clone problem
Originally Posted by techgnome
Wait, suddenly it's resolved? What was the issue? I NEED CLOSURE! (not to be confused with clojure, which is a wholy different skill set.)
I feel like I just read a who-dunnit novel and the last chapter is missing.
-tg
it was the stable-boy.......
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Jul 1st, 2020, 11:22 AM
#32
Re: [RESOLVED] Strange Class Clone problem
Personally, I sort of think this whole thread should be deleted. For someone who's trying to learn about VB6 classes and objects, this thread is just going to confuse the crud out of them.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Jul 1st, 2020, 02:35 PM
#33
Re: [RESOLVED] Strange Class Clone problem
Originally Posted by Elroy
Personally, I sort of think this whole thread should be deleted. For someone who's trying to learn about VB6 classes and objects, this thread is just going to confuse the crud out of them.
Oh my!
and now you‘re introducing databases into the equation
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|