Android Question compare 10 variables as byte and assign 10 variables as char

fgh3966

Active Member
Licensed User
Hello everybody

I try create a sub routine that compare one variable (for example 1 to 10) and when variable are good , then variable as char assigned with a character
Example :
variable = 2 char variable assign W
variable = 3 char variable assign F
variable = 4 char variable assign R
etc ...

i learn beginner guide >> Subs / routines , case - select, if then else. I try some codes, but B4A ide often gives me errors that I don't understand. Also I find that the examples of the manual are short. How do we analyze for example 4 values ? How initialize variables in sub routine ?

Thank
 

Attachments

  • Capture du 2023-02-28 20-31-54.png
    112 KB · Views: 140

Brian Dean

Well-Known Member
Licensed User
Longtime User
You will find that most problems in programming can be solved in many ways. One of them will be the best, but often several ways will be almost equally good. Some ways will definitely be the wrong way, although they might still work.

How do we analyze for example 4 values ? How initialize variables in sub routine ?

Here is an example that answers those question, but it is not a good example and is probably not the best way to do this task.
B4X:
Sub Example(n As Int) As String
    Dim result As String            ' Create a variable inside subroutine
    If (n = 1) Then result = "X"
    If (n = 2) Then result = "Y"
    If (n = 3) Then result = "Z"
    If (n = 4) Then result = "!"
    Return result   
End Sub

Here is another example that does the same job; again it is not a good example ...
B4X:
Sub anotherExample(n As Int) As String
    Dim letters As List                ' Create a list
    letters.initialize                ' Initialise it
    letters.Add("X")                    ' Add some content
    letters.Add("Y")
    letters.Add("Z")
    letters.Add("!")
    Return letters.Get(n - 1)
End Sub

In this case the better way would be to create the list once, outside the subroutine, rather than create it again every time that you need to use the subroutine. Also there are often more direct ways of converting numbers to characters if the characters are in a continuous series.

Also I find that the examples of the manual are short.

Yes - that is because they are just examples. If you want to learn programming you need to look aat many examples and learn the basic skills before you try to start writing a complete program.
 
Upvote 0

fgh3966

Active Member
Licensed User
Thank you Erel

After reading working examples I replaced my code.
This subroutine Sub calc_hexa has a byte variable as input and char type variable as output intended to be displayed.
I read the B4xLanguageBasic_frV1_1.pdf doc, I'm on page 48 item 4.2 SUB standard keywords.
I try the code: calc_hexa (in_hexa As Byte)As Char
I am looking for how to call my sub with the parameter corresponding to the input byte type

sub decode hexa/4:
Sub calc_hexa (in_hexa As Byte)As Char
'    Dim out_hexa As Char
    Dim in_hexa =0 As Byte
    
    
If in_hexa = 3 Then
    out_hexa = "3"
Else If in_hexa = 4 Then
    out_hexa = "4"
Else If in_hexa = 5 Then
    out_hexa = "5"
Else If in_hexa = 6 Then
    out_hexa = "6"
Else If in_hexa = 7 Then
    out_hexa = "7"
Else if in_hexa = 8 Then
    out_hexa = "8"
Else If in_hexa = 9 Then
    out_hexa = "9"
Else If in_hexa = 10 Then
    out_hexa = "A"
Else If in_hexa = 11 Then
    out_hexa = "B"
Else If    in_hexa = 12 Then
    out_hexa = "C"
Else If in_hexa = 13 Then
    out_hexa = "D"
Else if in_hexa = 14 Then
    out_hexa = "E"
Else If in_hexa = 15 Then
    out_hexa = "F"
End If

Return out_hexa
    
End Sub
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
B4X:
    Dim in_hexa =0 As Byte  
    
If in_hexa = 3 Then
    out_hexa = "3"

I am afraid that this code will not work. You are redefining the variable "in_hexa" and setting its value at zero. It will never match any of the "If ..." statements. Leave that line out - "in_hexa" is defined in the subroutine header as a byte variable; you must not redefine it inside the subroutine. But you do need to un-comment the "Dim out_hexa" line.
 
Upvote 0

emexes

Expert
Licensed User
If you are 1/ working with a contiguous range of integers, and 2/ translating them to characters, then perhaps simpler as:

B4X:
Sub calc_hexa (in_hexa As Byte) As Char

    Dim out_hexa As Char = "?"    'if in_hexa not valid hex value, then return "?"

    If in_hexa >= 0 And in_hexa <= 15 Then
        out_hexa = "0123456789ABCDEF".CharAt(in_hexa)
    End If
  
    Return out_hexa

End Sub
 
Last edited:
Upvote 0

fgh3966

Active Member
Licensed User
Very thank for your reply.
I don't know where declare variables out_hexa and in hexa in this code.

declar variables:
Sub Globals
    Dim Label1 As Label
    Dim  vertical_LBL  As Label
    Dim  vertical_DATA As Label
    
    
    Dim out_hexa =0 As Char
    
    Dim in_hexa =0 As Byte

So , I try with keyword "public"
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
I don't know where declare variables out_hexa and in hexa in this code.
You don't have to declare "in_hexa" anywhere - it is passed to the subroutine as an argument. Any byte value can be passed to the subroutine providing it has first been declared (with any name). "out_hexa" is declared inside the subroutine. It is "local" to that subroutine and does not exist outside of the subroutine, so does not need any other declaration. However its value can be passed back as a result to the calling code.
B4X:
Sub Button1_Click
    Dim b As Byte
    Dim c As Char
    b = 65
    c = calc_hexa(b)     ' Pass a byte value to the subroutine and get a char back
    Log("c = " & c)   
End Sub

Sub calc_hexa (in_hexa As Byte) As Char
    Dim out_hexa As Char = "?"    'if in_hexa not valid hex value, then return "?"
    If in_hexa >= 0 And in_hexa <= 15 Then
        out_hexa = "0123456789ABCDEF".CharAt(in_hexa)
    End If
    Return out_hexa
End Sub
 
Upvote 0

fgh3966

Active Member
Licensed User
i try to write lines
Dim out_hexa =0 As Char
Dim in_hexa As Byte
into sub_process_global section or sub section
and there are error >>> B4A write error Parameter name cannot hide global variable name.
I search where i must declare this variable, or how to init sub calc_hexa
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
You do not need to declare any of these variables as globals, in fact you must not. Here is an entire program ...

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim b As Byte
    Dim c As Char
    b = 6
    c = calc_hexa(b)     ' Pass a byte value to the subroutine and get a char back
    Label1.Text = c   
End Sub

Sub calc_hexa (in_hexa As Byte) As Char
    Dim out_hexa As Char = "?"    'if in_hexa not valid hex value, then return "?"
    If in_hexa >= 0 And in_hexa <= 15 Then
        out_hexa = "0123456789ABCDEF".CharAt(in_hexa)
    End If
    Return out_hexa
End Sub

I also attach it here if you want to download it ...
 

Attachments

  • SimpleDemo.zip
    9.3 KB · Views: 178
Upvote 0

fgh3966

Active Member
Licensed User
Hello , very thank for your reply.
So is it possible that these variables communicate with another activity?
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
What you are talking about here is the "scope" of a variable - those sections of a program where the value of a defined variable is meaningful. Take this example ...

B4X:
Sub A
    Dim i as int
End Sub

Sub B
    Dim i as int
End Sub

When Sub A is executed a new storage location is created. When the subroutine ends that storage location is disposed of. The "scope" of "i" only exists within that subroutine. Next time that subroutine executes another storage location will be used. In Sub B the same thing happens; there is no relationship whatsoever between the "i" in Sub A and the "i" in Sub B.

This is true for all subroutines in B4X except "Sub Globals" and "Sub ProcessGlobals". A variable defined in "Sub Globals" will have a scope of the entire activity, and one in "Sub ProcessGlobals" will have a scope of all the activities. Let us not talk yet about "classes".
 
Upvote 0

fgh3966

Active Member
Licensed User
Thanks for your help, I think I understand the scope of variables in a program, so I'm trying to incorporate your example into my code.
However, I can't integrate the B and C variables in my code.
Would it be possible that the "calc_hexa" subroutine could work with variables already existing in the program?
THANKS.
 
Upvote 0

fgh3966

Active Member
Licensed User
I can include your routine in my code and it run very well !
I replace some variables and I just replace "?" per "0" and it run perfectly !

Now in my project, due to orientation phone or tablett i must write two activities (i'm beginer)
So i don't understand what "log" instruction do ?

Thanks
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
So i don't understand what "log" instruction do ?
First of all an important point. If you have a question on a new topic, start a new thread! But in this case I will answer this one here.

The "Log(...)" statement is used for debugging. The content of the Log statement is shown in the "Logs" window on the right-hand side of the IDE. It is usually used to confirm the program flow and check internal values in the code ...
B4X:
Sub calc_hexa (in_hexa As Byte) As Char
    Log("calc_hexa : value = " & in_hexa)
    Dim out_hexa As Char = "?"    'if in_hexa not valid hex value, then return "?"
    ....
    Log("Returning " & out_hexa)
    Return out_hexa
End Sub

Now in my project, due to orientation phone or tablett i must write two activities
You do not need to write two activities - an Activity will open in both orientations. You might need to change the Layout to make the screen appear attractive in both orientations, and if you want to use the app on both 'phones and tablets that can be quite difficult. Even experienced programmers find this a problem. I would advise you to get some more experience using a single orientation before you try to tackle that particular challenge.
 
Upvote 0

fgh3966

Active Member
Licensed User
Good morning
And thank you again for your invaluable help.
I admit that I come from a world of programmers, let's say rather very low level, like assembler for microcontrôller (without variable management (lol) ) etc ...
Here I will have to look at radio_buttons which will activate / desactivate ==> make visible / invisible buttons or images.
I think that radio_buttons make it easier to display the settings on the IHM.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…