Null checking and if-blocks


this complaining possible null reference temp on line 7.

1    function test(byval source as ienumerable(of byte), byval format as stringas string 
2         dim temp = trycast(source, icollection(of byte))  
3         dim result  
4         if temp is nothing or format is nothing then 
5             result = new text.stringbuilder()  
6         else 
7             result = new text.stringbuilder(temp.count * format.length)  
8         end if 
9         return result.tostring  
10     end function 
 

hi jonathan,

for disjunctive conditions, want use orelse, not bitwise or operation. way, static checker can determine different control paths.

function test(byval source as ienumerable(of byte), byval format as stringas string    
     dim temp = trycast(source, icollection(of byte))     
     dim result     
     if temp is nothing orelse format is nothing then    
         result = new text.stringbuilder()     
     else    
         result = new text.stringbuilder(temp.count * format.length)     
     end if    
     return result.tostring     
end function    
 

with orelse, checker figures out.

-maf


DevLabs  >  Code Contracts



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'