Android Question add ... #include source code with B4X ide ?

fgh3966

Active Member
Licensed User
Hello everybody

In order to properly organize my project, I would like to create several source files for the same project.
How can we do with B4A v12 ?

Should I use #include ?

Thanks in advance.
Regard
 

zed

Active Member
Licensed User
Use B4XPages

 
Upvote 0

fgh3966

Active Member
Licensed User
Thank you for your answers but I don't understand.
Also B4XPages doesn't seem to work on my wine_OS.
My source code will get long and take several pages to write and it will be difficult to manage with a single page.
How can I simply fix this problem ?
Where can i find an example ?

Thanks in advance.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
My source code will get long and take several pages to write and it will be difficult to manage with a single page.
How can I simply fix this problem ?
How can we give you advice or examples if you don't indicate what exactly you want to program anyway?

And if you don't want to, or can't use B4XPages, then you will have to live with a long source file or you can try to split that long source file into classes and modules.
Both have there own limitation on the use of those classes and modules. Your best friend for classes and modules is the search option of the forum.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
To break up your code, you can create several class modules or code modules to hold the code.

A class module will require you to create an instance of the class before calling any subs whilst a code module can be called directly.

Its been a while since I looked at the documentation, but I believe that this is fully described in there. code modules are more restrictive than classes.

Most of the larger sample projects on that you can download from the forum use one or both of these methods.

Hope that helps.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Class modules are more flexible. I would recommend learning how to use both.

Here is a forum post about this:

The link is old, the correct link is :
 
Upvote 0

fgh3966

Active Member
Licensed User
Hello all


I admit that I did not expect these difficulties because i believing B4X would easily solve this problem.
Generally other IDEs resolve this problem simply, for example : in C, assembler etc ... this problem of multiple lines is solved with #<include>
I have to write about 80 times the lines below which take a lot of space and the code would be incomprehensible.
Also obviously I should expect to repeat many other lines.
So even if we concentrate the code below, the problem of many lines will come back.
And then the development IDEs: B4X, etc... with the communities of programmers are there to write lines of code, aren't we?

Should there be no explanation in French?
How do variables communicate between the source program and the modules?

Thank you for your understanding.


de nombreuses lignes à répéter:
   If QRT1 = 4 Then
        '******************************** 1 *************************************
        ImageView1.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView2.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
        ImageView3.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView4.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
    End If
        
    If QRT1 = 5 Then
        '******************************** 2 *************************************
        ImageView1.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView2.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
        ImageView3.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView4.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
    End If
    
    If QRT1 = 6 Then
        '******************************** 2 *************************************
        ImageView1.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView2.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
        ImageView3.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
        ImageView4.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
    End If
    
    If QRT1 = 7 Then
        '******************************** 0 *************************************
        ImageView1.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView2.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
        ImageView3.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
        ImageView4.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
    End If
 
Upvote 0

zed

Active Member
Licensed User
We still don't know what you want to do.
How can we give you advice or examples if you don't indicate what exactly you want to program anyway?
It may not be necessary to write all the "imageview.bitmap=" lines.
There is a recursive system.

For example to select all imagesview in a same panel.
B4X:
For Each iv As view In panelImageView.GetAllViewsRecursive
    if iv as imageview then
     'your code here
    end if
Next
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
My source code will get long and take several pages to write and it will be difficult to manage with a single page.
How can I simply fix this problem ?
Where can i find an example ?
As you've been told: classes

Examples (as you can see in the small piece of code from Erel, the 53 lines in the code, are divided into main (lines 1 to 19) and a class (from line 20 on)


We still don't know what you want to do, can your code be written with something like:
B4X:
    If QRT1 = 4 or QRT1 = 5  OR .... Then (or even if QRT1 >= 4 AND QRT1 <= 8)
        '******************************** 2 *************************************
        ImageView1.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView2.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
        ImageView3.Bitmap = LoadBitmap(File.DirAssets, "led_off.png")
        ImageView4.Bitmap = LoadBitmap(File.DirAssets, "led_vert.png")
    End If
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
If I understand you correctly, there is no need to use #include you can use a class or code module instead.

I have written this code directly into the comment, so it might not be completely correct, but it will give you an idea.
B4X:
private codem as mycodeclass
codem.initilize

select QTR1
   case 4
       codem.setButtons(false,true,false,false)

   case 5
     codem.setButtons(false,true,false,true)
end Select

... In the class mycodeclass

private sub setButtons(img1show as boolean, img2show as boolean, img3 as boolean, img4as boolean)

   iif(img1show,setImage(imageView1,"led_off.png"),setImage(imageView1,"led_vert.png"))
   iif(img2show,setImage(imageView2,"led_off.png"),setImage(imageView2,"led_vert.png"))
   iif(img3show,setImage(imageView3,"led_off.png"),setImage(imageView3,"led_vert.png"))
   iif(img4show,setImage(imageView4,"led_off.png"),setImage(imageView4,"led_vert.png"))
end sub

private sub setImage(imgv as imageview, img as string)
    imgv.bitmap = LoadBitmap(File.dirAssets,img)
end sub
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
BTW, I have classes and code modules that have several hundred lines of code. Both in B4X and PHP.
HTML is worse some of the pages can get really large
 
Upvote 0

fgh3966

Active Member
Licensed User
Thank you Digitwell and other.
Just Digitwell for your previous post at 10H55 PM (yesterday)
Can i try to compile tour code as a library ?
 
Upvote 0

fgh3966

Active Member
Licensed User
Hello

I think I found the solution.
just create a module linked to the project and include a function in it. Module are named modulM in example.
into modulM i create function calctoto
Then you have to call the function in the "file" main >>> result = modulM.calctoto(1)
Here are the sources, warning there is an error for the display of the variable result.

code of main:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    Private xui2 As XUI
    Public toto As Byte
    Public var As Byte
    Public result As Byte
    
End Sub

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

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    var = 1
    result = modulM.calctoto(1)
    
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
    xui2.MsgboxAsync ("result", "B4X" )
End Sub





modulM:
'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    'Public toto As Byte
    
End Sub


Public Sub calctoto(var As Byte)
Dim toto As Byte

toto = toto + var

Return toto

End Sub

if it can help someone
very thanl all !
 
Upvote 0

fgh3966

Active Member
Licensed User
I can't declare and use an imageview in a module

yet I declared it in public mode
 

Attachments

  • Capture du 2023-06-03 21-05-07.png
    241.8 KB · Views: 61
  • Capture du 2023-06-03 21-04-47.png
    268.1 KB · Views: 74
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…