31 May 2012

Math - Scaling a value

Sometimes you have this thing where you have two ranges, and you need to scale a value from the one to the other; e.g.:
Range 1: 0.....x.....100   x=50
Range 2: 0.....y.....500   y=?
Thus:
100 > 500
 50 >   ?
  x >   y

C# code

Thus, I want to know the value of y. I wrote this extension to get it:
internal static double Scale(this double value, double maxValue, double maxScale) {
  if (maxValue == 0 || maxScale == 0)
    return 0;
  else if (maxValue == maxScale)
    return value;
  else
    return value / (maxValue / maxScale);
}

result = 50.Scale(100, 500); //Result will be 250

Try it


if (max. value)equals (max. scale)
then (value)is (result)
...

See also

Interpolating a value

29 May 2012

.NET - get assembly attributes

Referencing assembly attributes

In .NET, when you go to the properties of a project and select Application > Assembly Information, you can fill in some basic information about the application you're making. It would be convenient if you could reference these fields from within your code, to avoid having to duplicate this information. There is a fairly easy way to do this. To get the company:

public static string Company {
  get {
    object[] attr = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
    if (attr == null || attr.Length <= 0)
      return string.Empty;
    else
      return (attr[0] as AssemblyCompanyAttribute).Company;
  }
}

This can now be written shorter. To get the title:

private static string GetTitle() => Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyTitleAttribute>()?.Title;

I put this as static properties in the Program class. That way I can reference these fields anywere I need them.

Using them in XAML

One useful purpose for this is setting the window title of a WPF application. To do this, you need to declare the namespace of the application in XAML. By convention, it's tag is xmlns:local. Then you can simply write:

Title="{x:Static local:App.Title}"

Assuming you have a method that retrieves the AssemblyTitleAttribute in the App class, the window will now display this title. Also note that the method has to be public for this.

24 April 2012

C# - Define an operator on a struct

Sometimes a struct comes in handy, as it is a value type that cannot be NULL. You can't however write things like a == b on them; that is unless you actualy define what == means for that struct. Take the following struct:
public struct IdName {
  public int Id { get; set; }
  public string Name { get; set; }
}
I simply tell it what == means, but watch out for null's. The compiler will also require that you define !=:
public static bool operator ==(IdName a, IdName b) {
  if ((object)a == null && (object)b == null) return true;
  else if ((object)a != null && (object)b != null) return a.Id == b.Id && a.Name == b.Name;
  else return false;
}
public static bool operator !=(IdName a, IdName b) {
  return !(a == b);
}
Compiling that will give you two warnings: you did not override GetHashCode() nor Equals(). These are necessary to get the underlying infrastructure to work well with the new type. So, implementing the latter should be easy:
public override bool Equals(object obj) {
  if (obj != null && obj is IdName)
    return ((IdName)obj).Id == Id && ((IdName)obj).Name == Name;
  else
    return false;
}
And the hashcode thing:
public override int GetHashCode() {
  return base.GetHashCode() ^ Id;
}
And now you can write a == b on structs, just like you can with integers and such.

01 February 2012

C# - == vs. .Equals()

I used to think that strA.Equals(strB) is faster than strA == strB, so I always used it. But that's not entirely accurate.

== is the identity test. It checks wheather two objects are one and the same. This is the faster of the two.
.Equals() asks one object to compare it's value against another and decide whether or not they are equal.

Since value types are kept on the stack, and not on the heap, they can only be compared by value. The ValueType-base class uses reflection to compare each property. Value types, like structs, should therefore override .Equals() and == to speed things up.

One last consideration is that strA.Equals(strB) requires strA to be an instantiated object, whereas strA == strB allows strA to be NULL.

Conclusion: comparing two strings using == is the same as doing it with .Equals().

09 January 2012

JSON - Parse a JSON date to a JS date

When a date is returned via JSON, it's written like this:
/Date(1326063600000)/
To convert this to a JavaScript Date, you parse it as follows:
new Date(parseInt(json.substr(6)))

22 December 2011

JS - Optional function parameters

Sometimes you want to call the same JavaScript function with a different number of parameters, to replicate the overloading we use in, for instance, .NET. This is how to do it:
function setMenu(param1, param2){
  if(param2 === undefined){
    param2 = -1;
  }
  //Code
}
You can call this function in multiple ways:
setMenu(0, 5);
setMenu(0); //Param2 will be set to '-1' by the function
And both will work.

20 December 2011

SL Toolkit chart - clickable legend items

The Silverlight Toolkit chart allows for click-though by setting IsSelectionEnabled to true on the series, and attaching the SelectionChanged event. Now you can click on bars or pie slices, and interact on that.

But what if you want to also make the legend beside the chart clickable. That's not supported out of the box. You do this by changing the Legend Items template.

The template

Define a template like the following for the legend items. I've put this as a resource in the Grid that contains my Chart.
<Grid.Resources>
 <Style x:Key="CategoryLegendItem" TargetType="toolkit:LegendItem">
  <Setter Property="Template">
   <Setter.Value>
    <ControlTemplate TargetType="toolkit:LegendItem">
     <StackPanel Orientation="Horizontal">
      <Rectangle Width="8" Height="8" Fill="{Binding Background}" Stroke="{Binding BorderBrush}" StrokeThickness="1" Margin="0,0,3,0" />
      <HyperlinkButton Content="{TemplateBinding Content}" Click="HyperlinkButton_Click" />
     </StackPanel>
    </ControlTemplate>
   </Setter.Value>
  </Setter>
 </Style>
</Grid.Resources>
Notice how I replaced the default Title element that displays the legend item text by a HyperlinkButton. That way it actually looks like something that can be clicked. This button also triggers the click event.

The code

First, the series LegendItemStyle must be set to the custom template. Since my chart is created in code I've put:
series.LegendItemStyle = (Style)LayoutRoot.Resources["CategoryLegendItem"]
Finally there's the HyperlinkButtons click handler.
private void HyperlinkButton_Click(object sender, RoutedEventArgs e) {
  RowPrestation row = (RowPrestation)((sender as HyperlinkButton)
                      .DataContext as PieDataPoint).DataContext;
  PieSeries series = (PieSeries)(Chart.Child as Chart).Series[0];
  series.SelectedItem = row;
}
Notice that the DataContext of the HyperlinkButton contains the PieDataPoint — in case of a pie chart — that goes with the legend item in question. And if you cast it, that points DataContext contains the bound object that was used as ItemsSource. Mine was a List<RowPrestation>.