why wont this work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg Merideth

    #1

    why wont this work?

    class test
    {
    private string _processName;

    public string GetProcessID()
    {
    return (_processName.L ength>1)?_proce ssName:"No Name";
    }
    }

    It compiles but on the method call I get
    SystemNullRefer enceException.

  • Brian W

    #2
    Re: why wont this work?

    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.Le ngth > 1)?_processName :
    "No Name";

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

    HTH
    Brian W



    "Greg Merideth" <gmerideth.remo ve@this.uclabs. com> wrote in message
    news:14c601c3a8 8a$4d7fa4f0$a60 1280a@phx.gbl.. .[color=blue]
    > class test
    > {
    > private string _processName;
    >
    > public string GetProcessID()
    > {
    > return (_processName.L ength>1)?_proce ssName:"No Name";
    > }
    > }
    >
    > It compiles but on the method call I get
    > SystemNullRefer enceException.
    >[/color]


    Comment

    • Chris R. Timmons

      #3
      Re: why wont this work?

      "Greg Merideth" <gmerideth.remo ve@this.uclabs. com> wrote in
      news:14c601c3a8 8a$4d7fa4f0$a60 1280a@phx.gbl:
      [color=blue]
      > class test
      > {
      > private string _processName;
      >
      > public string GetProcessID()
      > {
      > return (_processName.L ength>1)?_proce ssName:"No Name";
      > }
      > }
      >
      > It compiles but on the method call I get
      > SystemNullRefer enceException.
      >
      >[/color]

      Greg,

      _processName is null. Try:

      private string _processName = string.Empty;


      Chris.
      -------------
      C.R. Timmons Consulting, Inc.

      Comment

      • Guest's Avatar

        #4
        Re: why wont this work?

        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.
        [color=blue]
        >-----Original Message-----
        >"Greg Merideth" <gmerideth.remo ve@this.uclabs. com> wrote[/color]
        in[color=blue]
        >news:14c601c3a 88a$4d7fa4f0$a6 01280a@phx.gbl:
        >[color=green]
        >> class test
        >> {
        >> private string _processName;
        >>
        >> public string GetProcessID()
        >> {
        >> return (_processName.L ength>1)?_proce ssName:"No Name";
        >> }
        >> }
        >>
        >> It compiles but on the method call I get
        >> SystemNullRefer enceException.
        >>
        >>[/color]
        >
        >Greg,
        >
        >_processName is null. Try:
        >
        > private string _processName = string.Empty;
        >
        >
        >Chris.
        >-------------
        >C.R. Timmons Consulting, Inc.
        >http://www.crtimmonsinc.com/
        >.
        >[/color]

        Comment

        Working...