[บทเรียน,B4X]การเขียนโค้ดที่มีประสิทธิภาพ(Code Smells-Code Tips)

Theera

Expert
Licensed User
Longtime User
B4X:
'bad
Private Sub Button1_Click
    xui.MsgboxAsync("สวัสดี B4X ชาวไทยทุกคน", "B4X")
End Sub
Private Sub Button2_Click
    xui.MsgboxAsync("สวัสดี B4X ชาวไทยทุกคน", "B4X")
End Sub
'หมายเหตุ สามารถเขียนได้อีกแบบหนึ่ง

'โดยตั้งค่า Event Name ของแต่ละ Button เป็นชื่อส่วนกลาง เช่น ButtonX แล้วเขียนโค้ดจัดการร่วมกันได้
'แต่ถ้าต้องการตรวจสอบว่า Button อันไหนเป็นปุ่มที่ถูกกด ให้ตั้งค่า Tag เป็นชื่อของปุ่มนั้น เช่น Button1.tag="Button1" และ
'Button2.tag="Button2" แล้วใช้ตัวแปรชื่อว่า Sender เป็นตัวรับค่าไว้ก่อน ดูตามตัวอย่าง
B4X:
'good
Private Sub ButtonX_Click
     Dim btn As Button=Sender
     
     If btn.Tag="Button1" Then
        'Do something specific for Button1
     Else If btn.Tag="Button2" Then
        'Do something specific for Button2
    End If
End Sub
 
Last edited:

Theera

Expert
Licensed User
Longtime User
ขอขอบคุณErel ชาวอิสราเอล ณ ที่นี่ด้วย
B4X:
'old
If Lst = Null Or Lst.IsInitialized = False Then Return
'new
If IsInitialized(Lst) = False Then Return
 

Theera

Expert
Licensed User
Longtime User
ขอขอบคุณ Aeric ชาวมาเลเซีย ณ ที่นี่ด้วย
B4X:
'old
  Dim data As List
    data.Initialize
    data.Add(Array ("Paris", "France", "Europe"))
    data.Add(Array ("Kyiv", "Ukraine", "Europe"))
    data.Add(Array ("Cairo", "Egypt", "Africa"))
    data.Add(Array ("Beijing", "China", "Asia"))
    data.Add(Array ("Santiago", "Chili", "America"))

'new
        Dim data As List
        data.Initialize
        For Each row() As Object In res.Rows
            Dim val1 As String = row(res.Columns.Get("<ชื่อคอลัมน์>"))
            data.Add(val1)
        Next
 
Last edited:

Theera

Expert
Licensed User
Longtime User
ใน B4A เวอร์ชั่น 13.3 Erel กำหนดนิยามใหม่ ดังนี้ ขอขอบคุณ Erel ณ ที่นี้ด้วยครับ

B4X:
'Bad
If Map1 <> Null And Map1.IsInitialized Then ... 'boring

'Good
If Initialized(Map1) Then ... 'less boring
 

Theera

Expert
Licensed User
Longtime User
B4X:
IIf(SupportedOrientationsScreen.SelectOrientation="Portrait",Activity.LoadLayout("1"),Activity.LoadLayout("2"))  'Not Worked
'-----------------------------------------------------------------------
 
IfSupportedOrientationsScreen.SelectOrientation="Portrait" then  'Worked
         Activity.LoadLayout("1")
   Else
        Activity.LoadLayout("2")
   End If
'-----------------------------------------------------------------------
'Thank you, Daestrum's advice

Activity.Layout(IIf(SupportedOrientationsScreen.SelectOrientation="Portrait","1","2")) 'Better
 

Theera

Expert
Licensed User
Longtime User
ขอขอบคุณ Erel ณ ที่นี่ด้วยครับ

B4X:
'Bad
Private Sub B4XUISwitch1_Click
'แสดงวิธีการใช้งาน ทั้งแบบ B4XPages และแบบ Non-B4XPages
 
    If B4XUISwitch1.SwitchState Then
        Try
        'using  B4XPages  code

        'Allow screenshots

              Dim wrk_jo As JavaObject=Me
              'wrk_jo.InitializeContext    'Using B4XPages Remark this line
              wrk_jo.RunMethod("unsecurescreen", Null)
 
        Catch
       'using  Non-B4XPagescode
    
        'Allow screenshots

              Dim wrk_jo As JavaObject
              wrk_jo.InitializeContext    'Using Non-B4XPages UnRemark this line
              wrk_jo.RunMethod("unsecurescreen", Null)
    

        End Try


    
    Else
        Try
        'using  B4XPages  code
     
'        'Block screenshots
              Dim wrk_jo As JavaObject=Me
              'wrk_jo.InitializeContext  'Using B4XPages Remark this line
              wrk_jo.RunMethod("securescreen", Null)

 
        Catch
       'using  Non-B4XPages  code
    
'        'Block screenshots
              Dim wrk_jo As JavaObject
              wrk_jo.InitializeContext  'Using B4XPages UnRemark this line
              wrk_jo.RunMethod("securescreen", Null)
    

       End Try
 
End If

End Sub

B4X:
'Good
Private Sub SetSecureScreenState (Secure As Boolean)
'แสดงวิธีการใช้งาน ทั้งแบบ B4XPages และแบบ Non-B4XPages
Try
    'using  B4XPages  code
    Dim jme As JavaObject = Me
        'jme.InitializeContext    'Using B4XPages Remark this line
        jme.RunMethod(IIf(Secure, "securescreen", "unsecurescreen"), Null)
Catch
    'using  Non-B4XPagescode
    Dim jme As JavaObject
        jme.InitializeContext    'Using Non-B4XPages UnRemark this line
        jme.RunMethod(IIf(Secure, "securescreen", "unsecurescreen"), Null)
End Try
End Sub

Private Sub B4XUISwitch1_Click
    SetSecureScreenState(B4XUISwitch1.SwitchState)
End Sub

B4X:
'Java Code Module

#If Java
import android.annotation.TargetApi;
import android.content.Context;
import android.view.WindowManager.*;
public void securescreen() {
    //this.getWindow().setFlags(LayoutParams.FLAG_SECURE,LayoutParams.FLAG_SECURE);
    //ต้องแก้ไข getBA().activity แทน this
     getBA().activity.getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
}
public void unsecurescreen() {
    //this.getWindow().clearFlags(LayoutParams.FLAG_SECURE);
    //ต้องแก้ไข getBA().activity แทน this
    getBA().activity.getWindow().clearFlags(LayoutParams.FLAG_SECURE);
}
#End If
 
Last edited:
Top