Parameter passed by reference (Binary tree)

I am trying to create a binary tree (composed by letters of words) by recursion .
The method described below works in another language but here I meet a problem with the parameter p_List passed by reference.
When I start the program I get an error message: "You cannot pass an expression as parameter defined by reference ( byRef ) ".
I do not see how to get around this problem or maybe there is another problem in my logic.
If somebody could help me to understand better the transmission of a parameter by reference, I would be very grateful to him for it.
The method:

[code]FILL_LIST( ByRef p_List As T_Ptr_List, Chain As String, ByRef Nb_Cells As Integer, Posit As Integer )

Dim p_Inser As T_Ptr_List

p_Inser = New T_Ptr_List

If Posit <= Len(Chain) Then
If p_List = Nil Or p_List.Char > Mid( Chain, Posit, 1 ) Then
CREATION_CELL( p_Inser, Mid( Chain, Posit, 1) )
Nb_Cells = Nb_Cells + 1
p_Inser.Other = p_List
FILL_LIST( p_List.NextE, Chain, Nb_Cells, Posit + 1 )
if p_List.Char = Mid( Chain, Posit, 1 ) Then
FILL_LIST( p_List.NextE, Chain, Nb_Cells, Posit + 1 )
FILL_LIST( p_List.Other, Chain, Nb_Cells, Posit )
end if
end if
end if[/code]

Thank you.


Classes can’t use the ByRef keyword as paramters. If one parameter is a class instance it doesn’t work.

Only datatypes them selfs.

Replace these lines:


Dim obj As T_Ptr_List = p_List.NextE FILL_LIST(obj, Chain, Nb_Cells, Posit + 1)

[quote=362671:@Derk Jochems]Classes can’t use the ByRef keyword as paramters. If one parameter is a class instance it doesn’t work.

Only datatypes them selfs.[/quote]

Sure you can - just what you’re passing by reference might not be what you think it is

Try this

  1. new desktop app
  2. open preferences > debugging and make sure you turn ON show object ID’s in variable lists
  3. to the default window add the open event with the following code
         dim d as new Date
         break // take note of the object ID of D here
         break // take note of the object ID of D here
  1. add the method ReplaceDate as
       ReplaceDate(byref d as date)
              d = new Date

Note that the object ID changes from the first break statement to the second
This has not changed the contents of d - it changed what D refers to

Thanks my friends.

Eli, I thought I understood the principle. You create an intermediate parameter but it is necessary to link it again to p_List and there I have tried different things without success, thus it means I did not understand everything :frowning: .
If you could clarify that point you will save me to finalize my project.


ByRef means the compiler needs a memory address to be able to point to. p_List is such a thing, p_List.NextE is not.

This is just how the Xojo compiler does it, I’m sure other programming languages allow what you tried (in pointer-based languages like C for example). It’s probably just easier to implement a compiler if the developer has to “unwind” the property and assign it to a local variable compared to implementing that in the compiler.

When passing any OBJECT (ie. class) to a method… it is by default BYREF, as you are passing a POINTER (even if you are unaware of this)… Any other type of variable (Integer, Double, String, etc) are by default passed as BYVAL

this applies to DATE as well… to expand on Norms example

dim d as new date
msgbox d.ShortDate // this date is 100 days beyond today, and no BYREF was used.
Private Sub fixdate(d as date)
End Sub

Are you just storing words in your tree so you can, for example, get all words that start with “a” or “ab”, etc.? If so, you might be better served with an in-memory SQLite database with an indexed field. You can then do something like SELECT word FROM word_table WHERE word LIKE 'a%'. You can put that code behind your class to simplify access.