Archive for January, 2010 – Handling optional primitives

January 27, 2010 1 comment

If you are dealing with protos containing optional primitives, it’s not immediately obvious how you’d deal with them in C#.

For example, if the proto contains an optional int and you generated the C# class, the results would as follows:

optional int32 bar_ukey = 13;

>protogen -i:BarTest.proto -o:BarTestProto.cs

private int _bar_ukey =default(int);
[ProtoBuf.ProtoMember(13, IsRequired = false, Name=@"bar_ukey", DataFormat = ProtoBuf.DataFormat.TwosComplement)][System.ComponentModel.DefaultValue(default(int))]
public int bar_ukey
get { return _bar_ukey; }
set { _bar_ukey = value; }

As you can see, the highlighted default value doesn’t take the optional nature of the field into account.

To fix this, protogen needs to be run with detectMissing argument as follows:
protogen -i:BarTest.proto -o:BarTestProto.cs -p:detectMissing
which results in the property getting generated as

private int? _bar_ukey;
[ProtoBuf.ProtoMember(13, IsRequired = false, Name=@"bar_ukey", DataFormat = ProtoBuf.DataFormat.TwosComplement)]
public int bar_ukey
get { return _bar_ukey?? default(int); }
set { _bar_ukey = value; }
public bool bar_ukeySpecified
get { return _bar_ukey != null; }
set { if (value == (_bar_ukey== null)) _bar_ukey = value ? bar_ukey : (int?)null; }
private bool ShouldSerializebar_ukey() { return bar_ukeySpecified; }
private void Resetbar_ukey() { bar_ukeySpecified = false; }

Side note: Also tucked away in the -p template property observable and preObservable options for the observable pattern.

Categories: .net

C#: Hidden or lesser known features?

January 26, 2010 1 comment

Aaron blogged about some C# features that aren’t very well known. I won’t call them hidden, actually, as I came across them in the documentation and have been using coalesce, assignments, and to a lesser extent, partial methods (when I do use v3.5).

In any case, here’s another list that’s related. You never know what you’ll find.

Categories: .net

Kindle Development Kit?

January 21, 2010 1 comment

Amazon just announced an upcoming development kit for Kindle. As a Kindle user, the possibilities are interesting. Not sure how much I’ll use the active content though, given the enormous drain on the battery caused by wireless usage.

Categories: Uncategorized

Useful WPF binding

January 12, 2010 Leave a comment

See here.

Categories: .net Tags:

Entity Framework update

January 12, 2010 Leave a comment

This might be old news to some, but I’ve only been watching from the outskirts so far.

Categories: .net