Murtaza Fazal's Blog

Murtaza Fazal's Blog
Murtaza Fazal's Blog
Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, June 15, 2011

Alarm in Windows Phone 7.1 (Mango)

How often we want to create an application which could generate Alarm in the background and play it on the specified time? Once me and my brother were investigating this same task on iPhone and what we found was... the audio won't play for more then 40 seconds . Implementing the same thing on Windows Phone 7.1 (Mango) is piece of cake :) Just few lines of code and you are done.

I'll demonstrate how to write a simple program which plays any audio which you want :)


  1. Create a new Project
  2. Select Target Framework as 7.1
  3. In you Main.xaml.cs , include the library  "Microsoft.Phone.Scheduler".
  4. I've placed the audio file on the root , if you want you can create a folder as audio and place your file there. Then you should navigate as per your need. 
  5. Now write the following code 
    • Alarm myAlarm = new Alarm("mmfAlarmTest");   
      • // You need to specify a name for the alarm
    • myAlarm.BeginTime = DateTime.Now.AddMinutes(1);  
      • // I would like it to play after 1 minute of execution
    • myAlarm .Sound = new Uri("/toss-the-feathers.mp3",UriKind.RelativeOrAbsolute); 
      • // Remember my audio file is on the root directory?
    • ScheduledActionService.Remove("mmfAlarmTest");  
      • // Remove if there exist a similar alarm
    • ScheduledActionService.Add(myAlarm);   
      • // Register your alarm


And that's it !!! . Test it out and enjoy the fun ;) Using this, you can generate an application which sets alarm through your application

Friday, January 15, 2010

Becoming a Great Programmer

You might think that great programmers can type at a thousand words a second, have a mega-sized brain, and are fitted with a socket that lets them connect directly to a computer. This is not true. Especially the socket bit. In my experience, the best programmers are the ones who are the most fun to be with. The ones who you enjoy talking to. The ones who don’t get upset when you find a mistake in their programs and who will sometimes agree that your solution is better than the one that they had invented. I’d much rather work with someone like that than someone who can write a hundred lines of code a minute but who refuses to speak to me if I dare to suggest that one of those lines might be wrong. Great programmers take care to find out that what they are doing is the right thing. If they are working for a customer, they will make sure that the customer gets what the customer wants. They will not assume that they know the best way to do it and just do it their way. They will make sure that what they produce is tested and comes with helpful documentation. They will work in the team, make coffee when it is their turn, and do whatever it takes to make sure that the project has a happy ending. Of course, they might also fill your office with beach balls, superglue your keyboard to the desk, or cover your chair with aluminum foil, but these are all done with a friendly spirit.

I have secured the services of a great programmer who will be adding Programmer’s Points to our text. These are truly words of wisdom, so make sure to take note when you see them.

--
Rob Miles
In his book : Microsoft XNA Game Studio 2.0 Learn Programming NOW

Tuesday, September 1, 2009

Nullable Type in Visual Studio 2005+

Many of us learn programming through books or in some programming course. They course starts with simple data types and ends with a good looking application but one thing most of the books or instructors forget that the first and farmost requirement of a good looking application is to have good backend programming as well. I’ve being working on C# since a long time but I never came across this Nullable type in any of the books for beginners or Professionals and to me it seemed as one of the basic knowledge that was missing.

What does Nullable do? So far when we write a small piece of code, like for instance we are taking bio-data from the user in a console based application and if he presses enter without typing anything i.e. entered nothing in that field, what would happen? And what should have happened? The user pressed enter which means it should be a null field whereas a character 13 (ASCII CODE) is saved in its place. Is that the correct thing? Definitely not, and to overcome this state most of the books teaches us to use conditions that if its equal to 13 then ignore it or do something.

In Framework 2.0 and onwards, Microsoft has provided us with Nullable type through which we can allow null values in our simple datatypes. Nullable types represent value-type variables that can be assigned the value of null. You cannot create a nullable type based on a reference type. (Reference types already support the null value.)

Nullable < T >   variable;

Here T is a value type. The two forms are interchangeable. As in our example, we can use following code:

Nullable < string >  name = null;

Or what if we want to have a 3 state Boolean variable? True, False and Null? Thanks to Nullable type we can implement this within seconds by declaring our boolean variable under nullable type

Nullable < bool >  flag;

Microsoft added this feature but what’s the use of it?  Why to add another state even if we could do it through some conditions? With the use of Nullable, we can have the normal range of values of the underlying value type with additional of null value. Two fundamental members of the Nullable structure are “HasValue” and “Value” properties. If the HasValue property for a Nullable(T) object is true, the value of the object can be accessed with the Value property. If the HasValue property is false, the value of the object is undefined and an attempt to access the Value property throws an InvalidOperationException. The Nullable class supports obtaining the underlying type of a nullable type, and comparison and equality operations on pairs of nullable types whose underlying value type does not support generic comparison and equality operations.