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

Code contracts suggestions for DataGridView #386

@Patashu

Description

@Patashu

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions