Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you specify what type the variable will be. 1

Status
Not open for further replies.

ironhide1975

Programmer
Joined
Feb 25, 2003
Messages
451
Location
US
I keep having problems in Classic ASP trying to specify what type of data a variable will be (text,integer etc...) I keep running into problems during replaces or compares with types mismatching. I know a lot of this has been resolved with ASP.Net but how do you specify if a variable is text or numeric in ASP Classic?


 
all variables in asp are "variants" which means that they can be of any type...

for example when i do...

Dim blah

here variable blah is a variant

blah="dng" --> string
blah=1 ---> integer
blah=#01/13/2006# -->date

hope you got the point...

-DNG
 
You can test the variants with functions like:
IsNumeric(x) --> Returns true if x is numeric
IsDate(x) --> Returns true if x is a date

Also you can use the conversion functions:
CStr(x) --> to string
CInt(x) --> to short integer
CLng(x) --> to long integer
CSng(x) --> to single-percision float
CDbl(x) --> to double float
CCur(x) --> to currency
CDate(x) --> to Date
CBool(x) --> to boolean
 
Is there anyway to force an application to make sure a variable is a certain type of variant?

 
Please elaborate... I don't understand the question.
 
when you assign a value to a variable, you are forcing it to be of that type...

-DNG
 
You should be able to do replaces and compares with just about any type od variable (except maybe arrays and objects). It's generally not necessary to force a variable to a specific type, but if you want to check the type you can use the VarType(variable) function to check the type, as well as the two functions outlined by Sheco.

In my opinion, the fact that VBScript is loosely typed is not a problem that was resolved by ASP.Net. Mainly because I don't believe that loosely typed variables are a problem. Python is probably the most strict loosely typed language that I have used and VBScript is probably the least strict. That doesn't make either of them "wrong".
Oh, and technically I can write loosely tryped ASP.Net if I want to, since ASP.Net can be written in many languages (such as Python).

barcode_1.gif
 
My main problem I'm running into is situations where a variable needs to be a bit type. When I type my If then statements they go something like this

Code:
IF suchandsuch =1 or suchandsuch = true or suchandsuch = "1" THEN

Basically, for some reasons my variables come back as 1 (numeric) and sometimes "1" (varchar) or True (bit.) I am sorta looking for a way just to make the variables act consistant.

Another problem I am getting is Nulls in Classic ASP. So in this case the safety checks for this look like

Code:
If suchandsuch = "" OR isNull(suchandsuch) THEN

It appears to me that in ASP.Net you have to exactly specify what a variable will be, and as such I believe this would solve a lot of problems for me. I was inquiring if there is some way to do this in Classic.

So correct me if I'm wrong but I can use ASP to tell me what the variable is?


 
Did you look at the VarType() function that Tarwn suggested?

Also be aware that "VB-style" languages will completely evaluate your conditionals...

What I'm trying to say is...

OK, in C-style you can do this with no errors:
[tt]if ((A>0) || (10/A > 1))[/tt]

But in VB-Style you could get a divide by zero error on this:
[tt]IF ((A>0) OR (10/A > 1)) THEN[/tt]

The reason is that the former will stop after the first item in the OR is true.... but the VB will evaluate ALL of the OR conditions. So in VB you need to write the logic like this:[tt]
IF (A>0) THEN
IF (10/A > 1) THEN[/tt]

I bring this up because, when you have the chance for getting a Null, it might be tempting to check IsNull() as part of a compound conditional with the other condistions being string functions like Trim() or other such string functions will barf if you feed them a Null.

A good way around this for string functions is to take advantage of the fact that a string plus a null equals a string... so an empty string plus a null equals an empty string.
 
Sheco I run into the Null problems a lot when trying to make sure drop down boxes are properly selected. The code usually has to go something like this.

Code:
IF EmployeeID <> "" AND NOT isNull(EmployeeID) THEN
 IF cint(TempEmployeeID) = cint(EmployeeID) THEN 
   %> selected <%
 END IF
END IF

If I try to do the cint compare without checking to see if the employeeid is even populated, then I get an error.

Also I have a perfect example of what I'm dealing with that happened today. I have a form checkbox and my code originally went like this

Code:
IF CustomerCredit = True OR CustomerCredit = "1" THEN

The problem is the checkbox wasn't being selected, I did a response.write and the CustomerCredit was True, and such when I added the "TRUE" check to the code it worked as seen below

Code:
IF CustomerCredit = True OR CustomerCredit = "1" OR CustomerCredit = "True" THEN

This to me means that CustomerCredit is not being see as a BIT by ASP but as a Varchar. How do I force it to look at this like a bit?

Does this make sense?

 
Go ahead and concat the empty string when pulling the values out of your ADO recordset:[tt]
CustomerCredit = rs("CCredit") & ""[/tt]

That will replace nulls with empty strings.

Then you can do this:
[tt]IF CBool(Len(CustomerCredit)) THEN[/tt]

This works because Len() returns a number that is the string length and then CBool() convert zero to False and any other number to True.
 
Sheco said:
Go ahead and concat the empty string when pulling the values out of your ADO recordset:
CustomerCredit = rs("CCredit") & ""

That's excellent advice. I make it a standard practice.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
so
Code:
IF CBool(Len(CustomerCredit)) THEN
will make the item True or False then?

 
Len(string) returns an integer

CBool(expression) returns True or False.

When expression is non-zero, CBool() returns True... if expression evaluates to zero CBool() returns False.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top