August 30, 2005

w.blooger makes blogin easy

i just installed w.blooger tool. Im using it now for my future posts. its really a nice manage our posts. if anybody intrested download it from the below URL. its totally free..;)
download w.Blogger

August 26, 2005

Cut down your development time.

Wanna cut down your development time?? Use macros. Iit’s really cool. I’m using it through out my coding /development process. While we do developing we used to copy n paste some of the code snippets. For example adding comments to your code or assigning getters setters to a class etc..etc.. but with macros you can do those things by using shortcut keys.. let me show you how.. You’ll love macros once you get to know it. I will explain step by step for you to create a simple macro for adding comments to your code.

You can record a macro or you can code it using visual basic.

To record a macro press Ctrl+Shift+R & then type what you want to use repeatedly(for recording). Then stop recording by pressing the Ctrl+Shift+R. To save the macro open the macro explorer by pressing Alt+F8. And rename the TemporaryMacro. If you want to edit the macro right click on the macro then click edit. You can later assign to a shortcut key by simply going to Tools > Customize > Keyboards. Its that easy ;)

Here is a sample macro which I’m using to add comments to my code. You can save it as a macro & start using it. Remember you can do lots of thing using macros not just adding code snippts ;). if you need more sample macros just send me a mail to Ludmal at gmail.com. happy coding..

'Sample code for addComments macro

Sub MyComments()
DTE.ActiveDocument.Selection.Text = "//by " + System.Environment.UserName + " " + Now()

End Sub

Project Life cycle


check this out.. typical software project cycle...

August 16, 2005

Managed Code Performance

Today I read a very good article from a guy who works for Microsoft BCL Performance section. I found some good interesting concepts. If your interested about code performance you should read this article.

http://blogs.msdn.com/ricom/

I wrote a very simple performance test for testing the for vs foreach & string vs stringbuilder ..etc..etc.. you can find the code below.

Timer class: This calculates the code execution time.

///

/// To get the elapsed the time

///

public class MyTimer

{

long _start;

long _end ;

long _exeTime ;

public string ElapsedTime

{

get

{

//returns the Elapsed time

_exeTime = _end - _start;

return _exeTime.ToString();

}

}

///

/// Start the timer

///

public void Start()

{

_start = DateTime.Now.Ticks ;

}

///

/// End the timer

///

public void Stop()

{

_end= DateTime.Now.Ticks ;

}

}


Some of my test samples


static void Main(string[] args)

{

MyTimer m = new MyTimer();

ArrayList a = new ArrayList();

//For Tests

m.Start();

for(int i=0; i<1000;>

{

string sss= "as" + i.ToString();

Console.WriteLine(sss);

a.Add(sss);

}

m.Stop();

Console.WriteLine(m.ElapsedTime);


m.Start();

for(int i=0; i

{

Console.WriteLine(a[i].ToString());

}

m.Stop();

Console.WriteLine(m.ElapsedTime);

//Foreach Tests

m.Start();

foreach(string s in a)

{

Console.WriteLine(s);

}

m.Stop();

Console.WriteLine(m.ElapsedTime);

//String BuilderTest

StringBuilder name = new StringBuilder();

m.Start();

for(int i=0;i<100000;>

{

name.Append(i.ToString());

}

m.Stop();

Console.WriteLine(name.ToString());

Console.WriteLine(m.ElapsedTime);

}

August 12, 2005

Proper user of Interfaces in C#

We as software developers must always ...always remember to program to Interfaces NOT to Implementations. I'll tell you why. Everybody who's doing developing should know about software changes. We have to spent time on enhancements rather than the initial implementation. So what we require is a good design. IT IS A MUST, believe me. I had plenty of nightmares programming to bad designs. Check out the below UML design.






What we’re trying to do is to write our database method to the interface. Not to the concrete class.

Here is the complete code.

//interface to the DataHelper
public interface IDataHelper
{
void Connect();
void Execute();
}

//SqlDataHelper - using SQL database
public class SqlHelper :IDataHelper
{
public void Connect()
{
//SqlDataHelper Implementation
Console.WriteLine("Connectiing sql..");
}

public void Execute()
{
//SqlDataHelper Implementation
Console.WriteLine("executing Sql..");
}}

//OracleHelper - using Oracle database
public class OracleHelper : IDataHelper
{
public void Connect()
{
//SqlDataHelper Implementation
Console.WriteLine("Connectiing oracle..");
}

public void Execute()
{
//SqlDataHelper Implementation
Console.WriteLine("executing oracle..");
}}

public class UserDataBase
{
public UserDataBase()
{}

//method implementing to the IDataHelper interface
public static void CheckDB(IDataHelper help)
{
help.Connect();
help.Execute();
}

public static void Main(string[] args)
{
//Use 1
//Oracle as the Database
OracleHelper oraleHelper = new OracleHelper();
CheckDB(oraleHelper);

//Use 2
//Sql as the Database
SqlHelper sqlHelper = new SqlHelper();
CheckDB(sqlHelper);
}}

Later if you want to use MySql as the database simply write a class MySqlHelper & inherit from the IDataHelper class. You don’t have to change anything in the CheckDb() method. Its that simple. So try to program to interfaces whenever possible. Happy coding dudes..

Facade Design Pattern

Design Patterns are recurring solutions for the common software design problems. Knowing design patterns will dramatically improve the way you design your software. These days till late night I’m reading Head First Design Patterns book. Trust me its really good. I think all of the developers out there should have one. ;) So I would like to share some brief explanation to some of the design patters who really took my breath away. I will explain each & every design pattern in my design pattern series. So stay tune. More to come. Today I will explain you the Façade pattern. Its really simple.

What is Façade Pattern?

It’s an interface for an entire subsystem. It’s a high level layer that makes the subsystem easier to use.

Best way to learn is by a sample. So here is the problem. Imagine after hard day work you go home & prepare your self to watch a movie. What are the things that you have to do before you sit down infront of the TV. You have to on the TV, on DVD player, start AC, dim the lights….etc.. What if you can just sit down & do all the things with a remote control. Nice huh.. that’s what exactly façade pattern do.. so the remote control is the façade. You can get the clear picture by a simeple UML diagram & the code below. Comments are highly appreciated. Stay tune for the next design pattern….

HomeTheater facade.

public class HomeTheaterFacade
{

//Has all the objects

DVDPlayer _dvdPlayer;
Light _light;
TV _tv;
AC _ac;

public HomeTheaterFacade()
{
this._ac = new AC();
this._dvdPlayer = new DVDPlayer();
this._light = new Light();
this._tv = new TV();
}

//To watch the movie simply pass the movie
public void WatchMovie(Movie movie)
{
_light.Dim();
_tv.On();
_ac.Start();
_dvdPlayer.Play(movie);
}
}

see how the client use the HometheaterFacade to watch the movie

HomeTheaterFacade e = new HomeTheaterFacade();

Movie movie = new Movie("Matrix Reloaded");
e.WatchMovie(movie);

there more patterns that will imporove your OO designs. so stay tune more to come..

August 11, 2005

XML Serialization

I was just seeking for a topic to post in my blog today. I couldn’t find much. So I thought to tell you something about XMLSerialization. Most of the developers out there already know whats going on under the hood. But for the developers who’s seeking some new stuff gohead.. .net really rocks!!.

How to serialize a object to a XML file. code below.

Simply create a User class file.

[Serializable]
public class User
{
public User()
{
}

string _name;
string _age;

public string Name{get{ return _name;}set{_name = value;}}
public string Age{get{ return _age;}set{_age = value;}}

}


To serialize the user object into a xml file add the code below

public void SerializeUser()
{
//creates user object
User user = new User();
user.Name = "Ludmal";
user.Age = "25";

//this will create a xml file in the given location
TextWriter writer = new StreamWriter("C:\\user.xml");

//simply use the Serialize method from the XMLSerializer class
//
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(User));
serializer.Serialize(writer,user);
}


You can open the xml file & change the values easily.


-
Ludmal
34


To deserialize user object from the xml file simply add the below method

public User DeserializeUser()
{
//Read from the XML file
TextReader reader = new StreamReader("C:\\user.xml");

//Creates a instance of the xmlserializer class by passing the User class
XmlSerializer serializer = new XmlSerializer(typeof(User));

//Creats the user from the XmlSerializer by deserializing
User user = (User)serializer.Deserialize(r);

return user;
}

Its that simple. Try your self. We can easily save settings as XML files. Remember to add the Serializable attribute to all the classes that you want to serialize.

August 10, 2005

Viewstate & object serialization

Recently i came across a problem which i cant put a object into viewstate. So i did some research on that & found, if we want to put a object into viewstate we have to make the class serializable by adding serializable attribute. Then you can put your object into the view state. code below.

[Serializable]
public class User
{
public User()
{
//
// TODO: Add constructor logic here
//
}

string _name;
string _age;
string _address;

public string Name{get{ return _name;}set{_name = value;}}
public string Age{get{ return _age;}set{_age = value;}}
public string Address{get{ return _address;}set{_address = value;}}

}

//In the web form page

private void Page_Load(object sender, System.EventArgs e)
{
user = new User();
user.Name = "Ludmal";
user.Age = "25";

}

private void Button2_Click(object sender, System.EventArgs e)
{

ViewState["User"] = user;
}

private void Button1_Click(object sender, System.EventArgs e)
{
user = (User)ViewState["User"];
Response.Write("My Name is " + user.Name);
}