Monthly Archives: September 2010

.NET Auto-implemented properties

Also known as automatic properties.

From the start we have classic properties:

VB.NET

Private _Name As String
Public Property Name As String
    Get
        Return _Name
    End Get
    Set(ByVal value As String)
        _Name = value
    End Set
End Property

A shortcut (snippet) for this in Visual Studio is type property then hit tab

C#

private String _Name;
public String Name
{
    get
    {
        return _Name;
    }
    set
    {
        _Name = value;
    }
}

A shortcut (snippet) for this in Visual Studio is type propfull then hit tab

Then C# 3 (2008) came out with auto-implemented properties:

public String Name { get; set; }

A shortcut (snippet) for this in Visual Studio is type prop then hit tab

Later VB.NET 10 (2010) caught up:

Public Property Name As String

Though I don’t know of a built in snippet, it is easy enough to create your own or use property and remove the extra code.

Of course these shortcuts might change, but work for me 🙂

Advertisements

LINQ Basic DataSource binding

This is a quick example of LINQ and binding to a DataGridView control (via DataSource) the property name is used as the column header (Column[idx].HeaderText) this can be change via the following attribute:
System.ComponentModel.DisplayName(“Column header name here”)

The following example uses a LINQ query on a string collection to demonstrate this:

C#

var objCollection = from objs in fileStringCollection
                    select new WrapperForStrings
                    {StringProperty = objs};
this.DataGridView2.DataSource = objCollection.ToList();
this.DataGridView2.AutoResizeColumns();
...
public class WrapperForStrings
{
    private String stringValue;

    [System.ComponentModel.DisplayName("Column header name here C#")]
    public String StringProperty
    {
        get
        {
            return stringValue;
        }
        set
        {
            stringValue = value;
        }
    }
}

VB.NET

Dim objCollection = From objs In fileStringCollection _
                    Select New WrapperForStrings _
                    With {.StringProperty = objs}
Me.DataGridView2.DataSource = objCollection.ToList()
Me.DataGridView2.AutoResizeColumns()
...
Public Class WrapperForStrings
    Private stringValue As String

    <System.ComponentModel.DisplayName("Column header name here VB")> _
    Public Property StringProperty() As String
        Get
            Return stringValue
        End Get
        Set(ByVal value As String)
            stringValue = value
        End Set
    End Property
End Class

LINQ Distinct / Group By

LINQ offers up a nice way to group items and you can even reference the grouping values called keys.

Here’s an example of grouping a collection of Strings based on their length:

VB.NET

' VB requires the = Group part, but Group str By doesn't need the str part.
Dim myList = From str In fileStringCollection _
             Order By str.Length _
             Group By str.Length Into filGrouping = Group

C#

// Of course C# can't have the = Group part, requires the group str by to have the str part and requires the select
var myList = from str in fileStringCollection
             orderby str.Length
             group str by str.Length into filGrouping
             select new { filGrouping = filGrouping, Length = filGrouping.Key };

Here filGrouping behaves like a variable within the scope of the expression, we can look at the distinct values for the Length of the Strings by using the filGrouping.Key. Here we are returning the grouping, if you enumerate the grouping you get each of the Strings for that String Length.

For more on DISTINCT / GROUP BY see my more recent post (from a database point of view) SQL Server / Oracle: DISTINCT or GROUP BY

LINQ Basic Selects

Whether a select is required or not

VB.NET

' Notice no select is required
Dim fileStringCollection = From anythingGoesHere _
                           In My.Computer.FileSystem.GetFiles("C:\")

C#

// Select is required
var fileStringCollection = from anythingGoesHere
                           in Directory.GetFiles(@"C:\")
                           select anythingGoesHere;

Selecting into an object / properties

VB.NET

Dim objCollection = From objs _
                    In fileStringCollection _
                    Select New WrapperForStrings _
                      With {.StringProperty = objs}

C#

var objCollection = from objs
                    in fileStringCollection
                    select new WrapperForStrings
                      {StringProperty = objs};

Returning multiple columns (via an instance of an anonymous class) in a select

VB.NET

' VB can just use a plain syntax as follows
Dim myInfoCollection = From file In filesInfoCollection _
                       Select file.Name, file.CreationTime

C#

// In C# you must explicitly create a new (anonymous) object and explicitly define the properties and values
var myInfoCollection = from file in filesInfoCollection
                       select new { CSName = file.Name,
                         CSCreationTime = file.CreationTime };

Edit: For an example of WrapperForStrings see this post