"Code smells" are common patterns that can indicate that there is a problem in the code. A problem doesn't mean that the code doesn't work, it might be that it will be difficult to maintain it or that there are more elegant ways to implement the same thing. Remember that not everything is clear...
www.b4x.com
B4X:
'bad
Private Sub Button1_Click
xui.MsgboxAsync("สวัสดี B4X ชาวไทยทุกคน", "B4X")
End Sub
Private Sub Button2_Click
xui.MsgboxAsync("สวัสดี B4X ชาวไทยทุกคน", "B4X")
End Sub
'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
'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
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
'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