New Fun Blog – Scott Bilas

Take what you want, and leave the rest (just like your salad bar).

Archive for the ‘quickie’ Category

Enable SkyDrive for OneNote 2010

with 5 comments

Using OneNote 2010?

Trying to create a new notebook on SkyDrive through OneNote and can’t choose “Web” from File | New? Error is “This service is disabled by policy. Contact your system administrator to enable it.”?

Solution:

HKEY_CURRENT_USER\Software\Microsoft\Office\Common\WebIntegration\WebIntegrationEnabled=1

Written by Scott

July 15th, 2011 at 8:22 am

Posted in quickie

Quickie: “NullOr” For Fluent Null-Friendly Member Access

with 2 comments

Are you a fan of C#’s ?? operator like I am? It’s one of the lesser known features of the language, so don’t feel bad if you don’t. I like it because I’m a fan of special language syntax to shorten super common tasks. For example, it lets you change this:

[code lang=”csharp”]
if (obj == null) { obj = defaultValue; }
Console.WriteLine(obj.Name != null ? obj.Name : "<null>");
[/code]

…into this:

[code lang=”csharp”]
obj = obj ?? defaultValue;
Console.WriteLine(obj.Name ?? "<null>");
[/code]

Awesome. That covers a lot of simple use cases. Now, there’s another super common case: accessing members of objects that may or may not be null. Like this:

[code lang=”csharp”]
var name = obj != null ? obj.Name : "<null>";
var lname = obj != null && obj.Name != null ? obj.Name.ToLower() : "<null>";
[/code]

Yuck.

It would be great if C# had a ?. operator that lets you tersely express “return the member if the object is not null, otherwise return null”. Then we could do this instead:

[code lang=”csharp”]
var name = obj?.Name ?? "<null>";
var lname = obj?.Name?.ToLower() ?? "<null>";
[/code]

Well, we can do something like this using one of my favorite tools, the extension method.

[code lang=”csharp”]
public static class ObjectOperations
{
[DebuggerStepThrough]
public static TReturn NullOr<TIn, TReturn>(
this TIn obj, Func<TIn, TReturn> func,
TReturn elseValue = default(TReturn))
where TIn : class
{
return obj != null ? func(obj) : elseValue;
}
}
[/code]

NullOr means “this object can be null or we’ll run the function”. Access to the member can be expressed via a lambda, so the above code can now be expressed like this:

[code lang=”csharp”]
var name = obj.NullOr(_=>_.Name) ?? "<null>";
var lname = obj.NullOr(_=>_.Name).NullOr(_=>_.ToLower()) ?? "<null>";
[/code]

Not bad. It isn’t as terse as I’d like, but it is fluent and more readable, at least to me. Also, being a lambda, you really can put whatever you like in there. It’s not limited to just member access.

Extra

For fun I threw in an “elseValue” clause that lets you provide a default other than null. I haven’t had cause to use this yet, because I find the ?? more readable. Of these, which do you prefer?

[code lang=”csharp”]
var name = obj.NullOr(_=>_.Name) ?? "<null>";
var name = obj.NullOr(_=>_.Name, "<null>");
[/code]

I think I prefer the ?? version because it expresses “no matter what, if the result of this entire chain is null, use this instead”, whereas the default gets a little lost when it’s part of the final function call. It also is more work to rearrange the clauses, should the order of lookups be changed for whatever reason.

(I also wrote this up at StackOverflow and earned myself a nice Necromancer badge.)

Written by Scott

October 3rd, 2010 at 5:53 pm

Posted in csharp,quickie

Quickie: Fluent Object Casting

without comments

I’m a big fan of fluent interfaces. C# picked this style up in a big way starting with 3.5 and the Enumerable operators class. People are going nuts with it, often for replacing verbose old XML with something a little more readable and compact. Yay. Finally, C# has caught up with languages like Lua or Javascript which have had this ability forever.

Aside from a better configuration language, what I really like about coding fluently is that it’s a steady stream of left-to-right operators, like a pipeline. You think of the next operation and just chain it along, never having to back up. Domain specific sublanguages like LINQ (which I adore, incidentally) can make this even more compact, but I’m talking about the function-based fluent operators.

For enumerables, it’s easy:

[code lang=”csharp”]
var names = list.Where(i => i.Foo > 5).Cast<IBar>().Select(j => j.Name);
[/code]

Take the list, filter it, cast to the type we want, and pull out an element. Operation by operation, left to right. But what about for a single object? That fluency hits a brick wall when you run into C#’s old C-style casting on an individual object.

[code lang=”csharp”]
var name = ((IBar)list.First()).Name;
[/code]

Yuck. Not fluent, and hard to read. As you’re typing, you have to back up, stick the casted type on the front in parens, and, oh yeah, have to also go wrap the whole thing in parens to be able to continue the method chain. I’ve always hated having to do this and in the past would usually pull out the casting into a separate local.

It’s also easy to fix. We just need a little extension method:

[code lang=”csharp”]
public static partial class Extensions
{
[DebuggerStepThrough]
public static T To<T>(this object o)
{ return (T)o; }
}
[/code]

Now it’s nice and fluent:

[code lang=”csharp”]
var name = list.First().To<IBar>().Name;
[/code]

So we now have the system Cast<T> operator for casting elements within an enumerable, and To<T> for casting the overall type. Solid.

Written by Scott

May 17th, 2010 at 8:40 pm

Posted in csharp,quickie