Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Code contracts suggestions for DataGridView #386

Open
Patashu opened this issue Feb 17, 2016 · 3 comments
Open

Code contracts suggestions for DataGridView #386

Patashu opened this issue Feb 17, 2016 · 3 comments

Comments

@Patashu
Copy link

Patashu commented Feb 17, 2016

(This isn't an exhaustive list of contracts worth adding, just what I've noticed so far)

  1. CreateColumnsInstance() and CreateRowsInstance() should all ensure non-null result.

That way, Rows and Columns can ensure non-null as well.

The main reason this is a problem is due to Windows Form Designer generated code in InitializeComponent(). It can make lines like these

this.DataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {

which CC static analyzer will think is possibly accessing a null reference.

  1. DataGridViewRowCollection should require for all methods that take in a DataGridViewRow that the row is non-null, and Ensure for all methods that return a DataGridViewRow that the result is non-null

This seems pretty safe to me after going through the code, exception is thrown when null is passed in in relevant places already. But there is one tricky edge case that needs to be cleared up: DataGridViewBand.Clone() needs to guarantee it returns non-null.

public virtual object Clone()
{
    DataGridViewBand dataGridViewBand = (DataGridViewBand)     Activator.CreateInstance(base.GetType());
    if (dataGridViewBand != null)
    {
        this.CloneInternal(dataGridViewBand);
    }
    return dataGridViewBand;
}

It's probably safe, but maybe there's some edge case where Activator.CreateInstance returns null? It calls out to external methods so I can't fully analyze it.

  1. DataGridViewRow CreateCellsInstance() and Cells should Ensure non-null

Similar logic as 1).

  1. DataGridViewColumnCollection should require non-null columns to be added, and ensure columns retrieved are non-null

Similar logic as 2), haven't analyzed yet though.

  1. Control.DataBindings can't be null, ButtonBase.FlatAppearance can't be null, ImageList.Images can't be null

Checked and it's not possible, so just add contracts.

@SergeyTeplyakov
Copy link
Contributor

Great suggestion! Pull request is highly welcome!:)

@Patashu
Copy link
Author

Patashu commented Feb 17, 2016

Ok, I'll try my hand at it later.

@Patashu
Copy link
Author

Patashu commented Feb 22, 2016

#387

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants