Pages

Wednesday, March 31, 2010

?? Operator (C# Reference)

The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

Remarks


A nullable type can contain a value, or it can be undefined. The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. If you try to assign a nullable value type to a non-nullable value type without using the ?? operator, you will generate a compile-time error. If you use a cast, and the nullable value type is currently undefined, an InvalidOperationException exception will be thrown.

For more information, see Nullable Types (C# Programming Guide).

The result of a ?? operator is not considered to be a constant even if both its arguments are constants.

class NullCoalesce
{
static int? GetNullableInt()
{
return null;
}

static string GetStringValue()
{
return null;
}

static void Main()
{
// ?? operator example.
int? x = null;

// y = x, unless x is null, in which case y = -1.
int y = x ?? -1;

// Assign i to return value of method, unless
// return value is null, in which case assign
// default value of int to i.
int i = GetNullableInt() ?? default(int);

string s = GetStringValue();
// ?? also works with reference types.
// Display contents of s, unless s is null,
// in which case display "Unspecified".
Console.WriteLine(s ?? "Unspecified");
}
}

Tuesday, February 23, 2010

Silverlight Value Converters

 

From :- http://timheuer.com/blog/archive/2008/07/30/format-data-in-silverlight-databinding-valueconverter.aspx

 

Here i just want to update only the code portion of it

<ListBox x:Name="FeedList">
ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock FontFamily="Arial" FontWeight="Bold" Foreground="Red" Text="{Binding Title.Text}" />
<TextBlock FontFamily="Arial" Text="{Binding PublishDate}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>


 



Assume this XAML binds to SyndicationFeed.Items data coming from an RSS feed.  If we simply bind it the result would be this:





 public class Formatter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}





 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (parameter != null)
{
string formatterString = parameter.ToString();

if (!string.IsNullOrEmpty(formatterString))
{
return string.Format(culture, formatterString, value);
}
}

return value.ToString();
}


xmlns:converter="clr-namespace:TypeConverter_CS"


Finally the code look like this



 



1: <UserControl x:Class="TypeConverter_CS.Page"


   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 


   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"


   4:     xmlns:converter="clr-namespace:TypeConverter_CS"


   5:     Width="400" Height="300">


   6:     <UserControl.Resources>


   7:         <

converter:Formatter x:Key="FormatConverter" />


   8:     </UserControl.Resources>


   9:     <Grid x:Name="LayoutRoot" Background="White">


  10:         <ListBox x:Name="FeedList">


  11:             <ListBox.ItemTemplate>


  12:                 <DataTemplate>


  13:                     <StackPanel Orientation="Vertical">


  14:                         <TextBlock FontFamily="Arial" FontWeight="Bold" Foreground="Red" Text="{Binding Title.Text}" />


  15:                         <TextBlock FontFamily="Arial" Text="{Binding PublishDate, Converter={StaticResource FormatConverter}, ConverterParameter=\{0:M\}}" />


  16:                     </StackPanel>


  17:                 </DataTemplate>


  18:             </ListBox.ItemTemplate>


  19:         </ListBox>


  20:     </Grid>


  21: </UserControl>


Output