CDbl / VAL format

Complete newbie here and I’m sure I’m making a simple error
I have two TextField numbers that I want to use in some maths and then pass it to a Label
Anyhow, when I do the maths manually, I get the required results, however my code below does not get the result.
I suspect its something to do with the numbers format. I have searched the web with no luck and I have tried with a VAL too.

Any assistance would be appreciated

My code:

Dim a as Double
Dim b as Double
Dim c as Double

a= CDbl (TextField1.Text) ’ TextField is masked #.###
b= CDbl (TextField2.Text) ’ TextField is masked #.###
c= 3600*((a-b)/b) 'this does not work???
'c= 3600*((1.001-1.000)/1.000) ’ This works when tested

’ send result to “placeholder”
Label1.Text =CStr©


I get the same answer (3.6) with both instructions? Entered 1.001 in TextField1 and 1.000 in TextField2.

What answer do you get with c= 3600*((a-b)/b)?

Hi Alwyn,
Thanks for your reply… I get INF…
I now suspect it’s something to do with the fact that the text field is masked… I will try different formatting of the textfield and see how i go


I get the INF answer when Textfield2 is 0. Maybe just make sure that the value in TextField2 is 1.000.

I just tried my code in a clean project and it works… so there must be some error else where… I guess I need to go looking.
I am running this code as a method, could that be a problem?
… mmm nice learning curve

Good to hear you got it working :wink:

Running it as a method shouldn’t be a problem, and if it is a calculation you will be using a lot, actually recommended.

Your method should look something like this:

Function DoCalculation(a as Double, b as Double) As Double
Dim c as Double
c = 3600 * ((a - b) / b)
return c
End Function

You can then use the method like follow:

Label1.Text = CStr(DoCalculation(CDbl (TextField1.Text), CDbl (TextField2.Text)))

Thanks so much for your help.
I have tried your method but get a syntax error on the End function and End Sub
Clearly I’m not getting out my depth…

The End function shouldn’t be pasted into your method. Try these steps…

  1. Click on “Insert” (blue cube) and select “Method”.
  2. Enter “DoCalculation” for the method name.
  3. Enter “a as Double, b as Double” for the parameters.
  4. Enter “Double” for the return type.
  5. Paste the following code into the body of the method.

Dim c as Double
c = 3600 * ((a - b) / b)
return c

In all the steps the quotes should NOT be included.

Methods are very powerful, and it’s definitely worth the learning curve.

Thanks for that. I’m such a lemon…
Very powerful in deed and a lot quicker than my rough as version.
Anyhow, all working on a clean project but not in my project. very frustrating…
Not sure what to do next…
Thanks again

Have you tried to use the debugger to see what the values are when the calculation is performed?

In your current project, click on the gutter next to "c= 3600*((a-b)/b) " line, so that a red dot appears. Now when you run the program the program will stop when it gets to that point.

At the bottom right you will see all the variables of your program. Check what the value of the b variable is. If it is zero, then that would be why you are getting the answer INF.

found the problem… :slight_smile:
I had this little bit of code to clean up which must have been firing before my other method

’ 'Clean up
’ TextField_BoatName.Text = “” ’ clear text box
’ TextField_Rating.Text = “”
’ TextField_BoatName.SetFocus ’ set focus on textbox

and I guess it was clearing out one of the textfields…

So you were bang on the money with your 0 value.
Thanks again

You’re welcome, and glad you found the fix :wink:

and learnt how to make a proper method :slight_smile: