Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

May 2005 - Posts

Rico Mariani's Articles and Recommendations

About a week ago, Bob Gladon blogged about the performance of string concatenations. Erwyn van der Meer, a colleague of mine who unfortunately still doesn't blog here, responded to that post with a link to Rico Mariani's weblog. Rico regulary blogs about performance issues.

I just bumped into a video on channel 9. So I finally got to see what he looks and sounds like and to hear him talk about performance issues. But even better. When I looked at the comments posted with that video, I discovered a link to a document with links to articles and recommendations regarding performance. The same document also list a few performance quizes, which I will try soon to see if I really understand it all, and links to articles related to the garbage collector. Most items have already been blogged, but this list really saves you a lot of time. And it all makes sense, at least to me. You can find the list here,

Listen, grasshopper

I recently read an Product Showcase on The Code Project about an add-in called Grasshopper. Grasshopper is developd by MainSoft and allows you to create and deploy .Net applications for J2EE environments. From that document:

Consider this – many companies have Linux on their radar for some form of inclusion in their strategy, be it on the desktop or in the data center. To build applications for Linux, you would probably have to learn a new skill set such as GTK or Java™. For server side or hosted applications, the logical candidate is usually J2EE, due to its cross platform nature and its well-known security, manageability, performance and scalability characteristics. However, to develop J2EE applications, you need to learn the Java language, Java Servlets, Java Server Pages, JDBC for database connection, and even Enterprise Java Beans for distributed applications.

What if, as an alternative, you could broaden the reach of your skills to Linux and other Java-enabled platforms, and as a result, extend yourself (and your resume) in a new and exciting area? What if you could do this without rewriting most of your code, and instead re-use your existing C# code?

Although interesting, I dismissed it. It takes enough time already to look at Visual Studio stuff as it is. But today, one of the PHP/Linux developers in my current project thought it was really cool. And I must admit that being able to write J2EE applications while still working in C# or VB.net is pretty neat.

Posted: May 31 2005, 05:04 PM by Jan Schreuder | with no comments
Filed under:
Getting started with Enterprise Library 1.0

Card GameOn David Lem's blog I found a link to the Enterprise Library 1.0 Hands On Labs.

This download includes 8 labs in C# and VB.Net that explain how the library can be used. Each lab takes about 30-60 minutes to complete. A great way to get started with the Enterprise Library (if you haven't already).

Visual Studio 2005 Starter Kits and Code Samples

Starter kitsCard Game

To help get you started with developing stuff in Visual Studio 2005, Microsoft has developed a few starter kits which you can download here. At the moment, there are 6 starter kits available.

One of the kits is a Card Game Starter kit. This Visual C# Starter Kit is a complete Black Jack card game. The starter kit contains an extensible framework for building card games and a Black Jack game application that is built on top of this framework. I know card games aren't the coolest of games, but it does give you insight into how a simple game might be developed.

Code Samples

The code samples page on MSDN is a good resource for getting other sample applications, or samples explaining the various technologies in the .Net framework. You can find sample code on Remoting, Serialization, Garbage collection and many other subjects.

If you're relatively new to .Net these samples are very helpfull. I use them every now and then to help explain stuff to co-workers.

CommandBuilder object, update

A few weeks back, I blogged about using the SqlCommandBuilder object to create Create, Update and Delete objects for a table. This method does however have one drawback. When your table contains an auto-identifier then SqlCommandBuilder does not handle that very well. Usually, you'd want to have that value updated in your dataset when you call UpdateDataSet. Another thing is that the update and delete statements have very large where clauses, where normally you would be happy with just the primary key.

To work around this problem, I created my own SqlCommandBuilder, which you can download here. The commands created with this version are aware of auto-identifiers when they exist. This version supports tables with or without primary keys.

If you have comments, please don't hesitate to add them.

Crystal Reports and Visual Studio

I have used Crystal Reports in a number of .Net projects. It is shipped with Visual Studio 2003 and will also be part of Visual Studio 2005. If you want to use Crystal in your application, you will need to register the version that comes with Visual Studio. Registration of that version is free, and it get's you access to developers section on the Business Objects website.

