Sunday, March 21, 2010

Verbatim

Recently, we have discussed verbatim programs with my colleagues. Verbatim program prints itself on the output. I know that we did such a thing at the university in Pascal, but I could remember little of it. Thus I have decided to create one from scratch, although in C#. It took me some time to find a proper quotes escaping, that was the most difficult part of the program I would say. Still it is nice to see that even tough looking problems (initially) may have relatively easy solutions to be reached in a few minutes.

Here comes the source, perhaps not the best of its kind, but working fine.


using System;

namespace Verbatim
{
class Program
{
static void Main(string[] args)
{
const string ap = "\"";
const string s =
@"using System;

namespace Verbatim
{{
class Program
{{
static void Main(string[] args)
{{
const string ap = {1}\{1}{1};
const string s =
@{1}{0}{1};
Console.WriteLine(s, s, ap);
}}
}}
}}";
Console.WriteLine(s, s, ap);
}
}
}

Thursday, January 28, 2010

Long file names and paths

It was somewhat difficult for me to realize the file name length restriction in Windows. Finally I got it: The file name can only have the length such that the resulting full path will not exceed 260 characters.

E.g.: In C:\SomeDir\ you can create a file with 248 characters in name (including the extension), while in C:\SomeDir\SubDir\ you can use only 241 characters for a file name.

However, file systems used in Windows can handle paths up to 32,760 Unicode characters with each path component no more than 255 characters[1] It is possible to work with such long paths (i.e. exceeding 260 characters) in Windows, although there are some drawbacks. The best I have found regarding this topic is on BCL Team Blog.

Note: If you assign a drive letter to some folder, then working on that "drive", the full path will not contain the original folder anymore. Thus you can create a file, that you cannot work with properly in the original folder (the resulting path too lengthy), but still can be fully accessed on the "drive".

E.g.: Having mapped \\server\some_long_directory_structure\ to a virtual Z: drive, you can create and fully use a file Z:\some_file_with_long_name. However, that file may be difficult to work with when accessed as \\server\some_long_directory_structure\some_file_with_long_name.

Friday, January 15, 2010

TDD Kata

I did Roy Osherove's TDD Kata. It was refreshing and fun. When it came to step 8, my learning was stimulated, too. Well done, Roy!

I even plan to try this when doing pair programming again. I think such an exercise may help the pair to acclimatize.

I wish there was some "TDD Kata of the Day" page...

Monday, November 23, 2009

SelectedIndex if nothing selected

SelectedIndex property of Infragistics' WebCombo class has the following syntax


public int SelectedIndex {get; set;}

The documentation reads 'If no row is selected, this property returns Nothing or Null.' That sounds suspiciously since int cannot be null.

The property actually returns -1 if there are no selected rows.

By the way, the index is zero-based, i.e. the first row has the index 0.

Thursday, November 12, 2009

'+' not supported in datakey

Users reported us that sometimes the selection is lost in UltraWebGrid. We managed to discover that the problem occurs only when the key column contains '+'. The selection is recognized OK on client side, but on server side it behaves as if the selected row was the one without '+', i.e. none if there was no such row.

Having the data source


[DataObject]
public class DataProvider
{
[DataObjectMethod(DataObjectMethodType.Select)]
public static IEnumerable SelectData()
{
yield return new { Column1 = "a+b" };
yield return new { Column1 = "a b" };
}
}

and the grid

<igtbl:UltraWebGrid ID="UltraWebGrid1" runat="server" DataSourceID="ObjectDataSource1">
<Bands>
<igtbl:UltraGridBand DataKeyField="Column1">
<Columns>
<igtbl:UltraGridColumn Key="Column1" BaseColumnName="Column1" IsBound="True"></igtbl:UltraGridColumn>
</Columns>
</igtbl:UltraGridBand>
</Bands>
<DisplayLayout …>…</DisplayLayout>
</igtbl:UltraWebGrid>

you can select the row with "a+b", but once you enter server code (e.g. for server event handling), the selection is changed to "a b".

We needed to introduce another column to store the key value with '+' escaped somehow (e.g. replaced for another character).

Updated 3 February 2010:
I have been told by a colleague that even server events are not raised for a row having '+' in its key.

Thursday, September 3, 2009

Comma-separated Lists

One needs to convert a list of objects to comma-separated list of values sometimes. I often see StringBuilder used with some sort of loop. The most intellectual challenge is not to write comma after the last item; either there is some conditional expression in the loop step or some post processing is included to remove the last comma. Usually the result is consisting of several lines of code, perhaps too many for the purpose.

Users enthusiastic about Linq tend to involve Enumerable.Aggregate. Still String.Join may be the most straightforward method many times.

Thursday, July 30, 2009

The First Contact with FO.NET

I had a training recently on XSLT and XSL-FO. We were using java Apache FOP there. I was quite interested in the technology and was keen to give FO.NET a try.

Yesterday, I got to it finally. I found two drawbacks so far. Attribute width on inline element had no effect and I only found a workaround by using table. Then I discovered that block-container is not supported by FO.NET. But since you can have nested blocks, it is not a big issue.

I only worked with relatively simple document, so I guess there may be much more issues when using FO.NET. Still I was pleased with the output. I think FO.NET (and XSL-FO in general) might be a good option for creating PDF documents, especially when the source data are in XML.