How is IIF evaluated?

T

tshad

I have the following:
IIF(Shift2StartHour.SelectedIndex + Shift2StartMinute.SelectedIndex +
Shift2StartAmPm.SelectedIndex = 0, _
-1,GetTimeOfDay(Shift2StartHour.SelectedValue,Shift2StartMinute.SelectedValue,Shift2StartAmPm.SelectedValue)

I am getting an error on the GetTimeOfDay function that expects 2 integers
and a string - "Cast from string "" to type 'Integer' is not valid.

This is true, if the SelectedIndex were 0 - But my statement says if all 3
selected indexes are 0, then send back a -1. If I change the GetTimeOfDay
function to 0, then it does send back a -1.

I assume that it evaluates both the true and false results regardless of
what the result is.

Is there a way around this?

Thanks,

Tom
 
M

Marina

IIF is a function, not a language construct. As such, all of its parameters
are evaluated prior to the function getting called - just like any other
function. So, if there is an error evaluating one of the arguments, you will
get an exception. Even if that argument is the one that won't be returned
because of the condition. Doesn't matter, because IIF is just another
function and nothing special. You can write your own equivalent, since IIF
doesn't provide anything special anyway.
 
S

S. Justin Gengo

tshad,

SelectedValue returns a string not an integer. You need to convert the
SelectedValue to an integer.

GetTimeOfDay(CType(Shift2StartHour.SelectedValue, Int32),
CType(Shift2StartMinute.SelectedValue, Int32),
CType(Shift2StartAmPm.SelectedValue, Int32))

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
S

S. Justin Gengo

Oops,

I got going there and coverted everything! Of course you don't convert AM,
PM to an Int32.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
T

tshad

Marina said:
IIF is a function, not a language construct. As such, all of its
parameters are evaluated prior to the function getting called - just like
any other function. So, if there is an error evaluating one of the
arguments, you will get an exception. Even if that argument is the one
that won't be returned because of the condition. Doesn't matter, because
IIF is just another function and nothing special. You can write your own
equivalent, since IIF doesn't provide anything special anyway.

That was what I thought, but I wasn't sure.

Thanks,

Tom
 
T

tshad

S. Justin Gengo said:
tshad,

SelectedValue returns a string not an integer. You need to convert the
SelectedValue to an integer.

Actually, it does convert the data.

My problem was that in the 0 position, the value was set to "" and this
caused the problem. I changed it to "0" and it all works fine.

I was just curious that it would check the parameters if it wasn't going to
do the call. Apparently it does and that was what was causing the error.

Thanks,

Tom
 
T

tshad

S. Justin Gengo said:
Oops,

I got going there and coverted everything! Of course you don't convert AM,
PM to an Int32.
Right.

In the GetTimeOfDay, I am passing a string in the 3rd parameter.

Tom
 
J

Jason Kester

Yeah. Quit using IIFs. I mean come on, you wrote the thing and even
you can't decipher what it's trying to do.

Split that rat's nest out into seven lines of code, and make sure that
it's apparent what is happening with a quick glance. You'll have to
actually declare variables and cast the result from
Shift2StartHour.SelectedValue, and in the process you'll discover that
it's handing you a string not an integer.

The only thing that we, as programmers have to judge the talent of
other programmers is their source code. If we see sloppy, half-assed
code like that, we'll naturally assume you're a sloppy, half-ass
programmer and cease trying to help you. Write every line of code as
though it was going to be engraved on your tombstone as the one thing
the world will judge you by for eternity.

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 
T

tshad

Jason Kester said:
Yeah. Quit using IIFs. I mean come on, you wrote the thing and even
you can't decipher what it's trying to do.

Of course, I didn't know what it was doing.

That's why I was asking. And if you look at what I said, I surmised what it
was doing and was just trying to get confirmation on that.
Split that rat's nest out into seven lines of code, and make sure that
it's apparent what is happening with a quick glance. You'll have to
actually declare variables and cast the result from
Shift2StartHour.SelectedValue, and in the process you'll discover that
it's handing you a string not an integer.

So you are saying that we shouldn't use functions??????

That is exactly what this was doing. I was using a function!

This function is perfectly valid. And it works fine if my first listitem
has an integer (such as -1) as the value.

Are you say that the IIF is not clear?????

IIF(Shift2StartHour.SelectedIndex + Shift2StartMinute.SelectedIndex +
Shift2StartAmPm.SelectedIndex = 0, _
-1,GetTimeOfDay(Shift2StartHour.SelectedValue,Shift2StartMinute.SelectedValue,Shift2StartAmPm.SelectedValue)

IF all 3 dropdowns are selecting the 1st position (SelectedIndex = 0) then
pass back a -1 (no choice was made)
else
Call the GetTimeOfDay function to put the 3 values into an integer
(3hour, 15minute, am = 315).
end if

Seems pretty clear to me.

This is identical to C#s:
Status = age > 20 ? "adult" : "child"
The only thing that we, as programmers have to judge the talent of
other programmers is their source code. If we see sloppy, half-assed
code like that, we'll naturally assume you're a sloppy, half-ass
programmer and cease trying to help you. Write every line of code as
though it was going to be engraved on your tombstone as the one thing
the world will judge you by for eternity.

What is sloppy about it?

This particular line will be used 6 times in one of my pages, where the only
difference is the variables Shift1StartHour, Shift1EndHour, Shift2StartHour,
etc.

Are you saying I should write out 7 lines of code 6 times?

Or write a function (why would I do that if IIF does the job).

Why don't you like IIF?

A good programmer doesn't write new code when there is a function available
and writes new functions if none of the functions available does the job.

Tom
 
J

Jason Kester

tshad said:
Are you say that the IIF is not clear?????

IIF(Shift2StartHour.SelectedIndex + Shift2StartMinute.SelectedIndex +
Shift2StartAmPm.SelectedIndex = 0, _
-1,GetTimeOfDay(Shift2StartHour.SelectedValue,Shift2StartMinute.SelectedValue,Shift2StartAmPm.SelectedValue)

That is exactly what I'm saying. That is just plain ugly. The fact
that it's doing wacky things that you didn't expect should have tipped
you off.

private int GetTimeOfDay(HtmlSelect selStartHour, HtmlSelect
selStartMinute, HtmlSelect selStartAmPm)
{
if (selStartAmPm.SelectedIndex == 0
&& selStartHour.SelectedIndex == 0
&& selStartMinute == 0)
{
return -1;
}

return GetTimeOfDay(selStartHour.Value, selStartMinute.Value,
selStartAmPm.Value);
}


There you go, one more signature for your GetTimeOfDay function, and
you never have to see that hairball IIF again.

Look, immediate ifs have their uses, but they can get unreadable in a
hurry. A good rule of thumb is to never use an IIF (or ()?: ) that
needs a linebreak.


A good programmer doesn't write new code when there is a function available
and writes new functions if none of the functions available does the job.

Exactly my point. All the best!


Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top