You are not logged in.

Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

OlimilO

Intermediate

  • "OlimilO" is male
  • "OlimilO" started this thread

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

1

Thursday, October 22nd 2009, 12:09pm

a few words on assigning objects

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

This post has been edited 1 times, last edit by "OlimilO" (Oct 22nd 2009, 12:15pm)


Rate this thread
WoltLab Burning Board