3

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?

nfechner
  • 17,295
  • 7
  • 45
  • 64
Sujit Prabhakaran
  • 295
  • 3
  • 12
  • 26
  • I think because the searchstr is null or nothing and you are trying to check its length and do replaces inside it – Alex Feb 21 '12 at 08:11
  • See also http://stackoverflow.com/questions/28478/if-iif-and-if. http://stackoverflow.com/questions/28377/iif-vs-if. http://stackoverflow.com/questions/4431326/what-is-the-purpose-of-the-iif-in-vb. Etc – MarkJ Feb 21 '12 at 08:48
  • possible duplicate of [Using VB.NET IIF I get NullReferenceException](http://stackoverflow.com/questions/428959/using-vb-net-iif-i-get-nullreferenceexception) – MarkJ Feb 21 '12 at 08:49

3 Answers3

6

The IIf function 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 to IIf. 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:

If Not searchstr Is Nothing AndAlso searchstr.Length > 0 Then
    searchstr = searchstr.Replace("Desig_id", "designation_id")
Else
    searchstr = ""
End If
Jason Malinowski
  • 18,148
  • 1
  • 38
  • 55
  • 3
    More precisely: it not depends on Visual Studio version, its about .NET Framework version. You are probably using .NET 2.0. – Kamil Feb 21 '12 at 08:16
  • Well explained, sir. Ever since learning this very thing, I have changed any `IIf()` I come across (in a .NET 3.5 or higher project) to `If()`, since that is almost always preferable. – GregL Feb 21 '12 at 08:21
  • 4
    @Kamil: good catch, but it's more subtle than that! Since this particular feature needs no CLR support nor any class library support, it only depends on you having a compiler that's compiling language version VB9 or later. Using VS2010 right now, I can use the If operator in a project targeting .NET 2.0. :-) – Jason Malinowski Feb 21 '12 at 08:25
  • @Kamil: thanks for bringing that up. You had me running to the compiler as soon as you I saw your comment... – Jason Malinowski Feb 21 '12 at 08:49
1

iif is a function, not an operator like the c# ? that means there is no expression short circuiting. Also, your code should look like:

searchstr = IIf(
    String.IsNullOrEmpty(searchstr),
    searchstr.Replace("Desig_id", "designation_id"), "")  
linkerro
  • 5,318
  • 3
  • 25
  • 29
0

For readability you might be better writing it as

If string.isnullorempty(searchstr) Then
  searchstr.Replace("Desig_id","designation_id")
else
  searchstr = string.empty
End if

The isnullorempty checks both the length and whether the string is initialised.

munnster79
  • 578
  • 3
  • 16