Hurray! There is a new and faster Device Emulator in Visual Studio 2005. Deployment and debugging of .NET Compact Framework apps used to be very slow and tiring. Some bugs were fixed and deployment is significantly faster in Device Emulator ver. 2.0 (download here). Installer will replace the old version in your Visual Studio.
Monday, November 12, 2007
Friday, September 21, 2007
Recently I have produced a code with strange behaviour in my real C# project. Look at this code:
There is nothing strange. The method
Mystery.Foo(int)is called repeatedly. Then I replaced hidding method
Mystery.Foo(int)by overriding method:
And the output is:
Hah! What is the matter? Let us go to explore IL code of method
ihas been boxed. Why? I would expect that the IL body of method
Mystery.Foo(int)will be the same in new and override declaration but it is different. It is very strange behaviour of compiler. What is your explanation?
Overridded method in my real project was the right one. In addition, I renamed the methods, because my colleagues could be confused like me.
Monday, September 17, 2007
Launching tests directly from Visual Studio is very comfortable feature. Developers, who are familiar with test driven development, need to launch tests frequently, so this feature is necessary for them.
My favourite testing framework is NUnit. Tests in this framework are launched from external application with perfect GUI.
In this article I will demonstrate how to start external application from Visual Studio, e.g. NUnit GUI, and keep running debugger. Debugging NUnit tests is very significant advantage because standard GUI of NUnit framework does not support it.
So, suppose that you have created a class library project with the following NUnit test:
Now, you want to start this test directly from Visual Studio and use some breakpoints in the test. You can do it without any VS plug-in, just edit project settings. Open Project | Properties | Debug and set values like this:
Enter Start external program and Command line arguments, that is the path to the assembly containing your test.
For launching NUnit GUI and debugging tests press F5.
Monday, June 11, 2007
It is usual, that you write an application with GUI for a specified culture and then you suddenly get to know that the application should work with several different currency formats. In the following example I want to demonstrate a very simple method how to change the currency format in your application elegantly and dynamically.
Ordinarily numbers are formatted as currency by these ways:
Actually, there are many places like this in your application and you want to change the format depending on user's choice of culture. The following code comes in handy:
The whole example is:
List of all available culture names and identifiers is on MSDN.
Thursday, May 24, 2007
Maybe you wondered about syntax-highlighted code in this blog but
blogger.com does not provide any tool for this. If you use Visual Studio, just try this CopySourceAsHtml add-in.
CopySourceAsHtml generates HTML source of your syntax-highlighted code in VS. You can select and copy your code as HTML to clipboard, so your syntax-highlighted code can be placed anywhere.
Here are my settings of CopySourceAsHtml add-in:
My "File Style" is:
Here is an example output:
Wednesday, May 23, 2007
There is a very simple way how to use OpenGL library in .NET application using C#. The library what you need can be downloaded here csgl.sourceforge.net. The simplest way is to add these downloaded files to your project:
Create a new Windows application project. In the new solution add a new project Class library named
GLViewControlproject and reference
csgl.dlllibrary. Reference also
Set the "Copy to output directory" property for
csgl.native.dllfile. Presence of this file in the output directory is important unless you have to install
csglon each system particularly.
Now the project is ready for our code. Add a new class in
And now just drag and drop a new
GLViewcontrol from the toolbox in VS on your form and run the application. The result looks like this:
Wednesday, May 16, 2007
If you tried to concatenate strings from group in SQL, probably you would start looking for an aggregate function similar to SUM or COUNT. There is no function like this in SQL Server 2005 and even further there is no way how to create a custom aggregate function using SQL. You can use just built-in aggregates.
Fortunately, SQL Server 2005 integrates common language runtime (CLR). So, the solution is to write an aggregate function in C# and include it among built-in functions in SQL Server.
Let us show an example with aggregate function for strings concatenation. At first you should create a new project in VS - use template Class Library, name it for example MyCLRLib.
The key methods are:
Init()- initializes private variables
Accumulate()- appends the next value
Merge()- merges partial aggregates
Terminates()- returns a result aggregation
Now you can register this function in SQL Server. First of all, enable .NET CLR code execution in your SQL Server:
and then create new aggregate function from your compiled assembly:
Now you can write:
Tuesday, May 15, 2007
There are powerful tools in Unix such as grep, sed etc. These programs make use of regular expressions - regexp. Luckily, .NET contains a very useful class - a
Regex class in a
Before using of regular expressions you need to understand the syntax. Here is a basic:
|.||Matches any single character|
|^||Matches the beginning of a string|
|$||Matches the end of a string|
|*||Matches the preceding character or subexpression zero or|
|+||Matches the preceding character or subexpression one or|
For more information about regular expressions syntax see this link
Now I can show how it is being used. The key method is
The following example validates an e-mail adress:
For more information see:
.NET Framework Regular Expressions (MSDN)
System.Text.RegularExpressions Namespace (MSDN)
Friday, May 11, 2007
.NET provides a functionality for storing and loading objects. This post shows how it can be used. The mechanism is called serialization and its purpose is to convert any object to stream of bytes that can be saved on a disk, sent by a protocol via internet etc.
So, at the beginning you have a class which you want to serialize. For example it looks like this:
Now, you want to save object of this class on a disk. At first add some namespaces:
[Serializable]attribute to your class:
and then add methods for save and load in
Here is an example how to use these methods:
That is all. It is very simple to use.
Note: Instead of
BinaryFormatteryou can use
The code is the same, only replace
System.Runtime.Serialization.Formatters.Soapnamespace. For this namesapce you have to add reference to
System.Runtime.Serialization.Formatters.Soap.dllassembly in your project.
The difference is in the saved file. With
SoapFormatteryou get a file that is human readable. For our example, the saved file contains:
Notice how the reference-typed objects are stored.
Anyone can change the content of this file and the object will reflect these changes after deserialization.