Hello everybody,
let's talk about ByRef in Jabaco (1.4.0 Beta)
In VB6 there are two possibilities to declare a variable in a function header:
ByRef or ByVal
If you know VB6 you know the difference between ByRef and ByVal.
If you are a beginner maybe you have heard it but don't know what it is exactly?
It's better to look at it by an example so let's construct a very simple example.
VB6-Code
========
if you do not explicitely declare ByRef or ByVal the default ist ByRef.
ByRef means when you use the variable Value in fact you use a reference-pointer
to the variable that you have passed to the function.
|
Jabaco Source
|
1
2
3
4
5
6
7
8
9
10
|
Sub FooP(Value As Integer)
Value = Value + 12
'actually the Variable that you have passed
'to the Function call will be changed
End Sub
Sub FooV(ByVal Value As Integer)
Value = Value + 12
'here you have declared a locale variable inside the function
'and added 12 to the value that you passed to the function call
End Sub
|
Jabaco-Code
===========
you could declare ByRef of ByVal but there is no difference between them, the default and the only is ByVal.
|
Jabaco Source
|
1
2
3
4
5
6
7
8
9
10
|
Sub FooP(Value As Integer)
Value = Value + 12
'here you have declared a locale variable inside the function
'and added 12 to the value that you passed to the function call
End Sub
Sub FooV(ByVal Value As Integer)
Value = Value + 12
'here you have declared a locale variable inside the function
'and added 12 to the value that you passed to the function call
End Sub
|
So what is the effect of it?
lets test it in an small example project.
* Start VB and Jabaco
* on a Form put a Commandbutton Command1
* and a TextBox Text1, for convenience set the property MultiLine to True and the Font to "Courier New" size 10
* in the Form declare the variable t as string
* copy the two above subs into the forms module
VB and Jabaco Code
=================
|
Jabaco Source
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Option Explicit
Dim t As String
Public Sub Command1_Click()
Dim Value As Integer
Value = 2000
t = t & "Value before FooP: " & CStr(Value) & vbNewLine
Call FooP(Value)
t = t & "Value after FooP: " & CStr(Value) & vbNewLine
Call FooV(Value)
t = t & "Value after FooV: " & CStr(Value) & vbNewLine
Text1.Text = t
End Sub
|
in VB you will get the following text in the TextBox:
|
Source code
|
1
2
3
4
5
|
Value before FooP: 2000
Value inside FooP: 2012
Value after FooP: 2012
Value inside FooV: 2024
Value after FooV: 2012
|
but in Jabaco it is different, you will get this text in the TextBox:
|
Source code
|
1
2
3
4
5
|
Value before FooP: 2000
Value inside FooP: 2012
Value after FooP: 2000
Value inside FooV: 2012
Value after FooV: 2000
|
you see the variable Value still remains at the value 2000
Oooops so what could we do there?
VB6 is known as a pointerless language but you use pointers with ByRef.
Java is known as a pointerless language too, and of course you also could use pointers
if you simply use objects. Or In Jabaco you also could use user defined Types.
If you want to have the same results in Jabaco you could use the following approach:
|
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
|
Option Explicit
Dim t As String
Private Type PInteger
Value As Integer
End Type
Public Sub FooP(V As PInteger)
V.Value = V.Value + 11
'actually the Variable that you have passed
'to the Function call will be changed
t = t & "V.Value inside FooP: " & CStr(V.Value) & vbNewLine
End Sub
Public Sub FooV(ByVal Value As Integer)
Value = Value + 11
'here you have declared a locale variable inside the function
'and added 12 to the value that you passed to the function call
t = t & " Value inside FooV: " & CStr(Value) & vbNewLine
End Sub
Public Sub Command1_Click()
Dim V As PInteger
V.Value = 2000
t = t & "V.Value before FooP: " & CStr(V.Value) & vbNewLine
Call FooP(V)
t = t & "V.Value after FooP: " & CStr(V.Value) & vbNewLine
Call FooV(V.Value)
t = t & "V.Value after FooV: " & CStr(V.Value) & vbNewLine
Text1.Text = t
End Sub
|
many greetings
OlimilO