LogoASPAlliance: Articles, reviews, and samples for .NET Developers
A More Convenient cInt
by Dan Powderhill
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 17358/ 42

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-2021  |  Page Processed at 2021-12-05 6:15:58 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search