out parameter question

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

    out parameter question

    This code

    public void ReturnArray(out string[] strArray)
    {
    string[] theArray=new string[]{"aaa", "bbb", "ccc"};

    try
    {
    strArray=theArr ay;
    }
    catch
    {
    }
    }

    produces the syntax error.

    The out parameter 'strArray' must be assigned to before control leaves
    the current method

    Why does this happen? Because of the possibility the assignment can fail in
    the try block?


  • DBC User

    #2
    Re: out parameter question

    try this.

    before try catch set strArray as null

    strArray=null

    this way, regardless of what happen in the try catch , you have a value
    set for the array.

    Comment

    • DBC User

      #3
      Re: out parameter question

      try this.

      before try catch set strArray as null

      strArray=null

      this way, regardless of what happen in the try catch , you have a value
      set for the array.

      Comment

      • DBC User

        #4
        Re: out parameter question

        try this.

        before try catch set strArray as null

        strArray=null

        this way, regardless of what happen in the try catch , you have a value
        set for the array.

        Comment

        • DBC User

          #5
          Re: out parameter question

          try this.

          before try catch set strArray as null

          strArray=null

          this way, regardless of what happen in the try catch , you have a value
          set for the array.

          Comment

          • Brendan Grant

            #6
            out parameter question

            You are correct, because of the assignment occurring in a
            try/catch block, there is no guarantee that the assignment
            will occur.

            You'd be better off running checks against theArray to
            make sure it is not null and is what you want prior to
            assigning it to strArray outside of such a block.
            [color=blue]
            >-----Original Message-----
            >This code
            >
            > public void ReturnArray(out string[] strArray)
            > {
            > string[] theArray=new string[][/color]
            {"aaa", "bbb", "ccc"};[color=blue]
            >
            > try
            > {
            > strArray=theArr ay;
            > }
            > catch
            > {
            > }
            > }
            >
            > produces the syntax error.
            >
            > The out parameter 'strArray' must be assigned to[/color]
            before control leaves[color=blue]
            >the current method
            >
            >Why does this happen? Because of the possibility the[/color]
            assignment can fail in[color=blue]
            >the try block?
            >
            >
            >.
            >[/color]

            Comment

            Working...