Very confused with CellSet

  • Thread starter Thread starter jhcorey
  • Start date Start date
J

jhcorey

I have the code below that works fine. Note that CellSet does not have
a constructor, so I don't have a default way of initializing it.
If I try to do something else in the catch block, either
ExceptionManager.Publish(ex);
or simply commenting out the line, I get a compile error:
"Use of unassigned local variable myCellSet".

Can anybody explain what is going on?
TIA,
Jim

....
using Microsoft.AnalysisServices.AdomdClient;
....

public CellSet GetCellSet(string MDXQuery)
{
CellSet myCellSet;
try
{
AdomdConnection myConnection = new
AdomdConnection(GetConnectionString());
myConnection.Open();
AdomdCommand myCommand = new AdomdCommand(MDXQuery, myConnection);
myCellSet = myCommand.ExecuteCellSet();
myConnection.Close();
}
catch (Exception ex)
{
throw(ex);

}

return (myCellSet);

}
 
I have the code below that works fine. Note that CellSet does not have
a constructor, so I don't have a default way of initializing it.
If I try to do something else in the catch block, either
ExceptionManager.Publish(ex);
or simply commenting out the line, I get a compile error:
"Use of unassigned local variable myCellSet".

Can anybody explain what is going on?

With the changes you say, the compiler is worried about you returning
null when that path is followed through the code. It worries about
things like this because unexpected null returns from functions that
are supposed to produce objects often catch out the people and code who
call such functions. If you really want that after an exception is
thrown and published, you return null, you can tell the compiler that
you mean this by either:

- putting myCellSet = null; at the end of the catch block, or
- changing the declaration line to CellSet myCellSet = null;

Either way the compiler will be satisfied that the code makes it clear
what the return value will be, and it should be quiet.
 
Very good -- thanks. I simply initialized the CellSet to null at the
beginning, and that compiles file.
 
Back
Top