Zaccheus
January 22nd, 2007, 04:20 PM
Is there any way to receive a handle created by another member function and still use the C++/CLI stack semantics (i.e. automatic calling of Dispose) ?
I have a simple member function:
delegate void DataRecipient
(
SqlDataReader^ inSqlDataReader
);
//...
void Database::ExecuteQuery
(
String^ inSqlQuery,
DataRecipient^ inDataRecipient
)
{
// Create connection:
SqlConnection connection(ConnectionString);
SqlCommand command(inSqlQuery, %connection);
connection.Open();
// Execute query:
SqlDataReader^ reader;
try
{
reader = command.ExecuteReader();
// Give the query results to the data recipient.
inDataRecipient(reader);
}
finally
{
delete reader;
}
}
This compiles & works fine.
However, I would like use the stack semantics syntax with the SqlDataReader :
delegate void DataRecipient
(
SqlDataReader^ inSqlDataReader
);
//...
void Database::ExecuteQuery
(
String^ inSqlQuery,
DataRecipient^ inDataRecipient
)
{
// Create connection:
SqlConnection connection(ConnectionString);
SqlCommand command(inSqlQuery, %connection);
connection.Open();
// Execute query:
SqlDataReader reader( command.ExecuteReader() );
// Give the query results to the data recipient (a delegate).
inDataRecipient(%reader);
}
At the line where 'reader' is created, the compiler complains:
error C3673: 'System::Data::SqlClient::SqlDataReader' : class does not have a copy-constructor
Is it not possible to use that syntax in this way?
-------------------------------------------------------------
EDIT:
Yes! http://www.codeguru.com/forum/showthread.php?t=412448#post1582471
I have a simple member function:
delegate void DataRecipient
(
SqlDataReader^ inSqlDataReader
);
//...
void Database::ExecuteQuery
(
String^ inSqlQuery,
DataRecipient^ inDataRecipient
)
{
// Create connection:
SqlConnection connection(ConnectionString);
SqlCommand command(inSqlQuery, %connection);
connection.Open();
// Execute query:
SqlDataReader^ reader;
try
{
reader = command.ExecuteReader();
// Give the query results to the data recipient.
inDataRecipient(reader);
}
finally
{
delete reader;
}
}
This compiles & works fine.
However, I would like use the stack semantics syntax with the SqlDataReader :
delegate void DataRecipient
(
SqlDataReader^ inSqlDataReader
);
//...
void Database::ExecuteQuery
(
String^ inSqlQuery,
DataRecipient^ inDataRecipient
)
{
// Create connection:
SqlConnection connection(ConnectionString);
SqlCommand command(inSqlQuery, %connection);
connection.Open();
// Execute query:
SqlDataReader reader( command.ExecuteReader() );
// Give the query results to the data recipient (a delegate).
inDataRecipient(%reader);
}
At the line where 'reader' is created, the compiler complains:
error C3673: 'System::Data::SqlClient::SqlDataReader' : class does not have a copy-constructor
Is it not possible to use that syntax in this way?
-------------------------------------------------------------
EDIT:
Yes! http://www.codeguru.com/forum/showthread.php?t=412448#post1582471