If comparision operator is one of these or contains ?

  • Thread starter Thread starter jobs
  • Start date Start date
J

jobs

In the case where I need to check to see if a variable is one of many
values

IIF (( (Container.DataItem("ProcessStateId") = 0) or
(Container.DataItem("ProcessStateId") = 2) or
(Container.DataItem("ProcessStateId") = 4) ) ,"TRUE","FALSE")

Is there a smarter way to do this? and where would I have found that on
MSDN or Language refrences pages?

Thanks.
 
Why not use a select case like the following?

dim ReturnString as string

select case Container.DataItem("ProcessStateId")
case 0, 2, 4
ReturnString = "True"
case else
ReturnString = "False"
end select

I like using a Select Case statement over IIF because (imo) they are
much easier to read, modify, and maintain.

Just my 2 cents,

Seth Rowe
 
I thought about that.. and that might work.. I need to squeze the code
inside the Enable propertty of a button under a datagrid of asp.net
page.

<asp:Button ID="Rate" Text="Rate"
CommandArgument='<%#Eval("ProcessId") %>' CommandName="Rate"
Enabled='<%# IIF (( (Container.DataItem("ProcessStateId") = 0) or
(Container.DataItem("ProcessStateId") = 2) or
(Container.DataItem("ProcessStateId") = 4) ) ,"TRUE","FALSE") %>'
runat="server" />
 
Why not write it as a function and call it from the button press code?
Sid.
 
Im setting the button's enabled property at Early binding conditioning
based on data in the specific row. Keep in mind if I will do this in
late binding I will have to loop select again and then set that
property for each row.

Write a function that returns a boolean called from inside the enabled
property and pass it the bound item I need to condition from? Is that
really possible? If so, please send me the code.

Thanks.
 
jobs said:
In the case where I need to check to see if a variable is one of many
values

IIF (( (Container.DataItem("ProcessStateId") = 0) or
(Container.DataItem("ProcessStateId") = 2) or
(Container.DataItem("ProcessStateId") = 4) ) ,"TRUE","FALSE")

Is there a smarter way to do this? and where would I have found that on
MSDN or Language refrences pages?

Thanks.

I'd suggest you use short-sircuit operators ("OrElse", "AndAlso"),
instead of bitwise operators ("Or", "And"). This is because
short-ciruit operators are specific for boolean values. As a
consequence, if the first test yelds true, the other two won't be
evaluated. In other words, instead of evaluating all three expressions,
the runtime would evaluate each expression until it finds a true one,
and short-circuit the others because it "knows" that the result will be
true.

If you replace the operators, maybe you don't need the iif. I guess
that a boolean value, ToString, will already return "True" or "False".

HTH.

Regards,

Branco.
 
jobs said:
I thought about that.. and that might work.. I need to squeze the code
inside the Enable propertty of a button under a datagrid of asp.net
page.

<asp:Button ID="Rate" Text="Rate"
CommandArgument='<%#Eval("ProcessId") %>' CommandName="Rate"
Enabled='<%# IIF (( (Container.DataItem("ProcessStateId") = 0) or
(Container.DataItem("ProcessStateId") = 2) or
(Container.DataItem("ProcessStateId") = 4) ) ,"TRUE","FALSE") %>'
runat="server" />

Then you need a fuction like this:

Function IsOneOf( _
ByVal iFind as Integer _
, ByVal ParamArray iValues As Integer() _
) as Boolean

For Each iVal As Integer In iValues
If iFind = iVal Then
Return True
End If
Next

Return False
End Function

then

<asp:Button
ID="Rate"
Text="Rate"
CommandArgument='<%#Eval("ProcessId") %>'
CommandName="Rate"
Enabled='<%# IIf(IsOneOf(CInt(Container.DataItem("ProcessStateId")),
0, 2, 4), "TRUE", "FALSE") %>'
runat="server"
/>

HTH,
Phill W.
 
Nice. Thanks.
Then you need a fuction like this:

Function IsOneOf( _
ByVal iFind as Integer _
, ByVal ParamArray iValues As Integer() _
) as Boolean

For Each iVal As Integer In iValues
If iFind = iVal Then
Return True
End If
Next

Return False
End Function

then

<asp:Button
ID="Rate"
Text="Rate"
CommandArgument='<%#Eval("ProcessId") %>'
CommandName="Rate"
Enabled='<%# IIf(IsOneOf(CInt(Container.DataItem("ProcessStateId")),
0, 2, 4), "TRUE", "FALSE") %>'
runat="server"
/>

HTH,
Phill W.
 
Jason,
Write a function that returns a boolean called from inside the enabled
property and pass it the bound item I need to condition from? Is that
really possible? If so, please send me the code.
Yes its possible, as IIf *is* a function!

Where Whatever is defined in your code behind:

Protected Function Whatever(processStateId As String) As String
Select Case processStateId
Case 0, 2, 4
Return "True"
Case Else
Return "False"
End Select
End Function

NOTE: I would consider Defining Whatever as a boolean instead of a string:

Protected Function Whatever(processStateId As String) As Boolean

Of course you will want to define a more appropriate name for the Whatever
function.
 

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

Back
Top