why wont this work?

  • Thread starter Thread starter Greg Merideth
  • Start date Start date
G

Greg Merideth

class test
{
private string _processName;

public string GetProcessID()
{
return (_processName.Length>1)?_processName:"No Name";
}
}

It compiles but on the method call I get
SystemNullReferenceException.
 
It won't work because _processName is unassigned and therefore null. So you
can't call members of string.

Try something like this instead:

return (_processName != null && _processName.Length > 1)?_processName :
"No Name";

-- or --
return (_processName != null && _processName.Length != "" )?_processName
: "No Name";

HTH
Brian W
 
class test
{
private string _processName;

public string GetProcessID()
{
return (_processName.Length>1)?_processName:"No Name";
}
}

It compiles but on the method call I get
SystemNullReferenceException.

Greg,

_processName is null. Try:

private string _processName = string.Empty;


Chris.
 
Another member of the test class i'm working on puts a
value into _processName but I see what you mean. The
method doesn't ever know if _processName ever gets
initialized so it complains.

Thanks.
 
Back
Top