Hello,
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 )
else
if p_List.Char = Mid( Chain, Posit, 1 ) Then
FILL_LIST( p_List.NextE, Chain, Nb_Cells, Posit + 1 )
else
FILL_LIST( p_List.Other, Chain, Nb_Cells, Posit )
end if
end if
end if[/code]
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 .
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
fixdate(d)
msgbox d.ShortDate // this date is 100 days beyond today, and no BYREF was used.
Private Sub fixdate(d as date)
d.day=d.day+100
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.