AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=120&pId=-1
A More Convenient cInt
page
by Dan Powderhill
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 19243/ 37

Introduction
When you work with forms and databases a lot you will often want to save the numerical value of a form element, in particular a group of radio buttons, to a database. In general this is easy, however if the field is not mandatory and the user chooses not to answer then the value which gets passed is null, or a blank string. If you pass the cInt function a null value, blank string, or non-numerical string it raises an exception.

Whilst that in itself isn’t a problem as you can write a routine to check the value before using cInt and make sure it is a valid number, it is easier simply to adapt the cInt function to handle them.
cInt2 Function

The function below, cInt2, does just that and can be used in the same way as the regular cInt function.

function cInt2(strInput)
     'Remove any trailing or leading spaces
     strInput = trim(strInput)

     'If the input is null, blank, or not a number then set it to zero
     if strInput = "" or isNull(strInput) or not isNumeric(strInput) then strInput = "0"

     ' Convert it to an integer using the regular cInt function and return it.
     cInt2 = cInt(strInput)
end function

This function could also be adapted to cater for converting the “on” value passed by checked checkboxes to a given integer.


Product Spotlight
Product Spotlight 

©Copyright 1998-2019 ASPAlliance.com  |  Page Processed at 2019-12-11 7:55:46 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search