This seems like a fairly straightforward problem but I keep getting the same exception and I have no idea why.
I can only assume that it has something to do with a misunderstanding of how substring works in VB.NET.
The following code, keeps throwing a ArgumentOutOfRange exception:
<%=Html.Encode(IIf(item.description.Length > 150, item.description.Substring(0, 150), item.description))%>
Now what should happen here is, if item.description is over a 150 characters output the first 150 otherwise output the entire string. Problem is it keeps trying to get the sub-string regardless of the length of the result of the if statement.
Any help would be greatly appreciated.
When you use
IIFyou evaluate all the expressions – the true as well as the false “branches”.This means that for strings that are under 150 characters long, you still call
item.description.Substring(0, 150), causing the error.Perhaps have an
item.ShortDescriptionwhich only ever return the first 150 characters, using a normalIF/THEN.