Hi,
in Jabaco all intrinsic primitive data types will be copied with the equal sign " = "
But if you want to assign one Object to another, the meaning could either be
a) to copy the reference or
b) to make a flat copy, also called a clone, of it.
solution to a: simply use the equal sign " = ".
solution to b: you could use the Cloneable-interface. but ... there is another possibility: create a copy constructor
example for all possibilities:
|
Jabaco Source
|
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
|
Public Sub Command1_Click()
'assigning simply the reference
Dim a As New DummyBean("cafebabe")
Dim b As DummyBean = a
MsgBox a & " " & b
b.Bean = "greenbeen"
MsgBox a & " " & b
End Sub
Public Sub Command2_Click()
'using the interface Cloneable.
'It has no method at all, it's just a flag-interface.
'You have to implement the Clone-method yourself
Dim a As New DummyBean("cafebabe")
Dim b As DummyBean = Cast(a.Clone, DummyBean)
MsgBox a & " " & b
b.Bean = "greenbean"
MsgBox a & " " & b
End Sub
Public Sub Command3_Click()
'using a copy constructor
Dim a As New DummyBean("cafebabe")
Dim b As New DummyBean(a)
MsgBox a & " " & b
b.Bean = "greenbean"
MsgBox a & " " & b
End Sub
|
sample class for all possibliities:
|
Jabaco Source
|
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
|
Option Explicit
Implements Cloneable
Private dummy As String
Public Sub DummyBean(bean As String)
dummy = bean
End Sub
'copy constructor
Public Sub DummyBean(another As DummyBean)
Me.dummy = another.dummy
End Sub
Public Property Get Bean() As String
Bean = dummy
End Property
Public Property Let Bean(value As String)
dummy = value
End Property
Public Function toString() As String
toString = Bean
End Function
Public Function Clone() As Object
'Clone uses the copy constructor
'but it *must* return Object
'so you need to cast in the last
Clone = New DummyBean(Me)
End Function
|
regards
OlimilO