Many people at SO have indicated that I should use the using statement more often. Therefore, I practice.
The problem is that I cannot decide when to use it and when not to use it. When I think I should use it, then I get errors like in this example (PS. HashPhrase is the class I created):
using (HashPhrase hash = new HashPhrase()) { connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Persist Security Info=False;" + "Jet OLEDB:Database Password=" + hash.ShortHash(pass) + ";"; }
But it gives me an error: 'Password_Manager.HashPhrase': type used in a using statement must be implicitly convertible to 'System.IDisposable'
But in this example, it works fine:
using (OleDbConnection connection = new OleDbConnection()) { connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Persist Security Info=False;" + "Jet OLEDB:Database Password=" + hash.ShortHash(pass) + ";"; using (OleDbCommand command = new OleDbCommand(sql, connection)) { try { connection.Open(); command.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } } }
Are there any quick recommendations on how to determine when a using statement should be used?
source share