My random thoughts on software development

Category: Visual Basic .NET (page 2 of 3)

Visual Studio 2017 Launch Event
Join us!

Microsoft will release Visual Studio 2017 on March 7.  They organize a two-day virtual event to show the world the beauty of this latest release.

On March 7, you can watch a live stream with Julia Liuson, Brian Harry, Miguel de Icaza, and Scott Hanselman as they share the newest innovations in Visual Studio, .NET, Xamarin, Azure, and more. After the keynote, Microsoft engineers will lead interactive technical demo sessions to help you get the most out of Visual Studio 2017 and the rest of their tools and platform. On March 8, you can get productive even faster by joining a full day of live interactive trainings. Make sure you will sign up for these trainings.

But the event is not only virtual. You can join a couple of locally hosted events. Speaking about the Netherlands you have the choice of a couple events, geographically spread around the country. I’m not sure this list is comprehensive, but you can choose already from a nice list!

Join us on one of this meetings… I’m looking forward to meet you!

VB.NET to C# – 03
C#-Visual Basic Bilingual Dictionary

In this post I want to point you to a book which will help you to make switching between C# and VB.NET and vice versa more easily. The title of the book is actually quite self describing: ‘C#-Visual Basic Bilingual Dictionary’ and is written by Tim Patrick.


From the back: “Built on Microsoft’s powerful .NET Framework, C# and Visual Basic are complete equals in terms of coding power and application development possibilities. In today’s multi-platform environment, an understanding of both languages is a job requirement. The C#-Visual Basic Bilingual Dictionary unifies the languages by providing clear, functional equivalents for all syntax and grammar differences.

  • Complete coverage of all language keywords. Nearly 900 dictionary-like entries cover every Visual Basic and C# keyword and grammar feature, including VB’s “My” namespace.
  • Examples in both languages. Hundreds of code samples in both C# and Visual Basic make translations between the languages clear and easy to understand.
  • Full support for Roslyn. Each chapter covers the latest language features from Visual Studio 2015 and Microsoft’s “Roslyn” compiler.

Whether you work on a team that uses both languages, or just need to understand a technical article written in that “other” language, the C#-Visual Basic Bilingual Dictionary is an essential resource for developers crafting Microsoft software solutions.”

You can browse trough the content on the Amazon website. I’ve just ordered one 😉

VB.NET to C# – 02
Working with data types

The Common Type System (CTS) ensures that components written in C# or VB.NET can interact with each other. The CTS supports two general categories of types: Value types and Reference types. Value types directly contain data. An instance of a value type is either allocated on the stack or allocated inline in a structure. These types can be natively built-in (native data types), user-defined or enumerations. A reference type, in contrast to a value type, stores a reference to the value’s memory address and is allocated on the heap. A reference type can be a self-describing type, a pointer type or an interface type. As said, the native .NET types are in general fully compatible, but naming and assigning is different between VB.NET and C#.
Continue reading

VB.NET to C# – 01
Structure of your classes

I love Visual Basic.NET, but as a .NET developer I write code in C# too. In this serie of posts I’ll show you the main differences between VB.NET and C#. Today I will discuss the structure of classes.

Continue reading

Use LINQ to compare two sequences in Visual Basic .NET

One of the smartest ways to verify if two sequences of the type List(Of T) are equal, is to use LINQ. Or more specific, the extension method SequenceEqual(). It will compare both list in exact order and it returns a Boolean value, indicating if both lists are exactly the same.

Continue reading

Year first date literals in Visual Basic .NET

Since I write on a daily base unit tests, I’m wondering more and more why there is not an easy – and especially unambiguous – way to provide a hardcoded date to a variable or procedure argument. Do you know, without any doubt, what this date actually means?

Dim thisYearsBirthday As Date = #5/6/2015#

Was I born on the 5th of June or the 6th of May? Visual Basic .NET will support now a good ISO-standard, which starts with the year. It follows the convention of ‘yyyy-MM-dd’ and you can even use slashes as separators. You can also provide the time if you want.

Dim todayDateWithDash = #2015-02-13#
Dim todayDateWithSlash = #2015/2/13#
Dim nowDateTime24 = #2015-02-13 19:59:00#
Dim nowDateTime12 = #2015-02-13 7:59:00 PM#

