Coding For a Living

rss

A place to share and comment about coding for a living.


Visual Basic 2012– Async Feature Mike McIntyre

The VisualBasic 2012 Async feature makes it simpler to make code asynchronous.

With it you can call into asynchronous methods WITHOUT callbacks and WITHOUT splitting your code into different methods.

The following example shows a Visual Basic async method.

' Add an Imports statement and a reference for System.Net.Http

Imports System.Net.Http
 
Class MainWindow
 
    ' Mark the event handler with async so you can use Await in it. 
    Private Async Sub StartButton_Click(sender As Object, e As RoutedEventArgs)
 
        ' Call and await separately. 
        'Task<int> getLengthTask = AccessTheWebAsync(); 
        '' You can do independent work here. 
        'int contentLength = await getLengthTask; 
 
        Dim contentLength As Integer = Await AccessTheWebAsync()
 
        ResultsTextBox.Text &=
            String.Format(vbCrLf & "Length of the downloaded string: {0}." & vbCrLf, contentLength)
    End Sub 
 
 
    ' Three things to note in the signature: 
    '  - The method has an Async modifier.  
    '  - The return type is Task or Task(Of T). (See "Return Types" section.) 
    '    Here, it is Task(Of Integer) because the return statement returns an integer. 
    '  - The method name ends in "Async."
    Async Function AccessTheWebAsync() As Task(Of Integer)
 
        ' You need to add a reference to System.Net.Http to declare client. 
        Dim client As HttpClient = New HttpClient()
 
        ' GetStringAsync returns a Task(Of String). That means that when you await the 
        ' task you'll get a string (urlContents). 
        Dim getStringTask As Task(Of String) = client.GetStringAsync("http://msdn.microsoft.com")
 
 
        ' You can do work here that doesn't rely on the string from GetStringAsync.
        DoIndependentWork()
 
        ' The Await operator suspends AccessTheWebAsync. 
        '  - AccessTheWebAsync can't continue until getStringTask is complete. 
        '  - Meanwhile, control returns to the caller of AccessTheWebAsync. 
        '  - Control resumes here when getStringTask is complete.  
        '  - The Await operator then retrieves the string result from getStringTask. 
        Dim urlContents As String = Await getStringTask
 
        ' The return statement specifies an integer result. 
        ' Any methods that are awaiting AccessTheWebAsync retrieve the length value. 
        Return urlContents.Length
    End Function 
 
 
    Sub DoIndependentWork()
        ResultsTextBox.Text &= "Working . . . . . . ." & vbCrLf
    End Sub 
End Class 
 
' Sample Output: 
 
' Working . . . . . . . 
 
' Length of the downloaded string: 41763.
 

To learn more click: Asynchronous Programming with Async and Await


vRAD Well Known Names– Part 2 Mike McIntyre

This post explains several ways well known names help my company build applications rapidly.

In my previous vRAD post I introduced ‘PostalCode’, a well known name I have used for application development since 1992.

In our development world, the well known word PostalCode is used where ever a zip code, post code, or postal code is represented or stored, for example:

1. as the column name of all database table columns that store a zip code, post code, or postal code

2. in data models where ever a zip code, post code, or postal code is used

3. as the property name in all classes that require a zip code, post code, or postal code

4. in programmer documentation

5. in code and other generators

PostalCode helps build applications rapidly because it reduces development and development project time.

How? Reuse. Reuse as in where the same thing is used again for the same function – again, and again, and again.  Since 1992:

1. No design time has been spent coming up with what to name things that will represent or store zip codes, post codes, or postal codes.

2. Programmer documentation about PostalCode has not been changed since 1992.

3. Anyone who has worked for me for more than a couple of months can teach a new person to use PostalCode because it is – you guessed it – well known.

4. Code generators, which require postal codes, post codes, or zip codes - for schemas, models, code, and tests – used the well known name PostalCode. We never spend time modifying that part of our code generators.

Can you think of at least three more ways the well known word PostalCode helps us build applications rapidly?

Answers will be published in the next xRad blog post..


Cool Parallax Background for Windows Store App Mike McIntyre

From Wikipedia: “Parallax is a displacement or difference in the apparent position of an object viewed along two different lines of sight, and is measured by the angle or semi-angle of inclination between those two lines.”

A Parallax can create an interesting visual effect when used for a background in a Windows Store App.

David Catuhe explain how in this blog post ‘How to create a cool parallax background’.

image