in

vbCity Blogs

New (temp) place for vbCity Blogs

Mike McIntyre's .NET Journal

February 2009 - Posts

  • Programming Entity Framework by VB MVP Julie Lerman

    Programming Entity Framework

    Julie Lerman is very excited to share with fellow MVPs that her book, Programming Entity Framework, has finally hit the shelves.

    The book spans a lot of topics from baby steps of creating a model, to querying to architecting n-tier apps with lots of plumbing on the way. All code samples are in VB and C#.

    "Programming Entity Framework is a thorough introduction to Microsoft's new core framework for modeling and interacting with data in .NET applications. This book not only gives experienced developers a hands-on tour of the Entity Framework and explains its use in a variety of applications, it also provides a deep understanding of its architecture and APIs. From the Entity Data Model (EDM) and Object Services to EntityClient and the Metadata Workspace, Programming Entity Framework covers it all."

  • Visual Basic 2005/2008 - Get String Array from Lines in a Multiline TextBox

     A post on vbCity asking how to get the lines in a VB6 multiline TextBox reminded me how much I like using VB.NET.

    In VB.NET 2005 its this easy:

    Dim multilineStrings() As String = Me.MyMultilineTextBox.Lines

    In VB.NET 2008, you can use LINQ too:

     

  • Visual Basic 2008 - Use Lambdas with Generic Lists

    Lambdas make it easy to query Generic Lists.  Here's a simple example that uses the Lambda Where() function to query a List(Of Integer):

    Public Class GetDotNetCodeSamples

     

        Private integersList As New List(Of Integer)

     

        Public Sub New()

            Dim numbers() As Integer = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}

            integersList.AddRange(numbers)

        End Sub

     

        Public Function Example01() As String

            Dim integersQuery = integersList.Where(Function(n) n < 5)

            For Each x As Integer In integerQuery

                result &= x & Environment.NewLine

            Next

            Return result

        End Function

     

    End Class

     

  • Visual Studio 2010 - Dynamic Programing in Visual Basic: IronPython

    The new dynamic feature in Visual Basic 2010 let's you mix late-bound code in with statically typed code ie. you combine early and late binding in the same code.

    The dynamic feature uses the Dynamic Language Runtime (DLR) for caching and dynamic dispatch which enables interop with dynamic objects.

    The example below demonstrates how you would use an IronPython runtime to interop with with IronPython objects.

    Note the code example below will not work with the October 2008 CTP as the dynamic feature was not yet functional in VB at that time.

    IronPython in Visual Basic 2010

  • Visual Studio 2010 - Visual Basic Array Literals

    Here's another way Visual Basic 2010 makes it easier to program: Array Literals 

     Visual Basic 2010 Array Literals

    The Visual Basic compiler is smart enough to recognize when an array literal is composed of elements of the same type e.g. it 'infers' an array of integer in the first line of code above.

     

  • Visual Studio 2010 - Visual Basic Auto Properties

    Auto-implemented properties, new in Visual Basic 2010, provide a one-line way to declare a simple property with a backing field and a getter and a setter:

    Public Class Customer

        Property CustomerID As Integer

        Property CustomerName As String

    End Class

     

    The compiler generates a backing field with the same name as the property, but with a preceding underscore.

     

    Notice in the example below that the Customer class'  CustomerID and CustomerName properties have been auto generated and are available via intellisense:

     

    Initializers can be used to give an auto-implemented property a default value (which gets set in the class’ constructor):
       

        Property ID() As Integer = -1
        Property SupplierList() As New List(Of Supplier)
        Property OrderList() As New List(Of Order) With {.Capacity = 100}
       
        <DefaultValue("-")>
        Property Name() As String Implements ICustomer.Name

    Note: Auto-implemented properties cannot have parameters, nor can they be declared ReadOnly or WriteOnly.

     

     

     

     

  • Visual Studio 2010 Web Development - ClientIDMode and ClientID DropDownList Example

    Before Visual Studio 2010, I've not liked fiddling with client side IDs in ASP.NET, especially when web page element is nested in another element. For example, this markup that is part of a user web control sets ClientID to 'CustomerChoice'.

    <asp:DropDownList ID="ddlCustomers" ClientID="CustomerChoice" runat="server">
        <asp:ListItem>Jason</asp:ListItem>
        <asp:ListItem>Jack</asp:ListItem>
    </asp:DropDownList>

    If the user web control is hosted on an .aspx page, id becomes 'wucCustomers1_ddlCustomers' when rendered in a browser. The user control's ID is added as a suffix to the DropDownList's ID to become the ID.

     <select name="wucCustomers1$ddlCustomers" id="wucCustomers1_ddlCustomers">

                   <option value="Jason">Jason</option>

                   <option value="Jack">Jack</option>

    </select>

    If a JavaScript script is created that uses that id and then the control is moved to say - a content area, the id changes and the script breaks.

    Visual Studio 2010 introduces the ClientIDMode attribute to solve that problem.

    <asp:DropDownList ID="ddlCustomers" ClientID="CustomerChoice" ClientIDMode="Static" runat="server">
        <asp:ListItem>Jason</asp:ListItem>
        <asp:ListItem>Jack</asp:ListItem>
    </asp:DropDownList>

    Look at how the client side ID renders in the browser now:

    <select name="wucCustomers1$ddlCustomers" id="CustomerChoice">

                   <option value="Jason">Jason</option>

                   <option value="Jack">Jack</option>

    </select>

     

     

     

  • Internet Explorer Developer Toolbar

    Though its been available for several years, not everyone knows about Microsoft's 'Internet Explorer Developer Toolbar' for web developers.

    The toolbar makes it easy to explore a web page's markup, attributes, styles, and more.  You can check the pages you create to debug and tweak them.  You can examine pages from the web to discover new techniques.

     The screen shot below shows a typical use for the toolbar. The toolbar is shown at the bottom of a web page.

    1. Click the 'Select Element by Click' button. This turns the cursor into an arrow that can be used to click any part of the page.

    2. Click on a page element e.g. a drop down.

    3. In the document outline, the markup for element clicked will be shown.  You can drill down into the markup.

    4. Attributes and styles associated with the outline selection are shown.

    IE Devloper Toolbar 

    Download it at -> Internet Explorer Developer Toolbar

    Note:  You may get the erroneous warning below if you try to download the IE Developer Toolbar via Google search.  If you do use Microsoft Live Search instead -> http://www.live.com

     Google IE Toolbar Download Warning

     

     

     

  • Microsoft Transact SQL - CASE Statements in ORDER BY Clauses

    Case statements can be used to return columns of data created from logic.  The example below shows how a CustomerType column can be returned that types customers as either 'International' or 'Domestic' depending on whether the 'Country' column in a customers table contains 'USA' or not.

    But what if the results need to be ordered by a column created by CASE WHEN logic?

    Just use the CASE statement in the ORDER BY clause.

    The query example below shows how that is done and in the results pane, it shows customers ordered by CustomerType.

    CASE Statement in ORDER BY Clause

  • Visual Studio 2010/.NET 4.0 - Snippets in .Aspx Page Markup

     Being a huge Visual Studio snippets fan and doing a lot of web development,  I really like this Visual Studio 2010 feature for web developers:   snippets in markup!

    WIth this feature you can insert built in Asp.Net and JavaScript snippets.  You can create your own libraries of snippets and insert them too.

    Snippets in Markup in VS 2010

  • Visual Studio 2010/.NET 4.0 - Visual Basic <-> C# Language Parity

    In the slide below language features in white text are being added to the language:

    VS2010 Language Parity

     

Copyright 1998-2009 vbCity.com LLC
Powered by Community Server (Non-Commercial Edition), by Telligent Systems