How to fix IDE0017 when using File Dialogs

864 Views Asked by At

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...

1

There are 1 best solutions below

0
On BEST ANSWER

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"
};