Today I was asked to get a co-worker started with Crystal Reports and so I went looking for information. I soon discovered that a lot of interesting stuff is on MSDN, you just need to know where to look. All the information below, and more, can be found here on the Microsoft MSDN website.

Crystal Reports Reviewer's Guide

This reviewer's guide provides an overview of the features, power and ease of use of Crystal Reports for Visual Studio .NET. It advertises Crystal reports as follows:

Crystal Reports for Visual Studio .NET is the definitive out of the box reporting technology for use in .NET Windows and Web applications.

Getting Started Guide

This whitepaper provides step-by-step instructions on the details of generating and displaying reports in various deployment scenarios and application environments using Crystal Reports for Visual Studio.NET 2003. And you can download all the code for this guide here.

Interested?

I think everyone should be interested in Crystal Reports. It's easy to use, supported by Microsoft, and you can deploy your applications using the version in Visual Studio without a problem or licensing fee. I know Reporting Services are cool, but you need SQL Server to use it. And sometimes you have to work with Oracle, MySQL or even Access. 

Those of you that have Visual Studio 2005 installed can quickly see what Crystal Reports has to offer. To do so, follow these steps:

  1. From the file menu, select New Web Site.
  2. In the dialog you should see ASP.NET Crystal Reports Web Site. Select that project type.
  3. When Visual Studio has finished creating the project, press F5 to run the website.

You should see a pie-chart with sales figures for various countries. Click a section of the pie chart, to zoom in on the sales for a particular country. Crystal then shows another chart with the sales for that country specified by state or region. The demo report allows you to go 5 levels deep.

You can also print the report, using the print button on the webpage. Or export it, using the export button, to one of the following formats: Acrobat PDF, Word, Excel 97-2000, Rich Text Format.

Memory allocations revisited

A few weeks back I blogged about optimizing memory use in a .Net application. On that note, I was reminded to some links to Microsoft documents on the internet that deal with memory leaks and how to debug them. I posted them before, but with my recent post in mind and the blog now being in English, I moved the document links here.

The documents apply to both VB.Net and C#, even when the title suggests otherwise.

#ZipLib - Open source Zip, GZip, BZip2 and Tar Implementation For .NET

If you ever need to zip files in one of your projects, then have a look at #ZipLib.

#ziplib (SharpZipLib, formerly NZipLib) is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is implemented as an assembly (installable in the GAC), and thus can easily be incorporated into other projects (in any .NET language). The creator of #ziplib put it this way: "I've ported the zip library over to C# because I needed gzip/zip compression and I didn't want to use libzip.dll or something like this. I want all in pure C#."

The sources and/or binaries can be downloaded here.

Posted: May 23 2005, 08:20 AM by Jan Schreuder | with no comments
Filed under:
It really is a BETA version

Last night, I was anxious to try some stuff with the new VS 2005 Beta I installed recently. First step was to try and create a small database for my sample project. When I opened SQL Server Management Studio, I couldn't connect to my database server. SQL Server 2005 said something about "Data source name not found and no default driver specified". Fortunately, Patrick Wellink was online so I asked him if he had any problems. I knew he'd also installed the new Beta stuff. And as it happens, he did. He had also installed VS 2005 Team Suite and his installation was constantly nagging about a failing connection to Team Foundation Server.

We spent about an hour on the phone, discussing possible solutions. He then un-installed SQL server 2005 and then re-installed SQL server 2005. This didn't help with his connection problem. So he un-installed SQL server 2005 again and un-installed the VS 2005 Team Suite. He then installed SQL Server 2005 again and finally his database server was running correctly.

So went about it the same way. I spent a couple of hourse un-installing all the new Beta stuff. I then cleaned up any directories that were left behind. I then re-installed SQL Server 2005 and VS 2005 Team Suite, in that order. And now everything is working as I expected it to. It took me 4 hours in total to sort out this mess.

Absolute Beginner's Video Series to Visual Studio 2005 Express Editions

I was asked today if I knew of a way to get started with Visual Studio 2005. So I checked the Visual Studio Express Edition website and found the Absolute Beginner's Video Series to Visual Studio 2005 Express Editions.

This video series is designed specifically for individuals who are interested in learning the basics of how to create applications using Visual Basic 2005 Express Edition and Visual C# 2005 Express Edition. This includes over 10 hours of video-based instruction that walks from creating your first "Hello World" application to a fully functioning RSS Reader application.

Lesson Outline

  • Lessons 1-3: Workflow, Visual Studio Express Interface (Now Available!)
  • Lessons 4-7: Programming Language Basics (Coming Soon)
  • Lessons 8-11: Working with Data and SQL Server 2005 Express Edition (Coming Soon)
  • Lessons 12-16: Creating an RSS Reader (Coming Soon)

    I'm assuming this won't be of interest to the experienced readers here. But if you're interested in developing software using VB.Net or C#, and you're a beginner, I can really recommend these lessons.

  • Cool MSN style notify window

    At my current assignment, critical errors that may occur inside the various webservices are logged in a database. Provided that database is up-and-runnnig of course ;-) What we as developers needed however, was a way to monitor if anything had happened to one of the webservices. So, I set out to write a notifier application using the NotifyIcon control. The application had to have one of those MSN like pop-up windows that would alert me (or others) if something interesting was written to the logging database. I found two options on CodeProject.com:

    NotifyWindow by Robert Misiak

    This control displays an MSN-like notification window. It's pretty simple to use, and comes with another class (NotifyWindow2000) that will display the window untill there is mouse or keyboard activity.

    TaskbarNotifier by John O'Byrne

    This control has a lot more features and I am using this one for my notification app. One of the best features is that this control is skinnable. So you can make it look like a normal MSN-pop-up, or add your own bitmap to make it look fancier.

    Posted: May 20 2005, 07:51 AM by Jan Schreuder | with 2 comment(s)
    Filed under:
    How to: Create a dummy webservice using a WSDL file

    In most situations, at least in my case, when you need to build an application that consumes a web-service, that particular web-service is already up-and-running. Creating a webreference is then usually sufficient to use it in you .Net application. This week however, I was given a WSDL file and told to build an application using that information only. The web-service itself is still in development and they could not yet give me a version to test with. What I needed was a dummy web-service which supplied the interface based on the WSDL file.

    Erwyn van der Meer pointed me to a great tool from ThinkTexture that could do all this. See also Ernst's blog for more info. Unfortunately, this solution was not an option, since at my current assignment, I'm not allowed to install software on the development machine. So instead, I ended up doing it manually. Here's how I did it:

    Step 1 - Create an empty web-service

    Open Visual Studio and create a new project of type ASP.NET Web Service. Give it a proper name and click OK. VS will then create the entire project for you.

    Step 2 - Create source code using WSDL

    Open a Visual Studio .Net Command Prompt window and goto the directory where the supplied WSDL file is located. To generate application code, based on the WSDL definition, we use the WSDL tool. Depending on the language used to create your web-service, issue one the following commands:

    • VB.Net: WSDL /language:VB NAME.WSDL
    • C#    : WSDL /language:CS NAME.WSDL

    The WSDL tool will generate an abstract class in the language you selected.

    Step 3 - Add the created source to the project

    The source file that was generated by the WSDL tool then needs to be added to the VS project. The Service1.asmx file inherets from System.Web.Services.WebService by default. Change this so it inherits from the class generated by the WSDL tool. After this has been done, all abstract methods defined in the generated class need to be implemented in the Service1.asmx file.

    Step 4 - Compile the project

    Last step in the process is to compile the project. If you get any compilation errors you need to fix these, of course. When compilation is succesful, your dummy web is ready for use.

    Coding conventions

    In addition to Rick's post on comments in code, I couldn't resist adding my own post about coding conventions.

    Comments

    One last remark about comments in code and self-explanatory method names and variables. You may think this is non-sense, but someday you will be confronted with stuff you've written years ago. From my own experience, in may 2004 I was asked to help solve a problem in software written in 1992! It was C code written for MS-DOS. Thanks to the comments and the self-explanatory method names I was able to quickly understand what I intended to do back then. So if not to help others, help yourself and write comprehensible code and comment whenever things get too complex. 

    Coding Guidelines

    A lot of developers try to conform themselves to coding standards in an effort to improve the quality of the code. I was surprised about the number of different coding standards available when the question for an example standard hit our companies mailing list recently. Different teams adhere to different standards. Some examples:

    1. Iridium Software C# / VB.Net Coding Guidelines
    2. Lance Hunt's Coding Standards for .Net
    3. Microsoft Design Guidelines for Class Library Developers

    And there are more of these around, simply google for “coding guidelines“. I scanned briefly through some of these documents and found most of it is using common-sense when writing code. If you need guidelines, make sure everyone on the team uses the same guidelines. And regularly check (code-inspections) to see if the code is still in compliance with these standards. One thing though, coding guidelines are nothing more than guidelines. There might be a good reason not to conform to them in some situations, but then the developer must be able to explain why. Just like code-inspections, guidelines are merely a tool to help improve code, not the solution!

    Refactoring and measurements

    Gerke Geurts briefly mentioned Fowler's book on refactoring. He quotes the book, saying code that requires comments to be understood is highly likely in need of extraction to a new method. The method name should describe the intent of the code sufficiently well to take away the need to comment. I agree. I try to keep methods as simple as possible.

    To make sure this is the case, I currently use DevMetrics. DevMetrics is a C# code analysis tool that gathers software metrics on size and complexity. Of particular interest are the complexity figures presented by this tool. For each method in your class, the tool looks at the flow of and the number of lines. The higher this number is for your method, the more complex it is. When you want to refactor large and incomprehensible methods, this is the tool to find them. Using this tool will give you insight into the complexity of your code. You can download a Community Edition (freeware) version here.

    Performance comparison between VB.Net Collection and C# Queue and SortedList

    I have done a performance test to compare VB.Net Collection against C# Queue and SortedList. I was triggered to do this, because of a response to my previous post about using VB.Net Collection. The response argued that the performance of Collections are abysmal, because of the way it is implemented.

    Remember, we neede a list object that would not change the order of the items you added to it. Let me first show you the code I created to run the tests. The code adds 1 million entries to the list object used.

    VB.Net Collection

        DateTime startDate = DateTime.Now;
       
    Collection col = new Collection
    ();
       
    for (int
    i = 0; i < 1000000; i++)
            col.Add(
    "Item: " + i.ToString().PadLeft(8, '0'), null, null, null
    );

        TimeSpan duration = DateTime.Now - startDate;
        label1.Text = duration.ToString();

    C# Queue

        DateTime startDate = DateTime.Now;
       
    Queue col = new Queue
    ();
       
    for (int
    i = 0; i < 1000000; i++)
            col.Enqueue(
    "Item: "
    + i.ToString().PadLeft(8, '0'));

       
    TimeSpan duration = DateTime
    .Now - startDate;
        label3.Text = duration.ToString();

    C# SortedList

        DateTime startDate = DateTime.Now;
       
    SortedList col = new SortedList();

       
    for (int i = 0; i < 1000000; i++)
            col.Add(i.ToString().PadLeft(8,
    '0'), "Item: ");

       
    TimeSpan duration = DateTime.Now - startDate;
        label2.Text = duration.ToString();

    The results here are influenced a little by the string operations I added to the code. But we needed to be sure that the items always have to be in the order in which they are added to the list. For the SortedList, that meant we needed to ensure the key we have to provide to the add method would not change the order. So I added the same string operations to the other tests, just to make sure that all tests did approximately teh same. After running the test application, this were the results:

    • Collection: 1.984375 seconds
    • Queue     : 1.609375 seconds
    • SortedList: 5.1875 seconds

    Conclusion:

    As you can see, the Queue is the fastest here. The Vb.Net collection is about 23% slower in this example. But I ran the test without the string operations, and then the difference is only 13%. The string operations definitely influence this test. We add 1 million items, and when you add this much items, 0.38 seconds isn't really that much. I can imagine situations where this difference might be a real issue. But not everyone needs a list to store 1 million entries. The SortedList is by far the slower one.

    So which should you use? Depends on what you need to build. But I don't see a problem in using VB.Net collections in C#.

    The tests were performed using Visual Studio 2005 on a P4 3.0 gHz with 512 Mb internal memory.

    The C# "using" construct
    Cyrus Blather has written a very detailed article about the “using” construct and the pro's and con's of using it. I read it earlier this week and find it really interesting. I decided to blog the link after talking to a colleague about the using construct and what actually does. This article really explains it.
    More Posts Next page »