Passing variable to Class

noclass1980

Active Member
Licensed User
Longtime User
Hi,

I'm using the Table Class to draw simple tables and it has Turquoise and White rows as default. How can I pass 3 variables to set the RGB of the row based upon a user's preference? I'm using the AHPreferenceActivity to allow the user to select a colour and then use the HandleSettings to set the RGB values of the colour. If I dim ColR, ColG, ColB as Int in Process Globals in the Main module, how to I pass them to the Table Class for that the code below works?

B4X:
Tablecd2.Initialize(Colors.RGB(ColR,ColG,ColB), 0)
'where Tablecd2 is the row colour used in the Table Class
if I dim the three variables in Class_Globals in the Table Class then I just get a black row as the variable values are effectively 0 as the actual value from the main module is not passed.
Any suggestions? Thanks
 

yttrium

Active Member
Licensed User
Longtime User
Hi,

I'm using the Table Class to draw simple tables and it has Turquoise and White rows as default. How can I pass 3 variables to set the RGB of the row based upon a user's preference? I'm using the AHPreferenceActivity to allow the user to select a colour and then use the HandleSettings to set the RGB values of the colour. If I dim ColR, ColG, ColB as Int in Process Globals in the Main module, how to I pass them to the Table Class for that the code below works?

B4X:
Tablecd2.Initialize(Colors.RGB(ColR,ColG,ColB), 0)
'where Tablecd2 is the row colour used in the Table Class
if I dim the three variables in Class_Globals in the Table Class then I just get a black row as the variable values are effectively 0 as the actual value from the main module is not passed.
Any suggestions? Thanks

So why exactly doesn't this work when you dim them in Process_Globals with a default value? What error do you get?
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
So why exactly doesn't this work when you dim them in Process_Globals with a default value? What error do you get?

It's not that an error is caused as I don't set them to a default value in the Table Class. In the main module, I set
ColR=98
ColG=155
ColB=105

to create a Green colour but these values are not passed to the Table Class and so effectively I get
ColR=0
ColG=0
ColB=0

and so a black row is created. I would like to be able to passed the actual values to the TAble Class in order to create the green rows. Thanks.
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
It's not that an error is caused as I don't set them to a default value in the Table Class. In the main module, I set
ColR=98
ColG=155
ColB=105

to create a Green colour but these values are not passed to the Table Class and so effectively I get
ColR=0
ColG=0
ColB=0

and so a black row is created. I would like to be able to passed the actual values to the TAble Class in order to create the green rows. Thanks.

Do you begin rendering the table with a line in your Main module?
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Do you begin rendering the table with a line in your Main module?

If I understand you correctly, yes. the table_add sub begins as

B4X:
Sub DataTable_add
DataTable.Initialize(Me, "DataTable", 6)
allpanels(1).LoadLayout("Data_Layout")
DataTable.AddToActivity(allpanels(1), 0dip, 50dip, 100%x, 60%y)
Dim FirstLabelWidth,NextLabelWidth,NextLabelPos,LabelWidth As Int
Dim DataTableHeaders As List
Dim DataTablelab0, DataTablelab1, DataTablelab2,DataTablelab3,DataTablelab4 As Label
DataTable.Setheader(Array As String("Number","Type","Item", "P", "I","R" ))
For i = 1 To DataTableRows
DataTable.AddRow(Array As String("","", "", "", "", ""))
Next
DataTableItemWidth=(100%x-(2*DataTableTypeWidth)-(3*ScoreWidth))
DataTable.SetColumnsWidths(Array As Int(DataTableTypeWidth,DataTableTypeWidth, DataTableItemWidth,  ScoreWidth, ScoreWidth, ScoreWidth))
DataTable.RowHeight=75%y/20

at the moment the row colour is set in the Table Class but I would like to pass the varaible colour RGB values over to the Table Class. Thanks
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
If I understand you correctly, yes. the table_add sub begins as

B4X:
Sub DataTable_add
DataTable.Initialize(Me, "DataTable", 6)
allpanels(1).LoadLayout("Data_Layout")
DataTable.AddToActivity(allpanels(1), 0dip, 50dip, 100%x, 60%y)
Dim FirstLabelWidth,NextLabelWidth,NextLabelPos,LabelWidth As Int
Dim DataTableHeaders As List
Dim DataTablelab0, DataTablelab1, DataTablelab2,DataTablelab3,DataTablelab4 As Label
DataTable.Setheader(Array As String("Number","Type","Item", "P", "I","R" ))
For i = 1 To DataTableRows
DataTable.AddRow(Array As String("","", "", "", "", ""))
Next
DataTableItemWidth=(100%x-(2*DataTableTypeWidth)-(3*ScoreWidth))
DataTable.SetColumnsWidths(Array As Int(DataTableTypeWidth,DataTableTypeWidth, DataTableItemWidth,  ScoreWidth, ScoreWidth, ScoreWidth))
DataTable.RowHeight=75%y/20

at the moment the row colour is set in the Table Class but I would like to pass the varaible colour RGB values over to the Table Class. Thanks

Why not pass an argument, calling it like so?

B4X:
...
    DataTable_add(ColR, ColG, ColB)
End Sub

Sub DataTable_add (R As Int, G As Int, B As Int)
    DataTable.Initialize(Me, "DataTable", 6)
...
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
Passing variables to a Class Module

Why not pass an argument, calling it like so?

B4X:
...
    DataTable_add(ColR, ColG, ColB)
End Sub

Sub DataTable_add (R As Int, G As Int, B As Int)
    DataTable.Initialize(Me, "DataTable", 6)
...

Hi, thanks for the response, I tried but it didn't work but then I spotted in another thread that you can reference process globals in a class module by referencing the module where the variable is declared. I haven't tried it yet but the app compiled ok but I didn't have Tab connected so couldn't actually try it. The next step to try is to create an Activity module called "Class_Variables" and use this to store the variable value which can then be used in the Table Class module. What I mean is that in the Main module the three variables are defined as:

B4X:
Class_Variables.TabRowColourR=153
Class_Variables.TabRowColourG=204
Class_Variables.TabRowColourB=255
and then in the Table Class module the row could is set to
B4X:
Dim Tablecd2 As ColorDrawable
Tablecd2.Initialize(Colors.RGB(Class_Variables.TabRowColourR,Class_Variables.TabRowColourG,Class_Variables.TabRowColourB), 0)

I've done it this way so that I can have different tables with user defined colours. Within my app, the user can select an option A or an option B process to run which both create different tables (but not at the same time) and hopefully I can use this approach to be able to set different colours while using the Table Class module.

This appears to be acceptable BUT I haven't tried it yet. Will do soon and post an edit with results.

Just tried this and it works a peach! now have a method to pass variables to Classes! Brilliant! for info the Class_Variables code is simply
B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
Dim TabRowColourR As Int
Dim TabRowColourB As Int
Dim TabRowColourG As Int
End Sub
 
Last edited:
Upvote 0
Top