out parameter question

  • Thread starter Thread starter web1110
  • Start date Start date
W

web1110

This code

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

try
{
strArray=theArray;
}
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?
 
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.
 
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.
 
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.
 
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.
 
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.
 
Back
Top