Forgive me if this is already posted in a different way.  Basically there is
an issue with variable scope when using anonymous delegates.  Here is an
example (compile error in last method)...

private delegate void BorrowCommandDelegate(MySqlCommand command);
private delegate void BorrowReaderDelegate(MySqlDataReader reader);

private void BorrowCommand(BorrowCommandDelegate borrower) {
                
        MySqlConnection connection = null;
        MySqlCommand command = null;
                
        try {
                connection = new MySqlConnection(MyLib.ConnectionString);
                connection.Open();
                command = connection.CreateCommand();
                borrower(command);
        }
        catch (Exception ex) {
                MyLib.TraceIf(command != null, TraceLevel.Info, "Sql: " +
command.CommandText);
                MyLib.Trace(TraceLevel.Error, ex.ToString());
                throw new CustomException(ex.ToString());
        }
        finally {
                if (connection != null && connection.State == 
ConnectionState.Open) {
                     connection.Close();
                }
                connection = null;
        }
}

private void BorrowReader(string sql, BorrowReaderDelegate borrower) {
                        
     MySqlDataReader reader = null;
                        
     try {
                                
          this.BorrowCommand(delegate(MySqlCommand command) {           
                                command.CommandText = sql;
                                reader = command.ExecuteReader();
                                borrower(reader);
          });
     }
     finally {
          if (reader != null) {
                reader.Close();
                reader = null;
          }
     }
}

public List<BusinessObject> GetObjects() {

     List<BusinessObject> businessObjects = new List<BusinessObject>();
     string sql = "SELECT * FROM SomeTable";

     this.BorrowReader(sql, delegate(MySqlDataReader reader) {
                                
          while (reader.Read()) {
               BusinessObject businessObject = new BusinessObject() {
                    // Set Properties
               };
               businessObjects.Add(businessObject);   // COMPILE ERROR,
UNKNOWN SYMBOL businessObjects
          }
     });

     return businessObjects;
}
-- 
View this message in context: 
http://mono.1490590.n4.nabble.com/Variable-Scope-Problem-tp2280481p2280481.html
Sent from the Mono - General mailing list archive at Nabble.com.
_______________________________________________
Mono-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list

Reply via email to