Monday, 25 June 2012

C#: Serialization

IFormatter Interface is pretty handy to serialization! You can use it with BinaryFormatter Class. This is a very simple example of object serialization.
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iformatter.aspx
 http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter.aspx
---------------------------------------------------------------------------------------------------- 
<Car.cs>
using System.Linq;
using System.Text;

namespace Model
{
    [Serializable]
    public class Car
    {
        public int Price { get; set; }
        public string Name { get; set; }
    }
}
----------------------------------------------------------------------------------------------------
<Program.cs>
using Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace Serialization
{
    class Program
    {
        static void Main(string[] args)
        {
            SerializeCar();
            DeserializeCar();
        }
     
        private static void SerializeCar()
        {
            //Instantiate a new Car
            Car car = new Car { Name = "BMW", Price = 70000 };
            //Instantiate a new BinaryFormatter
            IFormatter formatter = new BinaryFormatter();
            //Create a new FileStream
            Stream stream = new FileStream("car.obj", FileMode.Create, FileAccess.Write, FileShare.None);
            //Use the formatter to serialize the Car object using the stream
            formatter.Serialize(stream, car);
            //close the stream
            stream.Close();
        }

        private static void DeserializeCar()
        {
            //Create a new FileStream to read the file
            Stream stream = new FileStream("car.obj", FileMode.Open, FileAccess.Read, FileShare.Read);
            IFormatter formatter = new BinaryFormatter();
            //Use the formatter to deserialize the Car object from file
            //Note: The car needs to be cast back to type Car
            Car car = (Car)formatter.Deserialize(stream);
            Console.WriteLine("{0} is deserialized", car.Name);
        }
    }
}
----------------------------------------------------------------------------------------------------

Friday, 15 June 2012

JavaScript onmouseover

When you want to display 3 or more images one after another, you should use Javascript onmouseover.


http://www.w3schools.com/jsref/event_onmouseover.asp
-------------------------------------------------------------------------------------------------
 <HTML>
 <img id="image" src="images/1.gif" onmouseover="rotate_images()">
-------------------------------------------------------------------------------------------------
<test.js>
var counter=1;
function rotate_images()
{
        if(counter >= 3){
        counter = 0;
        }
        var images = new Array("1.gif","2.gif", "3.gif");
        document.getElementById("image").src  = "images/" + images[counter];       
        counter++;
}
-------------------------------------------------------------------------------------------------

Sunday, 10 June 2012

C# LINQ Grouping Search using "Key"

LINQ is quite useful because you can write SQL in C#. It may be confusing at first, thought.
Key is used with group clause. The following websites can be helpful.

http://msdn.microsoft.com/en-au/library/bb384063.aspx#Australia%20%28English%29
http://msdn.microsoft.com/en-us/library/vstudio/bb397927.aspx
--------------------------------------------------------------------------------------------------
namespace LINQ_programm
{
    class Program
    {
        static void Main(string[] args)
        {
            Method_1();
         }
           static void  Method_1()
        {
            Car[] cars = Planet.Cars;
            var group_query = from c in cars group c by c.Price into g select new { Price = g.Key, Cars = g };

            foreach (var g in group_query)
            {
                Console.WriteLine("\n---Car priced{0:c}", g.Price);
                foreach (var c in g.Cars)
                {
                    Console.WriteLine("\nMake:{0}\nModel:{1}\nYear:{2}\nPrice:{3:c}", c.Make, c.Model, c.Date_Made.Year, c.Price);
                }
            }
        }
}

Thursday, 7 June 2012

Design Pattern: Singleton

This must be fantastic!!!The concept is that it restricts the instantiation to a certain number of objects. Singleton can make a software more secure and more organized.
http://en.wikipedia.org/wiki/Singleton_pattern

Monday, 4 June 2012

The workplace and Vivid

How should the workplace be like? Spacious? Clean? Luxurious?
I don't care where I work, I really care who I work with. I just want to work with people who are encouraging and organized. And I also should be like that, I hope I am...
 
I went to Vivid festival, it was fantastic! I'm sure I'll go next year again!!!
http://www.vividsydney.com/