You are not logged in.

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, June 11th 2009, 4:41pm

ByRef in VB and in Jabaco

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

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

2

Friday, June 12th 2009, 3:36pm

Concatenate Strings with StringBuilder.Append

With Strings it is the same. But Of course you do not have to use a certain self defined class or user defined type.
But do not use the Jabaco String or java#lang#String either.
Just use an object of the StringBuilder class that can be found in the namespace java#lang.

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
Public Sub Form_Load() 

   Dim s1 As New java#lang#String: s1 = "null " 
   Call AddString1(s1) 
   Call AddString2(s1) 
   MsgBox s1 '"null " 
   
   Dim s2 As New java#lang#StringBuilder("null ") 
   Call AddStringB1(s2) 
   Call AddStringB2(s2) 
   MsgBox s2 '"null one two " 

End Sub 
Public Sub AddString1(aSB As java#lang#String) 
   aSB = aSB & "one " 
End Sub 
Public Sub AddString2(aSB As java#lang#String) 
aSB = aSB & "two " 
End Sub 
Public Sub AddStringB1(aSB As java#lang#StringBuilder) 
aSB.append "one " 
End Sub 
Public Sub AddStringB2(aSB As java#lang#StringBuilder) 
aSB.append "two " 
End Sub

With the StringBuilder you do not concatenate the strings with the "&"-Operator but you use the apped-function.
and you can do this very very comfortable look at this:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
Public Sub Form_Load() 

   Dim s2 As New java#lang#StringBuilder("null ") 
   Call AddMore(s2) 
   MsgBox s2 

End Sub 
Public Sub AddMore(aSB As java#lang#StringBuilder) 
   aSB.append("three ").append("four ").append("five ").append("six ").append(vbnewline).append(aSB.toString) 
End Sub


greetings
OlimilO

Rate this thread
WoltLab Burning Board