When to use integer, when to use string?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    When to use integer, when to use string?

    I've heard it said that you only want to use a number (e.g. integer, long, etc.)
    if you are going to do calculations or some kind of math with it. Is this true?
    For example, I run a validate routine that checks an address entry - if
    something's missing in the entry, the code does different things based on what
    is missing, indicated my a 2 or a 3 - integer values. Should I use string data
    types here? Does it matter?

    Public Sub Validate()
    Dim fail As Integer
    On Error GoTo HandleErr
    fail = 1
    If IsNull(Me!Addre ssDescription) Then fail = 2
    If (IsNull(Me!Addr essDescription) Or Me!AddressDescr iption = "Main") And _
    IsNull(Me!Addre ss1) And _
    IsNull(Me!Addre ss2) And _
    IsNull(Me!Addre ss3) And _
    IsNull(Me!City) And _
    IsNull(Me!State ) And _
    IsNull(Me!Zipco de) And _
    IsNull(Me!Count ry) _
    Then fail = 3
    Exit_Here:
    Exit Sub
    HandleErr:
    Select Case Err.Number
    Case Else
    modHandler.LogE rr (Me.Form.Name)
    Resume Exit_Here
    End Select
    End Sub


  • MGFoster

    #2
    Re: When to use integer, when to use string?

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Usually, numeric values process faster than strings.

    In your code you have Fail = x. The question would be what are you
    going to do with the final Fail value? Display to a user? What sense
    will a number mean to a user? Use in a lookup of an error message?
    Makes sense 'cuz of faster processing of numeric values - hopefully,
    to find the error string for the error number (Fail value).

    HTH,

    MGFoster:::mgf0 0 <at> earthlink <decimal-point> net
    Oakland, CA (USA)

    -----BEGIN PGP SIGNATURE-----
    Version: PGP for Personal Privacy 5.0
    Charset: noconv

    iQA/AwUBQBGCTYechKq OuFEgEQKutACgwZ C8x4rrgvR6iOIky I6XG1r8hXcAnAwR
    CalVNm+9YyHBEAe 793Wn+3As
    =80Fw
    -----END PGP SIGNATURE-----


    deko wrote:
    [color=blue]
    > I've heard it said that you only want to use a number (e.g. integer, long, etc.)
    > if you are going to do calculations or some kind of math with it. Is this true?
    > For example, I run a validate routine that checks an address entry - if
    > something's missing in the entry, the code does different things based on what
    > is missing, indicated my a 2 or a 3 - integer values. Should I use string data
    > types here? Does it matter?
    >
    > Public Sub Validate()
    > Dim fail As Integer
    > On Error GoTo HandleErr
    > fail = 1
    > If IsNull(Me!Addre ssDescription) Then fail = 2
    > If (IsNull(Me!Addr essDescription) Or Me!AddressDescr iption = "Main") And _
    > IsNull(Me!Addre ss1) And _
    > IsNull(Me!Addre ss2) And _
    > IsNull(Me!Addre ss3) And _
    > IsNull(Me!City) And _
    > IsNull(Me!State ) And _
    > IsNull(Me!Zipco de) And _
    > IsNull(Me!Count ry) _
    > Then fail = 3
    > Exit_Here:
    > Exit Sub
    > HandleErr:
    > Select Case Err.Number
    > Case Else
    > modHandler.LogE rr (Me.Form.Name)
    > Resume Exit_Here
    > End Select
    > End Sub
    >
    >[/color]

    Comment

    • Mike Storr

      #3
      Re: When to use integer, when to use string?

      Add on top of that lower memory overhead, smaller storage requirements and
      the fact that most of the functions you'll come across and constants use
      them in some way. Using strings primarily would require a whole lot of
      conversions to accomplish most tasks.

      Mike Storr




      "MGFoster" <me@privacy.com > wrote in message
      news:jnfQb.2502 0$zj7.13653@new sread1.news.pas .earthlink.net. ..[color=blue]
      > -----BEGIN PGP SIGNED MESSAGE-----
      > Hash: SHA1
      >
      > Usually, numeric values process faster than strings.
      >
      > In your code you have Fail = x. The question would be what are you
      > going to do with the final Fail value? Display to a user? What sense
      > will a number mean to a user? Use in a lookup of an error message?
      > Makes sense 'cuz of faster processing of numeric values - hopefully,
      > to find the error string for the error number (Fail value).
      >
      > HTH,
      >
      > MGFoster:::mgf0 0 <at> earthlink <decimal-point> net
      > Oakland, CA (USA)
      >
      > -----BEGIN PGP SIGNATURE-----
      > Version: PGP for Personal Privacy 5.0
      > Charset: noconv
      >
      > iQA/AwUBQBGCTYechKq OuFEgEQKutACgwZ C8x4rrgvR6iOIky I6XG1r8hXcAnAwR
      > CalVNm+9YyHBEAe 793Wn+3As
      > =80Fw
      > -----END PGP SIGNATURE-----
      >
      >
      > deko wrote:
      >[color=green]
      > > I've heard it said that you only want to use a number (e.g. integer,[/color][/color]
      long, etc.)[color=blue][color=green]
      > > if you are going to do calculations or some kind of math with it. Is[/color][/color]
      this true?[color=blue][color=green]
      > > For example, I run a validate routine that checks an address entry - if
      > > something's missing in the entry, the code does different things based[/color][/color]
      on what[color=blue][color=green]
      > > is missing, indicated my a 2 or a 3 - integer values. Should I use[/color][/color]
      string data[color=blue][color=green]
      > > types here? Does it matter?
      > >
      > > Public Sub Validate()
      > > Dim fail As Integer
      > > On Error GoTo HandleErr
      > > fail = 1
      > > If IsNull(Me!Addre ssDescription) Then fail = 2
      > > If (IsNull(Me!Addr essDescription) Or Me!AddressDescr iption = "Main")[/color][/color]
      And _[color=blue][color=green]
      > > IsNull(Me!Addre ss1) And _
      > > IsNull(Me!Addre ss2) And _
      > > IsNull(Me!Addre ss3) And _
      > > IsNull(Me!City) And _
      > > IsNull(Me!State ) And _
      > > IsNull(Me!Zipco de) And _
      > > IsNull(Me!Count ry) _
      > > Then fail = 3
      > > Exit_Here:
      > > Exit Sub
      > > HandleErr:
      > > Select Case Err.Number
      > > Case Else
      > > modHandler.LogE rr (Me.Form.Name)
      > > Resume Exit_Here
      > > End Select
      > > End Sub
      > >
      > >[/color]
      >[/color]


      Comment

      • Salad

        #4
        Re: When to use integer, when to use string?

        deko wrote:
        [color=blue]
        > I've heard it said that you only want to use a number (e.g. integer, long, etc.)
        > if you are going to do calculations or some kind of math with it. Is this true?
        > For example, I run a validate routine that checks an address entry - if
        > something's missing in the entry, the code does different things based on what
        > is missing, indicated my a 2 or a 3 - integer values. Should I use string data
        > types here? Does it matter?
        >
        > Public Sub Validate()
        > Dim fail As Integer
        > On Error GoTo HandleErr
        > fail = 1
        > If IsNull(Me!Addre ssDescription) Then fail = 2
        > If (IsNull(Me!Addr essDescription) Or Me!AddressDescr iption = "Main") And _
        > IsNull(Me!Addre ss1) And _
        > IsNull(Me!Addre ss2) And _
        > IsNull(Me!Addre ss3) And _
        > IsNull(Me!City) And _
        > IsNull(Me!State ) And _
        > IsNull(Me!Zipco de) And _
        > IsNull(Me!Count ry) _
        > Then fail = 3
        > Exit_Here:
        > Exit Sub
        > HandleErr:
        > Select Case Err.Number
        > Case Else
        > modHandler.LogE rr (Me.Form.Name)
        > Resume Exit_Here
        > End Select
        > End Sub[/color]

        Are you referring to your flag FAIL? I so, I doubt it matters much more than am
        gnat on a summer day.

        Do you add phone numbers? No. Make it text
        Do you add social security numbers? No. Make it text
        Do you add zip codes? No. Make it text

        Do you add the dollar amounts in a checkbook. Make it a number, usually currency.

        However, as a flag? Who cares. Use common sense to determine the type of storage
        value for a flag.




        Comment

        • deko

          #5
          Re: When to use integer, when to use string?

          great... that's what I was curious about.

          I find myself needing to pass a variable to other subs to indicates count,
          yes/no, or some other condition... I will start using byte data type instead of
          string.


          Comment

          • Pink Panther

            #6
            Re: When to use integer, when to use string?

            Why don't you use constants and enums rather than

            if this then
            fail = 2
            else
            fail = 3
            end if

            'whatever else you want to return

            'put in a class mode
            Option Compare Database

            Public Enum CustomersDataEr ror
            cdeCouldNotOpen Connection
            cdeCouldNotOpen Recordset
            cdeNoRecords
            cdeCouldNotFind Record
            End Enum

            Public Event CustomersError( error As CustomersDataEr ror)


            Public Function GetCustomerName (ByRef CustomerId As Long) As String

            Dim rs As ADODB.Recordset

            Set rs = New ADODB.Recordset

            rs.Open "Customers" , CurrentProject. Connection

            rs.Filter = "Customerid = " & CustomerId

            If rs.EOF Then
            RaiseEvent CustomersError( CustomersDataEr ror.cdeNoRecord s)
            End If


            End Function

            Not a perfect example but enums are easier to use and remember than
            number like 2, 3 etc and less error prone to spelling mistakes like
            string and will be picked up by the compiler if you are going out of
            bounds. The are also displayed in the Intellisense to make matters
            even sweeter.

            Hit F2 in the code window and check out all the enums and how they are
            used.

            eg

            Property Mode As ConnectModeEnum
            Member of ADODB.Connectio n

            Const adModeRead = 1
            Member of ADODB.ConnectMo deEnum

            Enums can be very useful to make your code more readible and logically
            organised IMO (anyone else got a differing opinion?)

            Peter

            "deko" <dje422@hotmail .com> wrote in message news:<JZrQb.153 28$5u2.8907@new ssvr27.news.pro digy.com>...[color=blue]
            > great... that's what I was curious about.
            >
            > I find myself needing to pass a variable to other subs to indicates count,
            > yes/no, or some other condition... I will start using byte data type instead of
            > string.[/color]

            Comment

            Working...