Math Questions. Just do this instead, it's easier....

This is my code I'm working with. I'm trying to use the sum from the lhGal to show in lhGal2.Text.

Dim FirstNum, SecondNum, ThirdNum, FourthNum As Double

'Intial Fuel Add

FirstNum = lhPounds.Text

SecondNum = lhDensity.Text

ThirdNum = lhGal.Text

FourthNum = lhPounds2.Text

lhGal.Text = FirstNum / SecondNum - 0.4

'Additional Fuel Added to bring to 1200

lhGal2.Text= FourthNum / SecondNum - lhGal



Answer this question

Math Questions. Just do this instead, it's easier....

  • RichParker

    ROFL-

    It's still all Greek to me. But I'm slowly learning.

  • thukralz

    You should also use Option Strict ON (type as the first line in your class file)

    This will force you to make explicit conversions...and will help avoid conversion errors!

    lhGal2.Text = Cstr((FourthNum / SecondNum) - Cdbl(lhGal.Text))

    This still does not consider the error if the value in IhGal.Text can not be converted to a numeric value



  • vijil

    Hi,

    '   As long as the numbers are right in the textboxes this should work!!

    '   This is your original code only altered with CDBL and CSTR  

    '   instructions added into the code instead. 

    Dim FirstNum, SecondNum, ThirdNum, FourthNum As Double

    '  Intial Fuel Add 

    FirstNum =Cdbl( lhPounds.Text)

    SecondNum = Cdbl(lhDensity.Text)

    ThirdNum = Cdbl(lhGal.Text)

    FourthNum = Cdbl(lhPounds2.Text)

    lhGal.Text = Cstr(FirstNum / SecondNum - 0.4)

    '   Additional Fuel Added to bring to 1200  

    lhGal2.Text=Cstr( FourthNum / SecondNum - lhGal)

     

    '   Cdbl is Convert to Double 'Convert into one dataTYPE when doing maths.

    '   CStr is Convert to STRing. ' Always use when writing to a textbox.

     

     

    Regards,

    S_DS.

     

     



  • garyvdailyware

     ReneeC wrote:
    The explicit casting is additional code and it's inevitable that it will slow the code down.

    The compiler is going to make the cast implicitly anyway, so I doubt there's much if any performance penalty.  Either way, we can certainly agree to disagree

    Edit: And to be completely honest, some of it's just personal bias.  Coming from a C++ background, I can't help but cringe when I see Strings being added and subtracted with doubles.  Oh well.  Everybody's got their own preferences and there's nothing wrong with that.  (Just don't expect me to debug more than a few lines of that sort of code!)



  • NoGoodNameExists

    That didn't work...When I filled out the information none of my field filled, but then again it's user error....

    I messed around with it.. and now I can't get the lhTotalGal.Text to work.. It just places lhGal.Text and not the combine total.

    Dim SecondNum As Double

    SecondNum = lhDensity.Text

    lhGal.Text = 80 / SecondNum - 0.4
    lhGal2.Text = 1200 / SecondNum - lhGal.Text
    lhTotalGal.Text = (lhGal.Text) + (lhGal2.Text)

  • fasttrack

    You're not paying attention. You can't add text. You have to convert all text to numbers before doing any math processing.



  • Jason Beard

    Well I know why I was pulling out my hair... It wasn't working because all the fields have to be filled out before it would calculate. 

    This leads to more questions

    1.) I want only "Density and AddLip" to be filled out by the user.  As it stands right now all fields have to be filled out with numbers in order for the program to work.  How do I fix this

    2.) If I enter a number in "Density" for e.g. 6.08 and I delete that number to change it.  I get a runtime error.

    3.) "Density" can't be the first field to be populated or I get a runtime error, how do I fix that  

     

    'Left Hand Wing

    Dim FirstNum, SecondNum, ThirdNum, FourthNum, FifthNum, SixthNum, SeventhNum, EighthNum As Double

    Dim PoundNum, PoundNum2 As Double

    FirstNum = CDbl(Density.Text)

    SecondNum = CDbl(lhGal.Text)

    ThirdNum = CDbl(lhGal2.Text)

    FourthNum = CDbl(lhTotalGal.Text)

    FifthNum = CDbl(lhTotal.Text)

    SixthNum = CDbl(lhGR.Text)

    SeventhNum = CDbl(lhAddLip.Text)

    EighthNum = CDbl(lhTotal.Text)

    PoundNum = CDbl(80)

    PoundNum2 = CDbl(1200)

     

    'Initial Fuel - Not Counted in Calculating Total Gallons

    lhGal.Text = CStr(PoundNum / FirstNum - 0.4)

    'Added Fuel to get to 1200

    lhGal2.Text = CStr(PoundNum2 / FirstNum - SecondNum)

    'Total Fuel of Fuel added to reach 1200 and Added to Reach Lip

    lhTotalGal.Text = CStr(SecondNum + ThirdNum)

    'Gauge Reading

    lhGR.Text = CStr(FifthNum * FirstNum)

    'Total Fuel

    lhTotal.Text = CStr(SecondNum + ThirdNum + SeventhNum)

     

     

     


  • PublicError

     Spidermans_DarkSide wrote:

    Hi,

    '   As long as the numbers are right in the textboxes this should work!!

    '   This is your original code only altered with CDBL and CSTR  

    '   instructions added into the code instead. 

    Dim FirstNum, SecondNum, ThirdNum, FourthNum As Double

    '  Intial Fuel Add 

    FirstNum =Cdbl( lhPounds.Text)

    SecondNum = Cdbl(lhDensity.Text)

    ThirdNum = Cdbl(lhGal.Text)

    FourthNum = Cdbl(lhPounds2.Text)

    lhGal.Text = Cstr(FirstNum / SecondNum - 0.4)

    '   Additional Fuel Added to bring to 1200  

    lhGal2.Text=Cstr( FourthNum / SecondNum - lhGal)

     

    '   Cdbl is Convert to Double 'Convert into one dataTYPE when doing maths.

    '   CStr is Convert to STRing. ' Always use when writing to a textbox.

     

     

    Regards,

    S_DS.

     

     

     I

    If I add a number to "lhDensity" I error "Conversion from "" to type 'Double' is not valid. 

    lhGal2.Text=Cstr( FourthNum / SecondNum - lhGal) <----- once this is added I get  Operator '_' is not defined for type 'Double'  and 'System.Windows.Forms.Textbox'.

     

    I think I'm going give up....... for now. 


  • D. Wood

    I was assuming that you're getting the values from textboxes and outputting the results to textboxes. If not, what is the lh prefix referring to

    However, I noticed that you are outputting one of the results to the same place where you are getting one of the operands. Strongly suggest you use a different place for the output.

    Here is the corrected code, using 6 textboxes. I tried it and it works:


    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim FirstNum, SecondNum, ThirdNum, FourthNum As Double
    Dim Fuel1, Fuel2 As Double
    Double.TryParse(txtPounds.Text, FirstNum)
    Double.TryParse(txtDensity.Text, SecondNum)
    Double.TryParse(txtGalin.Text, ThirdNum)
    Double.TryParse(txtPounds2.Text, FourthNum)
    If SecondNum <> 0 Then
    Fuel1 = FirstNum / SecondNum - 0.4
    Fuel2 = FourthNum / SecondNum - ThirdNum
    txtGalout.Text = Fuel1.ToString
    txtGal2out.Text = Fuel2.ToString
    Else
    MessageBox.Show("txtDensity.Text is incorrect")
    End If
    End Sub
    End Class



  • HillBillyB

    I disagree with this. I always use option explicit but never Option Strict which requires casting with database code. It's rare that I have conversion errors and when they occur it doesn't take any time to debug them. The explicit casting is additional code and it's inevitable that it will slow the code down.



  • Mirricle

    lh = Left Hand because there will eventually be a right hand once I learn the code for the left. 

    Actually Pounds is a known number, this is what I have now.

    lhGal.Text = 80 / lhDensity.Text - 0.4

    lhGal2.Text = 1200 / lhDensity.Text - lhGal.Text

    lhAddLip.Text = lhGal.Text + lhGal2.Text <----------- The output isn't adding up.. It just adds lhGal.Text

    lhTotalGal.Text = lhAddLip.Text + lhGal2.Text

    lhGR = lhTotalGal.Text * lhDensity.Text

    End Sub

     

    There are a few things what I'm trying to accomplish here.   

    When I enter Density (This is an unknown because it changes with tempature)

    1.) 80 / Density -  .4   = gallons1

    2.)1200 / Density - Output of 1 = gallons2

    3.) TotalGallons1 * Density  = Gauge Reading

    4.) Input Add Fuel

    5.) Totalgallons1 = 2 + 4

    6.) Totalgallons2 = 1 + 2 + 4  

    If Total Gallons 1 or 2 = 0 I don't want it to error out.

     

    I made a excel spread sheet however it's VB 1 me 0

     

     


  • Selectis

    I take it you are having a problem with the last line An error something along the lines of:

    Operator '-' is not defined for types 'Double' and 'System.Windows.Forms.TextBox'.

    If so... the problem is that you are trying to get your value out of lhGal differently than you have from all of the other TextBoxes... instead, add .Text to the end of lhGal in order to specify exactly what value you want to pull out of the TextBox making the last line read:

    lhGal2.Text = FourthNum / SecondNum - lhGal.Text



  • Cellchuk

    Save all values as Double. The TryParse() method (in VS 2005 only) will convert a valid numeric string value into a number. If not valid it will convert the string into 0 without throwing an exception. Then use the Double values to perform the math processes. Convert the result back to a string to output to the textboxes.

    Please do turn Option Strict On. If you had it on in the first place, it would have flagged your previous code statements as invalid. All conversions must be done explicitly. With Option Strict Off, it tries to convert implicitly, and may return incorrect results.

    This is how your code should look:


    Dim FirstNum, SecondNum, ThirdNum, FourthNum As Double
    Dim Fuel1, Fuel2 As Double

    Double.TryParse(lhPounds.Text, FirstNum)
    Double.TryParse(lhDensity.Text, SecondNum)
    Double.TryParse(lhGal.Text, ThirdNum)
    Double.TryParse(lhPounds2.Text, FourthNum)
    If SecondNum <> 0 Then
    Fuel1 = FirstNum / SecondNum - 0.4
    Fuel2 = FourthNum / SecondNum - lhGal
    lhGal.Text = Fuel1.ToString
    lhGal2.Text= Fuel2.ToString
    Else
    'lhDensity.Text is incorrect
    End If



  • LTD

    DMan1 wrote:
    You should also use Option Strict ON (type as the first line in your class file)

    Agreed; the Strict and Explicit options are wonderful things that can save you a ton of debug time. There's no excuse to ever NOT use them!



  • Math Questions. Just do this instead, it's easier....