Object initialization can be simplified

Asked

Viewed 505 times

0

After having made some updates to the nuget(18) packages, I went to compile my project and this error occurs:

Object initialization can be simplified

It points to those files in my code.

GridTextColumn dataLib = new GridTextColumn();

Here the complete class

public class Control
    {
        SfDataGrid dataGrid = new SfDataGrid();
        DataService dataService = new DataService();
        public async void CriaDataGrid()
        {
            dataGrid.AutoGenerateColumns = false;

            GridTextColumn dataLib = new GridTextColumn();
            dataLib.MappingName = "DataLib";
            dataLib.HeaderText = "Data";
            dataLib.Width = 80;
            dataLib.TextAlignment = TextAlignment.Start;
            dataLib.CellTextSize = 9;

            GridTextColumn cliente = new GridTextColumn();
            cliente.MappingName = "Cliente";
            cliente.HeaderText = "Cliente";
            cliente.Width = 180;
            cliente.TextAlignment = TextAlignment.Start;
            cliente.CellTextSize = 9;

            GridTextColumn vendedor = new GridTextColumn();
            vendedor.MappingName = "Vendedor";
            vendedor.HeaderText = "Vendedor";
            vendedor.Width = 180;
            vendedor.TextAlignment = TextAlignment.Start;
            vendedor.CellTextSize = 9;

            GridTextColumn filial = new GridTextColumn();
            filial.MappingName = "Filial";
            filial.HeaderText = "Filial";
            filial.Width = 100;
            filial.TextAlignment = TextAlignment.Start;
            filial.CellTextSize = 9;

            dataGrid.Columns.Add(dataLib);
            dataGrid.Columns.Add(cliente);
            dataGrid.Columns.Add(vendedor);
            dataGrid.Columns.Add(filial);

            //dataGrid.AllowResizingColumn = true;
            dataGrid.ItemsSource = await dataService.GetLiberaAsync();
            dataGrid.SelectionMode = SelectionMode.Single;
            dataGrid.SelectionChanged += DataGrid_SelectionChanged;
        }
        void DataGrid_SelectionChanged(object sender, GridSelectionChangedEventArgs e)
        {
            //DisplayAlert("Alert", "You have been alerted", "OK");
        }
    }

EDIT1

Version of VS2017 inserir a descrição da imagem aqui

  • This is a build error or a Warning?

1 answer

2


That’s not a mistake, it’s a Warning that says boot can be done in a more readable and organized way.

The hint is that you initialize the objects like this:

GridTextColumn dataLib = new GridTextColumn
{
    MappingName = "DataLib",
    HeaderText = "Data",
    Width = 80,
    TextAlignment = TextAlignment.Start,
    CellTextSize = 9
};

Besides, you’ll probably get one Warning asking to use var on the left side of the assignment, since the type can be inferred.

  • is a Warning, but does not compile or deploy, so I opened the post.

  • I rebooted the VS and that mistake disappeared without me doing anything. Anyway, I will close the post with the answer of LINQ not to be open, no conclusion, because his approach to the title of the post is correct.

  • @pnet I’ve been scouring the Github and it seems that really Roslyn gave a complicated that shouldn’t with some warnings. You could confirm the full version of your Visual Studio?

  • I edited the post and put the version there

Browser other questions tagged

You are not signed in. Login or sign up in order to post.