Adding 2 variables together

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • begoddendave
    New Member
    • Mar 2007
    • 12

    Adding 2 variables together

    Hi

    I am trying to add 2 variables together but I think the computer see's the numbers as a string instead of an integer.

    I have looked for help with this but all I can see is ASP doesn't allow the user to define a variable as an int or a string

    My code is very simple (I hope). I retrieve a 4 digit code from my database which I call var_Pin. I then split it to var_LowPin and var_HighPin and I think it is at this point the computer sees the number as a string instead of an integer.

    I have added ***** beside the line that is also partly the fault. If I change var_HighPin to a number (eg) 20, it works fine.

    <%
    dim var_Pin
    var_Pin = (rsSampleReques t.Fields.Item(" PinsPerRow").Va lue)
    dim var_LowPin
    var_LowPin = (Left(var_Pin,2 ))
    dim var_HighPin
    var_HighPin = (Right(var_Pin, 2))
    dim var_CurrentPin
    var_CurrentPin = var_LowPin

    Do While var_CurrentPin < var_HighPin *****
    response.Write( var_CurrentPin)
    var_CurrentPin = var_CurrentPin + 1
    Loop
    %>

    Any ideas?

    Thank you

    Dave
  • idsanjeev
    New Member
    • Oct 2007
    • 241

    #2
    Hi begoddendave
    you are getting any error
    if you have problems to read data as interger then you can do it with vbscripts function

    Tips

    but your question is over to my head need more clearity.
    thanks

    Comment

    • DrBunchman
      Recognized Expert Contributor
      • Jan 2008
      • 979

      #3
      Hi Dave,

      If you call the CInt(var) function on your variables like so:

      var_LowPin = CInt(Left(var_P in,2))

      It will cast them to integers if possible.

      Hope this helps,

      Dr B

      Comment

      • begoddendave
        New Member
        • Mar 2007
        • 12

        #4
        Dr B

        THANK YOUUUUUUU

        That is perfect. It has worked. I am going to look up what the CInt actually does as I didn't think 'one' could define integers in ASP

        Thank you again


        Dave

        Comment

        • DrBunchman
          Recognized Expert Contributor
          • Jan 2008
          • 979

          #5
          My pleasure.

          In VBScript all variables are variants which means that at any point in the code they can be of one type or another. Although you can't explicitly define a variable as an integer, string or whatever they are implicitly defined by the values you assign to them or the actions you take on them.

          So saying x = 1 makes x an integer but saying x = "Hello" makes x a string.

          This is why you can cast from one data type to another as long as the value stored in the variable allows it.

          I'm sure you'll find a clearer and more thorough explanation via the magic of google.

          Happy coding,

          Dr B

          Comment

          Working...