There is one more catch with boxing (see also my former post Boxing and explicit conversions). Consider the code
public struct X
{
public string Value { get; set; }
}
public void Fun()
{
X x1 = new X { Value="original string 1" };
X x2 = new X { Value="original string 2" };
X[] array = new X[] { x1, x2 };
array[0].Value = "modified string";
//! x1 is not changed
}
I am occasionally amazed that x1 has "original string 1" on the line marked '!' although array[0] reads "modified string". Hell yes, that is because of boxing again. If X was class, x1 would be changed, too.
Wednesday, October 1, 2008
Boxing and arrays
Boxing and explicit conversions
Imagine you are writing a method that will process a number. You need to accept object as an argument.*) You know that you will accept integers. You have considered the usage of int and long, and you have chosen long since int can be safely converted to long. So you prepare something like
void ProcessNumber(object o)
{
long number = (long)o; //!
//do something with number
}
You test your method, and — surprise — you receive System.InvalidCastException on the line marked '!' when calling ProcessNumber(1). The problem is that boxing is involved, and the argument is not longer just int.
You can cast a boxed value only if it is of the very same type!
If it is not certain that you will receive single type in all calls, you had better use the appropriate method of the
System.Convert class.*) Consider accessing of properties via reflection.
Thursday, April 10, 2008
Quotation of the Day
"If you do serious Oracle Development, you will be clueless sooner or later." source
Tuesday, April 1, 2008
Edit .csproj file in Visual Studio
I bet everyone had the need to view or edit the content of .csproj file. I used to use notepad to do that. Accidentally I've discovered that it is possible to do so directly in Visual Studio. First you have to unload the project. You can use the context menu on the project in Solution Explorer to do so. Then this context menu changes so that it contains the item "Edit .csproj", by clicking which you can access and edit the content of .csproj file. Don't forget to "Reload Project" after you are finished.
Changing the size of paper in ActiveReports
I wanted to change the size of paper to A4. Thus I set the report's property PageSettings.PaperKind to System.Drawing.Printing.PaperKind.A4. The report designer reflected the change, but quite surprisingly I received the error when running the report:
Printer does not support A4 papersize. Please use PaperKind.Custom in your PageSettings or specify a papersize supported by the current printer
So I used
PaperKind.Custom even though my printer supports A4 very well and specified the dimensions of A4 manually. What was my surprise when Adobe Acrobat Reader (viewer) still used the size of Letter! I searched ActiveReports' Support Forums and managed to find the idea of the solution: Document.Printer.PrinterName must be set to an empty string, after that you may specify both PageSettings.PaperKind and Document.Printer.PaperKind arbitrarily; the latter is esential for viewer.
Monday, March 31, 2008
Domain-Driven Design, the Book
I'm really looking forward to the Domain-Driven Design book. I've read the preface and part of chapter 1 so far and this book really seems worth reading.
Tuesday, February 12, 2008
Limited Formatting in Bind
You can specify not only what property you are binding to, but also the format that should be used. Example:<asp:TextBox Text='<% Bind("SomeProperty", "{0:N2}") %>' />
to get number with two decimal digits.
The problem is that the format is applied in one-way manner. When the number is loaded, it is formatted perfectly. But the very same number (without editing) can be stored no more. (It depends on culture. I have discovered this when using non-us decimal separators.)