Can you please tell me why this line of code gives error:- “Object reference not set to instance of an object”
searchstr = IIf(
Not searchstr Is Nothing
AndAlso searchstr.Length > 0,
searchstr.Replace("Desig_id", "designation_id"), "")
Also can you provide alternative to make this type of validation?
The
IIffunction isn’t behaving how you think it does. It’s not a ternary-style operator a la C++ or C#. It’s just a normal function, which means both your conditional check and the searchstr.Replace are being evaluated before passing it toIIf. If searchstr is null, searchstr.Replace(…) is going to throw a NullReferenceException, even though you expected it wouldn’t be called at all.The If operator was added in VS2008 to support this, but if you’re in 2005, you have to expand it to an If/Else block: