c# - Best way to handle null properties on a method parameter -


when checking method's parameter, throw argumentnullexception if it's null. see first line in method below. properties on parameter shouldn't null? if try handle them same way, code analysis error:

ca2208 instantiate argument exceptions correctly method 'priorityderatingcomponentlogic.createitem(ivsimulation)' passes 'ivsimulation.solarpanel' 'paramname' argument 'argumentnullexception' constructor. replace argument 1 of method's parameter names. note provided parameter name should have exact casing declared on method.

public deratingcomponentbase createitem(ivsimulation ivsimulation) {     if (ivsimulation == null) { throw new argumentnullexception("ivsimulation"); }     if (ivsimulation.solarpanel == null) { throw new argumentnullexception("ivsimulation.solarpanel"); }     if (ivsimulation.globalequipment == null) { throw new argumentnullexception("ivsimulation.globalequipment"); }      // ... method body here } 

is ca error should suppress, or there generally-accepted way better handle this? perhaps issue upstream , shouldn't have check properties being null @ point?

throwing argumentnullexception intended indicate argument null. however, when 1 of argument components null, argument isn't, proper exception argumentexception name of argument parameter:

if (ivsimulation.globalequipment == null) {     throw new argumentexception("globalequipment cannot null", "ivsimulation");  } 

this provides information error in message, while specifying ivsimulation parameter name.

note: assume have no choice of validating globalequipment in constructor of ivsimulation, because throwing argumentnullexception valid choice in constructor.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -