
How do I check variable type ? Help Please
How do I check variable type that would give a message to users that entered other variable other than integers? Here is my script<BR><html> <BR><head><title>Online Calculator</title></head> <BR><body> <BR><H3>Here's Your Answer</H3> <BR><BR><% <BR><BR>iA=CInt(Request.form("Avalue")) <BR>iB=CInt(Request.form("Bvalue")) <BR>optype=Request.form("operation") <BR><BR><BR>function addition(iB) <BR> addition=Cint(iA+iB) <BR>End function <BR><BR>function subtraction(iB) <BR> subtraction=Cint(iAiB) <BR>End function <BR><BR>function multiplication(iB) <BR> multiplication=Cint(iA*iB) <BR>End function <BR><BR>function division(iB) <BR> division=cint(iA / iB) <BR>End function <BR><BR>function exponents(iB) <BR> exponents=Cint(iA ^ iB) <BR>End function <BR><BR>sub divrules <BR> Response.write "<b>You can't divide by zero please choose another value for B!</b>" <BR>End sub <BR><BR><BR><BR>select case optype <BR> case "add" <BR> isum=addition(iB) <BR> Response.write iA <BR> Response.write " + " <BR> Response.write iB <BR> Response.write " = " <BR> Response.write isum <BR> <BR> case "subtract" <BR> iminus=subtraction(iB) <BR> Response.write iA <BR> Response.write "  " <BR> Response.write iB <BR> Response.write " = " <BR> Response.write iminus <BR> case "multiply" <BR> imult=multiplication(iB) <BR> Response.write iA <BR> Response.write " X " <BR> Response.write iB <BR> Response.write " = " <BR> Response.write imult <BR> Case "divide" <BR> If iB=0 Then <BR> Call divrules <BR> Else <BR> idiv=division(iB) <BR> Response.write iA <BR> Response.write " / " <BR> Response.write iB <BR> Response.write " = " <BR> Response.write idiv <BR> End If <BR> case "exponent" <BR> iexpo=exponents(iB) <BR> Response.write iA <BR> Response.write " to the power of " <BR> Response.write iB <BR> Response.write " = " <BR> Response.write iexpo <BR> Case Else <BR> <BR>End select <BR>%> <BR><BR></body> <BR></html>

Homework, huh?
It's not a question of the VARIABLE type.<BR><BR>The type of <BR> Request.Form("...anything...")<BR>is *ALWAYS* a STRING. So you could do<BR> Response.Write TypeName( Request.Form("Avalue") )<BR>but that's going to *always* respond with<BR> String<BR><BR>What you *really* want to know is<BR> Can that value be CONVERTED to an integer?<BR><BR>And that's an entirely different question.<BR><BR>You CAN use the <BR> IsNumeric( ... )<BR>function to find out if the string can be converted to a valid NUMBER. But that number might be something like<BR> 8.817281999217E+87<BR>which wouldn't be a valid VBS integer, at all.<BR><BR>But it will check to see if the number is a valid real (DOUBLE) number.<BR><BR>So how about you start with that and then try to figure out how to check if a real number can be converted to a valid integer? No function to tell you that. Just logic.<BR><BR>***********<BR><BR>COMMENTARY:<BR><B R>Your functions are all poorly written. If you are going to pass in the value of iB, then why would you not *also* pass in the value of iA? <BR><BR>Of course, I could ask why you bother with the functions, in the first place.<BR><BR> case "add"<BR> Response.Write iA & " + " & iB & " = " & (iA+iB) & "<P>"<BR> case "subtract"<BR> ...<BR><BR>But maybe your teacher wanted you to use functions. Fine. But then make them *real* functions, with two arguments.<BR><BR>

My solution
Use Isnumeric() to see if it's numeric input. If so, then see if Instr(Cstr(iA), ".") > 0. If so, iA has a decimal and must therefore be something other than an integer. Do the same for iB.<BR><BR>Better ideas, anyone?

Try this...
<%<BR>num = 3000000000.0<BR><BR>Response.Write CStr( num ) & "<P>"<BR><BR>inum = CLng(num)<BR><BR>%><BR><BR>Hmmm???<BR><BR>

Well...
just using 3.0 would cause Cstr() to make it 3. The length of it doesn't help either, though. <BR><BR>I suppose you could have some clientside javascript checking the keys pressed and prevent the decimal from ever being entered.

I suppose you would recommend...
using regular expressions to solve the problem based on postings I've seen from you in the past.

I can tell you didn't TRY the code
The CLNG() function call gets an error.<BR><BR>Can you see why?<BR><BR>You *could* say forget the CLNG. Just leave the numbers as doubles and do the arithmetic as is.<BR><BR>Of course, he isn't testing for arithmetic overflow in any of the operations, so maybe that is the most sensible thing. But assuming that the homework assignment was to actually work with VBS integers, that doesn't answer the question.<BR><BR>

No...it depends entirely...
...upon how the teacher worded the assignment.<BR><BR>Your suggestion is okay if teach just said "make it work with integers". But if he/she said "make it work with integer VARIABLES" then it falls short.<BR><BR>

No, I did try it.
I know the Clng() gets an error. What has that got to do with Cstr()? I'm evaluating the Cstr() because that was my suggestion. I dropped out the Clng() because I didn't know what you were trying to prove with that (until just now when I read this post from you) and stuck with the Cstr() just to see how it would handle the number you posted. <BR><BR>Yes, arithmetic overflow could be an issue, but I'm just trying to show how to figure out if it is an integer or not.

Okay, we're on same page...
Like I said, it depends on what the assignment was.<BR><BR>Oh, what the heck. I'd do it like this:<BR><BR><%<BR>a = Request("a")<BR>If Not IsNumeric(a) Then<BR> Response.Write "a is not a valid number"<BR> Response.End<BR>End If<BR>a = CDbl(a) ' must work if IsNumeric is true<BR>ia = "Nope"<BR>If a = Int(a) Then<BR> On Error Resume Next<BR> ia = CLng(a)<BR> On Error GoTo 0<BR>End If<BR>If ia = "Nope" Then<BR> Response.Write "a is not in the range of valid VBS integers"<BR> Response.End<BR>End If<BR>%><BR><BR>Clumsy, but accurate.<BR><BR>But of course, 2000000000 + 2000000000 will then no longer be a valid LONG integer.<BR><BR>
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

