May 2006 - Posts

You might have noticed in my previous post that the Linq query started with From instead of Select. While I did prefer the syntax that started with Select, because of its similarities with SQL, this change makes a lot of sense. If we want a good IntelliSense behavior we need to tell the editor what we are working on before we do any work, just like we declare variables before we use them, and starting with Select means that the IDE has no idea what we are going to query so it cannot give us any help.
 
And with VB wanting to be the most productive environment means it wants us to get as much help as possible so we need to give it a bit had to and tell it what we want to query.
 
While it wasn't my first choice I fully agree with the change as I think IntelliSense is an important productivity tool and I will get used to the syntax real quick :-)
 
Maurice de Beijer
with no comments
Filed under: ,
Previously I stated that I dislike the separation between the code and the database schema. A first look at SqlMetal.exe, a utility that comes with DLinq suggested that it might ease the problems and guess what, it does :-)
 
I created a small sample app and added the following command to the pre-build event:
"c:\Program Files\LINQ Preview\Bin\SqlMetal.exe" /database:Northwind /code:"$(ProjectDir)Northwind.vb" /language:vb

 
Build the project to have the Northwind.vb file generated and include it in the project.
 
Next I added the following code:
 
Sub main()
    Dim northwind AsNew Northwind(My.Settings.DataConnection)
 
    ' Build a list cities with multiple customers
    Dim results = From customer In ( _
                   From customer In northwind.Customers _
            GroupBy customer.Country, customer.City _
            Select it.Key, it, Count(it)) _
        Where customer.Count > 1 _
        Select customer _
        OrderBy customer.Count Descending, customer.Key.Country, customer.Key.City
 
    ForEach row In results
        ' Print the country and city
        Console.WriteLine("Country: {0} - City: {1} - Count: {2}", _
            row.Key.Country, row.Key.City, row.Count)
 
        ' Print the customers for the city
        ForEach customer In row.it
            Console.WriteLine("  => {0}", customer.CompanyName)
        Next
 
    Next
 
 
    Console.ReadKey()
 EndSub
 
 
 
 
Now build and run the project and you will see the output appear.
 
Now go open the table schema for the Customers table and change the City column name to TheCity. Save the changes and try to run. This time you will get compile errors on every reference to the City property because its no longer valid.
 
Easy to do and so much better than runtime errors that the City column name is invalid.
 
Enjoy!
 
Maurice de Beijer
with 2 comment(s)
Filed under: ,
Now you don’t normally catch Don Box doing a lot of VB programming. Now he does, maybe Erik Meijer showed him the light.
 
 
 
Maurice de Beijer
with no comments
Filed under:
One of the problems I am always having with database development is the gap between data and code. Sure it isn’t hard to move data from database to memory, manipulate it and move it back but what remains a drag is someone making schema changes to the database and only finding out at runtime that the classes no longer match the underlying database schema.

Well there might me light at the end of the tunnel :-)

One of the features of DLinq is a command line utility called SqlMetal. Using this tool you can generate the types required for DLinq to work. Basically it generates a large source file, depending on the command line parameters, that you can include in your project. Now suppose you add this command line utility to the Visual Studio projects pre-build event? This way you will always have an up to date set of entity objects matching the database schema and you will get compile errors as soon as you change the database schema.

Update: See http://msmvps.com/blogs/theproblemsolver/archive/2006/05/30/97708.aspx for an example.

Maurice de Beijer
www.TheProblemSolver.nl

with 2 comment(s)
Filed under: ,
If you, like me, are into playing with the latest toys you surely don't want to miss the latest WinFX beta. And Microsoft has just release Beta 2 so hurry and download it :-)
 
 
 
Maurice de Beijer
with no comments
Filed under:
If you do any work at all with Microsoft Office the Visual Studio Tools for Office add-on is must have. And just in case you have never done any work with it, you really need to take a look at the current version 2 of Visual Studio Tools for Office, it is seriously cool for writing small applications or a useful addition to larger projects.
 
Well anyway the development team is hard at work at the next version and have just release a CTP so we can start playing with it and giving them feedback on what we like and what we don't :-) And guess what, it’s the June CTP and it isn't even June yet, guess they are ahead on their schedule :-)
 
 
 
 
 
Maurice de Beijer
with no comments
Filed under:
Red Gate make some pretty cool tools for SQL Server. Now most of them cost some money, not a lot specially considering what you get but still they aren't free.
 
Now they have a new add that provides IntelliSense for SQL commands. And not only the keywords but also the table, columns and pretty much everything you need. And it works with a host of environments: Query Analyzer, SQL Server 2005 Management Studio, Visual Studio 2005, Visual Studio .NET 2003, SQL Server 2000 Enterprise Manager and even UltraEdit32. And best of all, it is free until September 1st.
 
 
 
 
 
Maurice de Beijer
with no comments
Filed under:
With version 1.* of the .NET framework you needed to override the ToString() function to get a useful display in the debugger watch window. While this still works as before it is not exactly an ideal way, after all the ToString() function can be called during other actions and isn’t meant for debugging purposes.
 
The primitive way of looking at a collection.
 
In version 2.0 of the .NET framework we have a better way of doing this and that is the DebuggerDisplay attribute. Using this attribute gives you quite a bit of control over how the debugger displays a type. And just in case you where thinking that a single like with inserted fields isn’t enough, you can use a read-only property or a function to return the string displayed in the attribute :-)
 
