By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,500 Members | 830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,500 IT Pros & Developers. It's quick & easy.

When to use integer, when to use string?

P: n/a
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!AddressDescription) Then fail = 2
If (IsNull(Me!AddressDescription) Or Me!AddressDescription = "Main") And _
IsNull(Me!Address1) And _
IsNull(Me!Address2) And _
IsNull(Me!Address3) And _
IsNull(Me!City) And _
IsNull(Me!State) And _
IsNull(Me!Zipcode) And _
IsNull(Me!Country) _
Then fail = 3
Exit_Here:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
modHandler.LogErr (Me.Form.Name)
Resume Exit_Here
End Select
End Sub
Nov 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
-----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:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

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

iQA/AwUBQBGCTYechKqOuFEgEQKutACgwZC8x4rrgvR6iOIkyI6XG1 r8hXcAnAwR
CalVNm+9YyHBEAe793Wn+3As
=80Fw
-----END PGP SIGNATURE-----
deko wrote:
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!AddressDescription) Then fail = 2
If (IsNull(Me!AddressDescription) Or Me!AddressDescription = "Main") And _
IsNull(Me!Address1) And _
IsNull(Me!Address2) And _
IsNull(Me!Address3) And _
IsNull(Me!City) And _
IsNull(Me!State) And _
IsNull(Me!Zipcode) And _
IsNull(Me!Country) _
Then fail = 3
Exit_Here:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
modHandler.LogErr (Me.Form.Name)
Resume Exit_Here
End Select
End Sub


Nov 12 '05 #2

P: n/a
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
www.veraccess.com

"MGFoster" <me@privacy.com> wrote in message
news:jn*******************@newsread1.news.pas.eart hlink.net...
-----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:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

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

iQA/AwUBQBGCTYechKqOuFEgEQKutACgwZC8x4rrgvR6iOIkyI6XG1 r8hXcAnAwR
CalVNm+9YyHBEAe793Wn+3As
=80Fw
-----END PGP SIGNATURE-----
deko wrote:
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!AddressDescription) Then fail = 2
If (IsNull(Me!AddressDescription) Or Me!AddressDescription = "Main") And _ IsNull(Me!Address1) And _
IsNull(Me!Address2) And _
IsNull(Me!Address3) And _
IsNull(Me!City) And _
IsNull(Me!State) And _
IsNull(Me!Zipcode) And _
IsNull(Me!Country) _
Then fail = 3
Exit_Here:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
modHandler.LogErr (Me.Form.Name)
Resume Exit_Here
End Select
End Sub

Nov 12 '05 #3

P: n/a
deko wrote:
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!AddressDescription) Then fail = 2
If (IsNull(Me!AddressDescription) Or Me!AddressDescription = "Main") And _
IsNull(Me!Address1) And _
IsNull(Me!Address2) And _
IsNull(Me!Address3) And _
IsNull(Me!City) And _
IsNull(Me!State) And _
IsNull(Me!Zipcode) And _
IsNull(Me!Country) _
Then fail = 3
Exit_Here:
Exit Sub
HandleErr:
Select Case Err.Number
Case Else
modHandler.LogErr (Me.Form.Name)
Resume Exit_Here
End Select
End Sub


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.


Nov 12 '05 #4

P: n/a
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.
Nov 12 '05 #5

P: n/a
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 CustomersDataError
cdeCouldNotOpenConnection
cdeCouldNotOpenRecordset
cdeNoRecords
cdeCouldNotFindRecord
End Enum

Public Event CustomersError(error As CustomersDataError)
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(CustomersDataError.cdeNoRecords)
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.Connection

Const adModeRead = 1
Member of ADODB.ConnectModeEnum

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

Peter

"deko" <dj****@hotmail.com> wrote in message news:<JZ******************@newssvr27.news.prodigy. com>...
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.

Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.