locked
use of out keyword RRS feed

  • Question

  • In the code below, oData is an out parameter to ReadData function. oData is assigned to in this function. Still I get following error - "The out parameter oData must be assigned to before controlleaves the current method."

    public void ReadData(out CPushVariableData[] oData)

    {

    try

    {

    CPushVariableData[] oDataLocal;

    oResponseManager.ReadData(out oDataLocal);

    int iNofDataObjets = oDataLocal.GetLength(0);

    oData = new CPushVariableData[iNofDataObjets];

    for (int i = 0; i < iNofDataObjets; i++)

    {

    oDataIdea = oDataLocalIdea;

    }

    }

    catch (Exception exc)

    {

    }

    }

     

    Also, when I remove  the try catch block I don't get the error.

    Tuesday, July 17, 2007 7:23 AM

Answers

  • It happened because of the flow of try catch block.
    When the code got an error it will jump to catch block.

    And oData is still unassign which not meet criteria of "out" parameter.

    you can put assignment statement for oData in catch block to solve this issue.

    Tuesday, July 17, 2007 7:47 AM
  • of course, It's compile-time error.

    compiler can check the flow easily just like u wrote something like this.

     

    int compileError()

    {

       try {

          return 0;

       }

       catch(Exception) {

       }

    }

     

    even nothing error in code above but it can't compile b'coz "not all code path return value"
    the same thing happened to out parameter.

    Tuesday, July 17, 2007 9:28 AM

All replies

  • It happened because of the flow of try catch block.
    When the code got an error it will jump to catch block.

    And oData is still unassign which not meet criteria of "out" parameter.

    you can put assignment statement for oData in catch block to solve this issue.

    Tuesday, July 17, 2007 7:47 AM
  • I am getting a compiler error and not a runtime error.
    Tuesday, July 17, 2007 9:22 AM
  • of course, It's compile-time error.

    compiler can check the flow easily just like u wrote something like this.

     

    int compileError()

    {

       try {

          return 0;

       }

       catch(Exception) {

       }

    }

     

    even nothing error in code above but it can't compile b'coz "not all code path return value"
    the same thing happened to out parameter.

    Tuesday, July 17, 2007 9:28 AM
  • How can I put the assignment statement in the catch block?If I put the assignment statement in the catch block will it ever get assigned at all?

     

    How about putting the asignment statement in the finally block?

    Sunday, July 22, 2007 6:15 AM
  • Try something like this:

     

    Code Snippet

    public void ReadData(out CPushVariableData[] oData)

    {

        oData = null;

        try

        {

            CPushVariableData[] oDataLocal;

            oResponseManager.ReadData(out oDataLocal);

            int iNofDataObjets = oDataLocal.GetLength(0);

            oData = new CPushVariableData[iNofDataObjets];

            for (int i = 0; i < iNofDataObjets; i++)

            {

                oData[i] = oDataLocal[i];

            }

        }

        catch (Exception exc)

        {

            if (oData == null)

                oData = new CPushVariableData[0];

        }

    }

     

    Strictly speaking, you don't need the lines in the catch block there; the oData = null at the beginning of the method is what gets rid of the error.  It's just usually nicer to return empty arrays instead of null.

    Monday, July 23, 2007 2:54 PM
  • Of course, you can.

     

    Consider this.

     

    if (condition) {
          return something;
      } else {

        return anotherthing;

    }

    we may didn't know if the code path ever return but it's ok because when condition was false the program know

    the it should return anotherthing;

     

    same as the catch block

     

    try {

         ...

    } catch (Exception) {

        // The code may or may not run through this block

        // but If it happen it will have something to return

    }

    Tuesday, July 24, 2007 1:58 AM