Console.WriteLine(nowDateTime24.ToString("yyyy-MM-dd HH:mm:ss"))
Console.WriteLine(nowDateTime12.ToString("yyyy-MM-dd hh:mm:ss"))

The results are shown in the screenshot below. Please note that you can specify the time part as 24-hour format as well as the 12-hour format. The ToLongDateString() extension methods formats the dates to the Dutch language, but I’m sure that you will understand the concept.


BTW… for the people who are still doubting about my birthdate… I was born on a 6th of May!

Show generated SQL of Entity Framework in Visual Basic .NET

When you are writing LINQ queries against an Entity Framework context, you don’t have to worry about the SQL which is generated and executed. However, in some cases you may be interested in this SQL. It’s quite easy to switch on a logging mechanism which will be display all executed SQL in your Debug window. You have to provide an Action to the DbContext.Database.Log property. You provide a lambda with the functionality you want. If you want you can log this output also to a file or for example the Clipboard.

Continue reading

Query a CSV file with LINQ and Visual Basic .NET

Once in a while I’m still surprised by the functionality of LINQ. I saw a question on the Internet from a developer who asked if it is possible to query against Comma Separated Values (CSV) in a file. Based on one of the answers I wrote this post, because it’s really great what you can do with so less source code. As you probably already knew, I’m a big fan of LINQPad, which I will use to demo my examples. One of the nice features is the extension method .Dump(). As you can imagine, this will dump (or write) the data to the Results window of LINQPad. It works only in LINQPad, so, if you want to try this source in Visual Studio, you have to remove the .Dump() statements and write your own code to display the results.

Dim path = GetAndCreateFilePath()
Dim results = (From line In File.ReadAllLines(path).Dump("Lines read")
               Let value = line.Split(",").
                                Skip(1). ' Skip the RowNumber
                                Select(Function(x) Integer.Parse(x))
               Select New With {.MinimumValue = value.Min(),
                                .MaximumValue = value.Max(),
                                .Average = value.Average(),
                                .RowTotal = value.Sum()}).ToList().

As you can see in the VB.NET code above, first we create a file, then we read and query this file and in the last statement we delete this temporary file. Before diving more deep in this code, you have to see what we are writing to disk in the GetAndCreateFilePath() procedure. The lines starts with a row number and four numeric values.

Private Function GetAndCreateFilePath() As String
    Dim filePath = Path.GetTempFileName()
    Dim stringBuilder As New StringBuilder
    With stringBuilder
        .AppendLine("1, 309, 88, 125, 825")
        .AppendLine("2, 458, 258, 198, 189")
        .AppendLine("3, 87, 587, 675, 136")     
    End With
    File.WriteAllText(filePath, stringBuilder.ToString())
    Return filePath
End Function

In the first code snippet we querying a LINQ statement against a List(Of String), the line variable. With help of the Split() function, we divide this line into an array of String, with in each element a value from the line. Because we are not interested in the first value, the Row Number, we skip this value with Skip(1). As a side note it’s good to mention that starting from Visual Basic.NET 2015 you can use inline comments. In this example it really helps to explain why we are skipping the first element of the array. If we have selected the correct range of data, we can easily project this data, and calculations on it to an anonymous type. And as earlier said, the Dump() method writes the data to the Results window of LINQPad. You can use this method more than once in one LINQ statement. First we are writing the actual lines read and at the end the statistics of the data. The power of LINQ is really amazing!


Loop in batches trough an IEnumerable in Visual Basic .NET

Sometimes you need to handle data in batches. With help of this extension method, all collections derived from an IEnumerable are easy to populate in batches. Note that this code needs to be placed in a static class, so in terms of VB.NET in a module.

Continue reading

Left Outer Join in LINQ with Visual Basic .NET

Many developers encounters problems when trying to mimic the functionality of a Left Outer Join in LINQ. To be honest, the needed code is on first sight a bit strange. You can use a Group Join … Into .. and the DefaultIsEmpty() method. Let’s start with a regular (Inner) Join example. It uses the NorthWnd database and will retrieve all customers with at least one order.

Continue reading

Older posts Newer posts

© 2019 Obelink.ToString()

Theme by Anders NorenUp ↑