Join multiple similar IF'S

# Thread: Join multiple similar IF'S

1. Junior Member
Join Date
Dec 1969
Posts
11

## Join multiple similar IF'S

Hi, <BR><BR>I have some IF statemnets like this:<BR><BR>&#060;%<BR>a = request.form("Field1") <BR>b = request.form("Field2") <BR><BR>If a = "" Then<BR>a = "0"<BR>Else<BR>End if <BR><BR>If b = "" Then<BR>b = "0"<BR>Else<BR>End if <BR><BR>and so on till i get to letter J (that&#039;s about 9 IF statements)<BR>%&#062;<BR><BR>Is there a way to join these in one MASTER IF statement, something like:<BR><BR>&#060;%<BR>IF a TO j = "" THEN<BR>the letter that is "" should be = "0"<BR>ELSE <BR>END IF<BR>%&#062;<BR><BR>Thanks in advance, <BR><BR>A. Porobic<BR>

2. Senior Member
Join Date
Dec 1969
Posts
2,854

## no, but...

You don&#039;t need the <BR><BR>ELSE<BR>End IF<BR><BR>Just do it like so...<BR><BR>If trim("" & a)="" then a="0"<BR>If trim("" & b)="" then b="0"<BR>If trim("" & c)="" then c="0"<BR>etc etc....

3. Junior Member
Join Date
Dec 1969
Posts
11

## I did it....

And it works great, <BR><BR>Thanx man

4. Senior Member
Join Date
Dec 1969
Posts
1,024

## RE: Join multiple similar IF'S

If your form elements are named Field1, field2,...field9, field10 etc.<BR><BR>You could do<BR><BR>dim arrTest(1,0)<BR><BR>dim intElements<BR>intElements = 0<BR><BR>dim varFormContent<BR><BR>For i = asc("a") to asc("j") &#039;(or 1-9)<BR> dim y<BR> y=i-96 &#039;to move it to 1-9 (from ascii)<BR><BR> varFormContent = request.form("form" &y)<BR><BR> if varFormContent = "" then <BR> redim preserve arrTest(1,intElements)<BR> <BR> arrTest(0, intElements) = chr(i)<BR> arrTest(1, intElements) = varFormContent<BR><BR> intElements=intElements +1<BR> end if<BR>next<BR><BR>This would give you an array with only the elements that have a value; their &#039;name&#039; in the &#039;left column&#039; and the value in the right.<BR><BR><BR><BR>cp<BR><BR><BR>

5. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## I disagree, partially.

I agree w/ removing the unused "Else" part.<BR><BR>But, I don&#039;t know how much code reading that you have done, but I have found that if:<BR>If Expr Then<BR> ... code here ...<BR>End If<BR><BR>Is easier to read and digest than some places using a block If, and others not.<BR><BR>Sure, it saves a single line of typing, but so many Editors autocomplete things like that, I don&#039;t see that it&#039;s such a big deal.<BR><BR>Just my \$0.02. For me, all things about programming come back to maintainability and readability.

6. Senior Member
Join Date
Dec 1969
Posts
2,854

## fair enough

Thats a valid point, and I also use the <BR><BR>if expr then<BR><BR>end if<BR><BR>format, Maintainability is a key factor, but at the same time, you could argue (I&#039;m not trying to) that the compiler reads the<BR><BR>if Expr then &#039;Do Something<BR><BR>faster as its on one line...<BR><BR>Your \$0.02c just increased in value :-)<BR>

7. Senior Member
Join Date
Dec 1969
Posts
4,697

How about a function<BR><BR>Function RequestFormValueWithDefault(ElementName,DefaultVal ue)<BR> RequestFormValueWithDefault= Request.Form(ElementName)<BR>if Len(temp)=0 then<BR> RequestFormValueWithDefault = DefaultValue<BR>end if<BR>End Function<BR><BR>Then you can <BR>a = RequestFormValueWithDefault("Field1","0")<BR>b = RequestFormValueWithDefault("Field2","0")<BR>

8. Senior Member
Join Date
Dec 1969
Posts
4,697

## RE: How about a function : TYPO

if Len(RequestFormValueWithDefault)=0 then

9. God
Senior Member
Join Date
Dec 1969
Posts
18,177

## I would bet that it *is*..

.. faster to do it on a single line, like you said.<BR><BR>But, I can&#039;t imagine that it&#039;s a measurable difference. And, if you are getting into measuring that tiny bit, then you shouldn&#039;t be writing in ASP in the first place!<BR><BR>Now, our combine \$0.02 just increased in value, again. :)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•