Important alert: (current site time 7/15/2013 11:09:02 PM EDT)
 

VB icon

A Better cBool function

Email
Submitted on: 7/8/2004 2:35:53 PM
By: Mark Kahn  
Level: Advanced
User Rating: By 1 Users
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 9158
(About the author)
 
     I've run into some problems with the cbool function recently, rediculous things like cbool(null) throwing an error. So I re-wrote it. For the most part it acts exactly as the built in cbool function but it'll never throw an error. Although the main point of this is to show that you can re-write built-in vb functions, which I just randomly discovered :-)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: A Better cBool function
' Description:I've run into some problems with the cbool function recently, rediculous things like cbool(null) throwing an error. So I re-wrote it. For the most part it acts exactly as the built in cbool function but it'll never throw an error.
Although the main point of this is to show that you can re-write built-in vb functions, which I just randomly discovered :-)
' By: Mark Kahn
'
' Side Effects:some behavior that might not be 100% intuitive. feel free to change it:
cbool(object) = true
cbool("1/1/1900") = false
cbool("1/2/1900") = true
cbool("asdf") = true
cbool("most other strings") = true
cbool(array())= false ' array is empty
cbool(array(1))= true ' array not empty
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=8912&lngWId=4'for details.'**************************************

function cbool(str)
	cbool = true
	SELECT CASE varType(str)
		case 0: cbool = false
		case 1: cbool = false
		case 2: if str=0 then cbool = false
		case 3: if str=0 then cbool = false
		case 4: if str=0 then cbool = false
		case 5: if str=0 then cbool = false
		case 6: if str=0 then cbool = false
		case 7: if str=2 then cbool = false
		case 8
		if Isnumeric(str) then
			if str=0 then cbool = false
		end if
		if instr("|false|no|0|not|null|", "|" & str & "|")>0 then cbool = false
		case 9: cBool = true
		case 10:cbool = false
		case 11:cbool = str
		case 12:cBool = true
		case 13:cBool = true
		case 14:if str=0 then cbool = false
		case 17:if cint(str)=0 then cbool = false
		case 8192,8204: if ubound(str)<0 then cbool = false
	END SELECT
end function


Other 19 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/12/2007 11:24:29 AMmike

I too have run into this problem with the cbool function. This solution is quite common. - I give it a 4.
My only suggestion is to replace the numbers in the Case statements with the actual varType constants. That way six months from know you will not have to go and lookup the values when your trying to figure out why something is not working.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.