Steve Piehl Member Licensed User Longtime User Mar 17, 2022 #1 I am sure this is a very basic question but I have tried many different ways and I can't seem to get anything to work. Sorry if there is already an answer to this elsewhere, I wasn't able to locate it. I have an input screen in which the user inputs values in FieldA, FieldB, FieldC and FieldD. I have created a function that will calculate a new value and display it in a second set of fields (next to the first set). For example user enters: 20 in FieldA, I want to display $400.00 in FieldATTL (next to fieldA) In my code, I want to only write the total calculation code once --- I want to be able to pass the screen object name (fieldX) to the function and have the function update the appropriate total field (FieldXTTL) be updated with the appropriate value. Here is the code I have working for updating one field but I don't know how to pass the field name as a variable and get the code to update the .text attribute appropraitely. Sub UpdateTotal(Fld As Label, Txtval As Float, Multiplier As Float) Dim x As Float x =Txtval * Multiplier FldATTL.Text=NumberFormat2(x,0,2,2,False) End Sub In the code above, I want to pass the "FieldATTL" through the UpdateTotal function and use it to update the .text attribute of the object on the screen. I know the red code is where the issues are. Any help is greatly appreicated.
I am sure this is a very basic question but I have tried many different ways and I can't seem to get anything to work. Sorry if there is already an answer to this elsewhere, I wasn't able to locate it. I have an input screen in which the user inputs values in FieldA, FieldB, FieldC and FieldD. I have created a function that will calculate a new value and display it in a second set of fields (next to the first set). For example user enters: 20 in FieldA, I want to display $400.00 in FieldATTL (next to fieldA) In my code, I want to only write the total calculation code once --- I want to be able to pass the screen object name (fieldX) to the function and have the function update the appropriate total field (FieldXTTL) be updated with the appropriate value. Here is the code I have working for updating one field but I don't know how to pass the field name as a variable and get the code to update the .text attribute appropraitely. Sub UpdateTotal(Fld As Label, Txtval As Float, Multiplier As Float) Dim x As Float x =Txtval * Multiplier FldATTL.Text=NumberFormat2(x,0,2,2,False) End Sub In the code above, I want to pass the "FieldATTL" through the UpdateTotal function and use it to update the .text attribute of the object on the screen. I know the red code is where the issues are. Any help is greatly appreicated.
LucaMs Expert Licensed User Longtime User Mar 17, 2022 #2 Use a Map to associate (to... map!) the left and right views. m.Put(Fld, FldATTL) Upvote 0
klaus Expert Licensed User Longtime User Mar 17, 2022 #3 Test this one: B4X: Sub UpdateTotal(Fld As Label, Txtval As Float, Multiplier As Float) Dim x As Float x =Txtval * Multiplier Fld.Text = NumberFormat2(x,0,2,2,False) End Sub Upvote 0
Test this one: B4X: Sub UpdateTotal(Fld As Label, Txtval As Float, Multiplier As Float) Dim x As Float x =Txtval * Multiplier Fld.Text = NumberFormat2(x,0,2,2,False) End Sub
Steve Piehl Member Licensed User Longtime User Mar 17, 2022 #4 Thanks Klaus! I had tried that...but I realized I was attempting to pass the name in quotes when calling the function...generating a type warning. I appreciate your help! my skills are a bit rusty. Upvote 0
Thanks Klaus! I had tried that...but I realized I was attempting to pass the name in quotes when calling the function...generating a type warning. I appreciate your help! my skills are a bit rusty.
teddybear Well-Known Member Licensed User Mar 18, 2022 #5 LucaMs said: Use a Map to associate (to... map!) the left and right views. m.Put(Fld, FldATTL) Click to expand... This is the solution for you. In the case calculation code once, you must first map Fld to FldATTL. Then the code is like this B4X: Sub UpdateTotal(M As Map, Fld As Label, Txtval As Float, Multiplier As Float) Dim x As Float Dim Lbl as Label Lbl = M.get(Fld) x =Txtval * Multiplier Lbl.Text=NumberFormat2(x,0,2,2,False) End Sub Upvote 0
LucaMs said: Use a Map to associate (to... map!) the left and right views. m.Put(Fld, FldATTL) Click to expand... This is the solution for you. In the case calculation code once, you must first map Fld to FldATTL. Then the code is like this B4X: Sub UpdateTotal(M As Map, Fld As Label, Txtval As Float, Multiplier As Float) Dim x As Float Dim Lbl as Label Lbl = M.get(Fld) x =Txtval * Multiplier Lbl.Text=NumberFormat2(x,0,2,2,False) End Sub