October 22nd, 2009

Illuminati

Things that cheer Andy up #76112

I just found this piece on the latest version of the BCL (base class libraries) that will arrive in .Net 4.

And I'm delighted to see that they're simplifying a couple of things that have been a bit of a pain so far.  My favourite of which is converting strings to enums, which used to require:
try 
{
    string value = Console.ReadLine();
    FileOptions fo = (FileOptions)Enum.Parse(typeof(FileOptions), value);
    // Success
}
catch 
{
    // Error
}
but is now
string value = Console.ReadLine();
FileOptions fo;
if (Enum.TryParse(value, out fo)) {
    // Success
}
(and that TryParse is clearly using inferred typing - in the same pattern that I tend to use in my code.)
Illuminati

Syntax I'd like to use.

Given a particular case I need to make four contols invisible.

I can just say:
control1.visible = false;
control2.visible = false;
label1.visible = false;
label2.visible = false;
but it seems awfully longwinded.

You can shorten it a bit to:
control1.Visible = control2.Visible = label1.Visible = label2.Visible = false;
but that's pretty awkward to read, and has redundancy too.

I think that C# lambda functionality would allow me to end up with code something like:
{label1,label2, control1,control2}.Set(Control c => c.visible=false;)
but that also seems pretty clunky (I can't check that syntax at work, we're using VS 2005, which doesn't support lambdas).

Is there a language that would let you do this?:
{label1,label2, control1,control2}.Visible = false;