Sub Activity_Create(FirstTime As Boolean)
Dim Label1 As Label=TestView(1)
Dim Radio1 As RadioButton=TestView(2)
End Sub
Sub TestView (no) As View
Select Case no
Case 1: Dim atView As Label
Case 2: Dim atView As RadioButton
End Select
atView.Initialize("")
atView.Text="good"
Return atView
End Sub
I want to return View (Label, RadioButton) selectively among 1 and 2.
Running the code gives the following error
can someone help
Error description: Current declaration does not match previous one.
Previous: {Type=Label,Rank=0, RemoteObject=True}
Current: {Type=RadioButton,Rank=0, RemoteObject=True}
Error occurred on line: 9
Case 2: Dim atView As RadioButton
I don't have B4A in front of me to test, but my first try would be:
B4X:
Sub TestView (ViewTypeNumber) As Object 'As View might work too
Select ViewTypeNumber
Case 1:
Dim NewLabel as Label
NewLabel.Initialize("")
NewLabel.Text = "good"
Return NewLabel
Case 2:
Dim NewRadioButton as RadioButton
NewRadioButton.Initialize("")
NewRadioButton.Text = "good"
Return NewRadioButton
Case Else:
Log("Invalid ViewTypeNumber " & ViewTypeNumber)
Log("Probably going to return Null")
End Select
End Sub
Sub TestView (ViewTypeNumber) As Object 'As View might work too
Dim V as View 'default View to return if not ViewTypeNumber 1 or 2
Select ViewTypeNumber
Case 1:
Dim NewLabel as Label
V = NewLabel
Case 2:
Dim NewRadioButton as RadioButton
V = NewRadioButton
End Select
V.Initialize("")
V.Text = "good"
Return V
End Sub
although I'm uncertain as to how a generic View is going to handle the .Initialize and .Text =
The purpose is to set common properties to Views (Label, RadioButton)
For example, Color, Typeface, Gravity, DEFAULT_BOLD, Left, Top, Width, Height, etc. (15 types)
This method also throws an error at v.Initialize("") Error description: Unknown member: initialize
Sub TestView (ViewTypeNumber) As Object 'As View might work too
Dim v as View 'default View to return if not ViewTypeNumber 1 or 2
Select ViewTypeNumber
Case 1
Dim NewLabel as Label = v
Case 2
Dim NewRadioButton as RadioButton = v
End Select
v.Text = "good"
Return v
End Sub
Sub TestView (ViewTypeNumber) As Object 'As View might work too
Dim v as View 'default View to return if not ViewTypeNumber 1 or 2
Select ViewTypeNumber
Case 1
Dim NewLabel as Label = v
Case 2
Dim NewRadioButton as RadioButton = v
End Select
v.Text = "good"
Return v
End Sub
Sub TestView (ViewTypeNumber) As Object 'As View might work too
Dim v As View 'default View to return if not ViewTypeNumber 1 or 2
Select ViewTypeNumber
Case 1
Dim NewLabel As Label = v
NewLabel.Text="good"
************************
************************
Case 2
Dim NewRadioButton As RadioButton = v
NewRadioButton.Text="good"
************************
************************
End Select
End Sub
Messy code, but I did it like this: 15 identical properties were put into each CASE
The B4XView was specifically designed to allow common properties to be set for different views in a consistent way across platforms.
B4X:
Dim lbl As Label
lbl.Initialize("")
Dim btn As Button
btn.Initialize("")
Dim views() As B4XView = Array As B4XView(lbl, btn)
For Each vx As B4XView In views
vx.Text = "XXXX"
Next
Log(lbl.Text & TAB & btn.Text)
'OR
lbl.As(B4XView).Text = "AAAA"
btn.As(B4XView).Text = "BBBB"
Log(lbl.Text & TAB & btn.Text)