1

I am getting three "Suggestions" IDE0017 Object Initialization cab be simplified.

    private string dbSelect()
    {
        // This is the User File Name Selection
        OpenFileDialog openThis = new OpenFileDialog();
        openThis.DefaultExt = "sqlite";
        openThis.Filter = "SQLite Databases|*.sqlite";
        if (openThis.ShowDialog() == DialogResult.OK)
        {
            return openThis.FileName;
        }
        return null;
    }


    public bool openDatabase()
    {
        OpenFileDialog openThis = new OpenFileDialog();
        openThis.DefaultExt = "sqlite";
        openThis.Filter = "SQLite Databases|*.sqlite";
        if (openThis.ShowDialog() == DialogResult.OK)
        {
            m_dbConnection = new SQLiteConnection("Data Source=" + openThis.FileName + ";Version=3;");
            m_dbConnection.Open();
            return true;
        }
        return false;
    }


   public bool createDatabase()
    {
        SaveFileDialog createThis = new SaveFileDialog();
        createThis.DefaultExt = "sqlite";
        createThis.Filter = "SQLite Databases|*.sqlite";

        if (createThis.ShowDialog() != DialogResult.OK || createThis.FileName.Trim() == "")
        {
            return false;
        }

        m_dbConnection = new SQLiteConnection("Data Source=" + createThis.FileName + ";Version=3;");
        m_dbConnection.Open();

        return true;
  }

How do I reform these to simplify them? They've been fine until VS2017...

And, so far, Google hasn't really helped.

And, I am not sure I like the idea that simplification means placing everything in a single line.

I was taught that readability is important and all in one line is just plain cluttered.

But, I'd hate to miss a trick... I suppose I can just turn it off...

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Mike Sr
  • 511
  • 1
  • 5
  • 15
  • 1
    If you mouse-over the line you should get a wee lightbulb icon, click its dropdown & you can preview the change & have VS make it for you. – Alex K. Oct 04 '18 at 12:37
  • 1
    This is NOT an error, just a suggetion made by Visual Studio to make your code easier to understand. – Poul Bak Oct 04 '18 at 12:43
  • Where do you get these suggestions, on which lines? – Tim Schmelter Oct 04 '18 at 12:47
  • OpenFileDialog openThis = new OpenFileDialog() { //data initializer } – A_Sk Oct 04 '18 at 12:48
  • Alex K - Thanks learned something! Poul Bak - I knew that, it's in the first line. :) Tim Schmeiter - on the actual dialog definition lines – Mike Sr Oct 04 '18 at 15:22

1 Answers1

2

Uppon creating a new instance of anything instead of doing MyType myVariable = new MyType(); and then set every property from myVariable line by line you can do MyType myVariable = new MyType() {}; and between the {} you can set the properties you want to set.

For the SaveFileDialog for example you can do :

SaveFileDialog createThis = new SaveFileDialog() 
{
    DefaultExt = "sqlite", 
    Filter = "SQLite Databases|*.sqlite"
};
nalka
  • 1,894
  • 11
  • 26