Friday, November 19, 2010

Math.Round rounds numbers, but in which way?!!

I was sure I knew how it rounds fractional parts of number and then I got weird results in some import batch.

So, let's look at the example.
  
Math.Round(1.5) //goes to 2, right?
Math.Round(2.5) //goes to 3, right? Wrong!

Go back to method definition on MSDN and read about method definition and return value once more - here.
  
public static decimal Round(
decimal d
)

"The integer nearest parameter d. If the fractional component of d is halfway between two integers, one of which is even and the other odd, then the even number is returned."

Okay, let's clarify rule.
  
Math.Round(1.5) //goes to 2
Math.Round(2.5) //goes to 2 !!

Very strange rule if you ask me.

Have a nice day.

Wednesday, September 29, 2010

State management in Windows Phone 7


There are 2 types of states you want to save on windows phone 7. One is when user leaves your application and another one is when your application is tombstoned (deactivated by launching some other application from shell, like phone is ringing, phone is locked and so on).

In a first case persistent data is saved into Isolated storage. Using of isolated storage is covered deeply in a number of places on net. So, in this case I put as a reference following link.

Small hint: all the data you save to isolated storage must be serializable.

Another hint - how to check is entity seriazable or not -
  
private static bool IsSerializable(T obj)
{
return ((obj is ISerializable) ||
(Attribute.IsDefined(typeof (T), typeof (SerializableAttribute))));
}


In a case another app forces your application closing deactivation event occurs and your handler on this event in app.xaml is executed. Keep in mind following - you have only 10 seconds to save your transient data. If you don't save them in 10 seconds data is discarded and exception raises.

Hint: In this case data is saved into IDictionary<string,object> property called State on PhoneApplicationPage class. Keep in mind following - "The state is only accessible during or after the OnNavigatedTo(NavigationEventArgs) method is called. Also during or before the OnNavigatedFrom(NavigationEventArgs) method is called. If you implement it too early or too late it will throw an exception. You should not use this property for excessive storage as there is a limit of 2MB for each page and 4MB for the entire application."

Cheers

Thursday, September 9, 2010

Accelerometer readings from WP7


Recently I've found interesting image which explains how axes values are represented over sensor on windows phone 7 device. Once when you register event handler for ReadingChanged event you'll get values for x,y and z axes over AccelerometerReadingEventArgs argument.

Argument contains property for x, y and z points. The values are expressed in G-forces (1G = 9.81 m/s2), so a value of 1.0 means that the corresponding axis is being pulled with the same force as the gravity in Paris. For example, if Z is -1.0, then the device is lying flat, face up, on a perfectly flat surface. If Z is 1.0, then the device is lying flat, faced down.

Keep in mind that acceleration reading change event occurs very often (50 times per second) and that you deal with a lot of data.

Wednesday, April 28, 2010

Silverlight Masterclass UK


by DanM
27. April 2010 19:00

The Silverlight Tour comes to the UK – and it’s called the Masterclass!

This 3 day hands-on training with both designer and developer tracks looks awesome and (uniquely) has two expert trainers per course.

Currently scheduled in London, Manchester, and the Midlands for June, all courses also come with the chance to win an xbox 360, and Silverlight Spy licences!

Early bird discount of £100 if you book in May, and if you are a member of #SLUGUK or #nxtgenug there are additional discounts to be had.

Full Details are here: http://silverlightmasterclass.net



Cheers

Friday, April 9, 2010

Huge entities list in batches?

Work with huge list of entities in batches? It has been never easier.

Common programming issue –you have list of 10000 integers or entities (bad programming practice!) in generic list and you need to process them in batches of, let’s say, 250 items per batch.

In the past I solved this problem with following algorithm:

- Sort items per some criteria (in most case sort on identifier)

- Take first chunk of data

- Save last processed id into local variable

- While batch size is more than zero

- Process batch

- Take new chunk of data where identifiers are greater than last processed one

- Set new last processed id

Here is listed code snippet:
//sort list
adIds.Sort((number1, number2) => number1.CompareTo(number2));

int lastProcessedId = 0;

List<> tempList = (adIds.Where(item => item > lastProcessedId)).Take(250).ToList();

while (tempList.Count() > 0)
{
markerAds = AdHandler.GetByIDList(tempList); //items processing (in this case grabbing data from db)

lastProcessedId = tempList[tempList.Count - 1];

tempList = (adIds.Where(item => item < lastProcessedId)).Take(250).ToList();

It looks like few simple lines of code. By power of linq, anonymous types and group by method in c# 3.0 it can be done even simpler and in less lines of code.
var groups = adIds.Select((id, index) => new {GroupID = index/250, ID = id}).GroupBy(x => x.GroupID);

foreach (var group in groups)
{
markerAds = AdHandler.GetByIDList(group.Select(x=>x.ID).ToList());
}

Variable “groups” contains chunks of 250 items per group. Faster, simpler and better.

Cheers

Friday, March 26, 2010

Few more usefull Windows Phone 7 links

Programming Guide for Windows Phone

Windows Phone 7 Series UI Design & Interaction Guide

Videos of MIX10 Windows Phone sessions

Getting Started With Windows Phone 7 Series Development

Tip & information:


protected override void OnManipulationStarted(ManipulationStartedEventArgs args)
{
Color clr = Colors.White;
if (args.OriginalSource == txtblk)

....

Original Source tells you in which element finger manipulation has been started.

"Currently, no production devices have been released. As I understand, the devices will start hitting the market Q4 2010 before Christmas" by Chris Bennett.