1

I really do not understand, and all my studies have only confused me further.

Code snippet:

namespace Family_Finances
{
    class sqlStuff
    {
        private SQLiteConnection m_dbConnection;
        //DialogResult msgBoxResult = DialogResult.Ignore;
        //string myDirectory;
        public int beepFreq = 880;
        public int beepLength = 500;
        private SQLiteConnection dbConn;

        private string dbSelect()
        {

what I am trying to do is create a class, never mind that I am not even referencing it yet, that is reusable and places all my sqlite support code into a class.

I've reverted back to the original code because I've tried all the built-in corrections and the dung heap keeps getting deeper, including changing code in the main Namespace where it's not even referenced.

I want my db_conn available throughout the class so that I don't have to pass it to/from every reference...

Two concepts I know I have trouble with...

"Using" and "IDisposable"

(I didn't know "Using" implied a "Dispose()" and I didn't need a "Close()". Good grief my code worked and I'd never seen an example with "Dispose()" in it. Maybe it would be easier to go back to pastoral studies and learn classical Greek? But, I do love to code...)

So how do I fix this so I can reuse the code in other projects as well as this one...

Please be gentle... I am self taught... And, there are obviously holes (deep wells) in my understanding on the Framework and C#. Thanks!

David
  • 208,112
  • 36
  • 198
  • 279
Mike Sr
  • 511
  • 1
  • 5
  • 15
  • What exactly is the problem you're facing? You have a class called `sqlStuff`, and any given instance of that class has those class-level variables. What isn't working? – David Oct 06 '18 at 12:21
  • David... Beat me to the edit. :) didn't realize the earlier code hadn't been indented properly. Thanks. – Mike Sr Oct 06 '18 at 12:22
  • 1
    David... Getting the CA1001 Warning that the class should implement IDisposable. The code has been used before in VS2010 without problems. But, VS2017 does not like my beginners style. – Mike Sr Oct 06 '18 at 12:23
  • Ah, that makes sense to me now. I guess it's been a while since I've seen that, it's become standard practice to implement `IDisposable` on any class which itself has `IDisposable` members. You'll want your `sqlStuff` class to be `sqlStuff : IDisposable` and then in that implementation it should ensure that the connection objects are properly disposed. There's a duplicate question which illustrates this effectively, I'll mark this as a duplicate... – David Oct 06 '18 at 12:32
  • The `Dispose` method effectively just needs to do two things: (1) Make sure to call `.Dispose()` on any non-null `IDisposable` objects (the answer below can throw a `NullReferenceException` if they're `null`); (2) Only be called once (the answers on the linked duplicate use `bool` flags to ensure that). Then anywhere that you *use* `sqlStuff` should itself be in a `using` block or otherwise ensure that it calls `.Dispose()` on the `sqlStuff` instance. – David Oct 06 '18 at 12:34

1 Answers1

1

Any class that contains a field that implements the IDisposable interface, by microsoft rules, should implement the IDisposable pattern on the containing object as well. Your SQLiteConnection objects implement this pattern.

This isn't a strictly enforced rule unless you plan to use the class as a library or expose it to outside assemblies.

More information can be found here: https://learn.microsoft.com/en-us/visualstudio/code-quality/ca1001-types-that-own-disposable-fields-should-be-disposable?view=vs-2017

Incomplete example:

class sqlStuff : IDisposable
{
    private SQLiteConnection m_dbConnection;
    //DialogResult msgBoxResult = DialogResult.Ignore;
    //string myDirectory;
    public int beepFreq = 880;
    public int beepLength = 500;
    private SQLiteConnection dbConn;

    public void Dispose()
    {
        m_dbConnection?.Dispose();
        dbConn?.Dispose();
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}
Sichi
  • 277
  • 2
  • 10
  • This has a potential for `NullReferenceException`s in the `Dispose()` method. – David Oct 06 '18 at 12:35
  • @David ty, fixed – Sichi Oct 06 '18 at 12:38
  • In https://stackoverflow.com/questions/3607213/what-is-meant-by-managed-vs-unmanaged-resources-in-net It says to use "Using" 'code' using (var connection = new SqlConnection("connection_string_here")) { // Code to use connection here } As this ensures that .Dispose() is called on the connection object, ensuring that any unmanaged resources are cleaned up. But, how can I use "Using" when I want the db_conn available throughout the class and the methods (in the class) available throughout the Namespace? – Mike Sr Oct 06 '18 at 12:56
  • While my question is marked [Duplicate] the referenced question does not specifically address my implementation. IM(B)HO (B) - Beginner... – Mike Sr Oct 06 '18 at 13:01
  • You would only use "Using" in cases where you only temporarily need access to the disposable object. If you want your SQLiteConnection available to your entire namespace, then you wouldnt use "Using". You'd place it in a internal/public static field and implement IDisposable on the containing class. When you're done with the connection/class that holds it, youd call .Dispose() on it. – Sichi Oct 06 '18 at 13:18
  • To be honest I am not sure my methodology is viable. Rethinking. Will pass the filename for the database or make that public instead. And, use "Using" within methods that act on the database. This will have the advantage of not accidentally leaving the database open. Hope this helps someone else. – Mike Sr Oct 06 '18 at 13:24
  • It's entirely viable to have a class dedicated to interfacing with a database, with methods inside of it that retreive information. Your connection could exist as private within it, and be used in your methods. This class would need to implement IDisposable, and when done with it you would call .Dispose(), Whether that's after you're finished getting the information you need, or when the application closes is up to your specific case. – Sichi Oct 06 '18 at 13:28