Showing the same list using the DebuggerDisplay attribute.
 
 
Module Module1
 
    Sub Main()
        Dim people AsNew List(Of Person)
 
        people.Add(New Person("Maurice", "de Beijer"))
        people.Add(New Person("John", "Doe"))
    EndSub
EndModule
 
 
<DebuggerDisplay("Person: {_firstName} {_lastName}.")> _
Class Person
    PublicSubNew(ByVal firstName AsString, ByVal lastName AsString)
        _firstName = firstName
        _lastName = lastName
    EndSub
 
    Private _firstName AsString
    PublicProperty FirstName() AsString
        Get
            Return _firstName
        EndGet
        Set(ByVal Value AsString)
            _firstName = Value
        EndSet
    EndProperty
 
    Private _lastName AsString
    PublicProperty LastName() AsString
        Get
            Return _lastName
        EndGet
        Set(ByVal Value AsString)
            _lastName = Value
        EndSet
    EndProperty
 
 
    PublicFunction DebuggerDisplayFunction() AsString
        Return _firstName + " " + _lastName + " from a function"
    EndFunction
 
    <DebuggerDisplay("Person: {DebuggerDisplayFunction}.")> _
        PublicReadOnlyProperty DebuggerDisplayProperty() AsString
        Get
            Dim name AsString
            name = _firstName + " " + _lastName + " from a property"
            Return name
        EndGet
    EndProperty
EndClass
 
Maurice de Beijer
with no comments
Filed under:
One of the things that is sort of hard to do is change the value of parameters to constructors in my classes. Take following, very simple example:
 
PublicClass BaseClass
    PublicSubNew(ByVal param AsInteger)
        Console.WriteLine(param)
    EndSub
EndClass
 
PublicClass DerivedClass
    Inherits BaseClass
 
    PublicSubNew(ByVal param AsInteger)
        MyBase.New(param)
    EndSub
EndClass
 
Now if I want to change the value of param in the constructor in the DerivedClass I can't simply add change it's constructor to:
 
PublicSubNew(ByVal param AsInteger)
    param = param + 1
    MyBase.New(param)
EndSub
 
This code won’t compile because the call to the base constructor must be the first line of code.
 
However it turns out you can include a call to a shared function in the call to the base constructor. This allows you to execute any code you want when the objects is instantiated before the base class constructor is called and gives you ample opportunity to change the value passed to the base class.
 
PublicClass DerivedClass
    Inherits BaseClass
 
    PublicSubNew(ByVal param AsInteger)
        MyBase.New(ChangeParam(param))
    EndSub
 
    PrivateSharedFunction ChangeParam(ByVal param AsInteger) AsInteger
        Return param + 1
    EndFunction
EndClass
 
So what restriction do you have? Well the object isn’t instantiated yet so the function needs to be shared and you cannot pass the object reference to it and set any properties/fields.
 
Maurice de Beijer
If you are in the Netherlands a do any form of serious .NET development I expect you are here in Papendal for the annual SDC conference. If not you are surely missing one of the better events in the Netherlands this year. There are loads of excellent speakers and around 400 people attending this year.
 
Well we are just about to start so if you are here enjoy and have a fun and informative two days :-)
 
Maurice de Beijer
with no comments
Filed under:
Don’t forget, next week Monday and Tuesday is the SDC conference in the Netherlands. We have a large selection of first class speakers so it is certainly worth your time. And there are just a few places left so make sure you don’t miss the opportunity.

See you there next Monday!

Maurice de Beijer
www.TheProblemSolver.nl


with no comments
Filed under:
Scott Guthrie and his team just released the Visual Studio 2005 Web Application Project. Even though I am not much of a web developer and mainly do winform development I am still pleased to see this. I did a few small ASP.NET 2.0 projects and wasn't all that happy with the project less way of working. Even though I haven’t tested this yet I hear good things about it from those in the know. One thing is certain, my next ASP.NET project will be using this.
 
Take a look at http://weblogs.asp.net/scottgu/archive/2006/05/08/445742.aspx for a complete post by Scott explaining how things work.
 
Maurice de Beijer
with no comments
Filed under:
Microsoft has released a new font specially for use in Visual Studio 2005. This font is supposed to make you code easier to read and is designed for use with Clear Type enabled. First impression is yes the code is more readable so I am going to run with it for a while and see if I still like it after a few weeks.
 
with no comments
Filed under:
I recently ran into a nasty bug with object deserializing in the .Net framework.
 
Basically using the SerializationInfo object to save an ArrayList with a custom type also implementing ISerializable loses the data. The data is actually serialized and even deserialized, just not in time to put it back in the ArrayList so effectively getting lost.
 
Wasted some time tracking this down :-(
 
 
Maurice de Beijer
with no comments
Filed under:
Are you going to the TechNet/MSDN Briefings in 't Spant in Bussum tomorrow? If so come by and say hello. I am not quite sure where but I should be around the Microsoft/Community area somewhere.
 
If you missed the opportunity to register and found that the show was fully booked you might still be in luck. Even though you can't go to the location itself it will be broadcasted on the Internet so you can still get the latest info.
 
Enjoy the show.
 
 
Maurice de Beijer
with no comments
Filed under: