August 30, 2005
w.blooger makes blogin easy
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.
'Sample code for addComments macro
Sub MyComments()
DTE.ActiveDocument.Selection.Text = "//by " + System.Environment.UserName + " " + Now()
End Sub
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
{
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#
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.
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);
}}
Facade Design Pattern
It’s an interface for an entire subsystem. It’s a high level layer that makes the subsystem easier to use.
{
Light _light;
TV _tv;
AC _ac;
public HomeTheaterFacade()
{
this._ac = new AC();
this._dvdPlayer = new DVDPlayer();
this._light = new Light();
this._tv = new TV();
}
public void WatchMovie(Movie movie)
{
_light.Dim();
_tv.On();
_ac.Start();
_dvdPlayer.Play(movie);
}
}
Movie movie = new Movie("Matrix Reloaded");
e.WatchMovie(movie);
August 11, 2005
XML Serialization
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.
-
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
[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);
}