Sunday, July 31, 2005

XML Serialization of a record list

XML Serialization only serializes public data, so I wrote a small contact book application that demonstrates serializing collections by creating a couple of records consisting of name and phone number objects and serializing them.
I wouldnt want to define my variables as public, so I defined them as private, but exposed them through public properties, in which we could implement value checks etc. I annotated those properties with the [XmlElement] attribute so that each of those properties when serialized would become an XML Element as you would see shortly. I did similar with the name class. Note that you need to define default constructors.

public class PhoneNumber
{
int _countryCode;
int _cityCode;
int _number;

[XmlElement("CountryCode")]
public int CountryCode
{
set { _countryCode = value; }
get { return _countryCode; }
}

[XmlElement("CityCode")]
public int CityCode
{
set { _cityCode = value; }
get { return _cityCode; }
}

[XmlElement("Number")]
public int Number
{
set { _number = value; }
get { return _number; }
}

public PhoneNumber(int countryCode, int cityCode, int number)
{
_countryCode = countryCode;
_cityCode = cityCode;
_number = number;
}
public PhoneNumber()
{
}
}

public class Name
{
string _firstName;
string _middleName;
string _lastName;

public Name(string firstName, string middleName, string lastName)
{
_firstName = firstName;
_middleName = middleName;
_lastName = lastName;
}
public Name()
{
}

[XmlElement("firstname")]
public string FirstName
{
get { return _firstName; }
set { _firstName = value; }
}

[XmlElement("middlename")]
public string MiddleName
{
get { return _middleName; }
set { _middleName = value; }
}

[XmlElement("lastname")]
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
}


Then, I defined the Record class that aggregates Name and PhoneNumber. I defined private members for name and phone number and exposed them through public properties, annotated with [XmlElement] attributes.

public class Record
{
Name _name;
PhoneNumber _phoneNumber;

[XmlElement("Name")]
public Name name
{
set { _name = value; }
get { return _name; }
}

[XmlElement("PhoneNumber")]
public PhoneNumber phoneNumber
{
set { _phoneNumber = value; }
get { return _phoneNumber; }
}
public Record()
{
}
}


Then, our top level class, which I was too lazy to rename from the VS .NET name Class1, I annotated it with the [XmlRoot] attribute to provide a name for the XML root element. Then, in the accessors for the records, I annotated it with the [XmlElement] attribute so that the name and phone number elements would be nested inside a Record element and Record elements would be inside the root element. Of course, there can only be one XML root element for a document. I also implemented a get accessor indexer so that you could access collection elements through array-like syntax. The rest of the code isnt too difficult to follow.

[XmlRoot("NameList")]
public class Class1
{
ArrayList _nameList;

public Class1()
{
_nameList = new ArrayList();
}

[XmlElement("Record")]
public Record[] Records
{
get
{
Record[] names = new Record[_nameList.Count];
_nameList.CopyTo(names);
return names;
}
set
{
if ( value == null )
return;
Record[] names = (Record[]) value;
_nameList.Clear();
foreach (Record name in value)
_nameList.Add(name);
}
}

public Record this [int index]
{
get
{
return (index < _nameList.Count) ? (Record) _nameList[index] : (Record) null;
}
}

public void Add(Record rec)
{
if (rec != null)
_nameList.Add(rec);
}
///
/// The main entry point for the application.
///

[STAThread]
static void Main(string[] args)
{
Class1 oMain = new Class1();
Name hisName = new Name("Adnan", "Farooq", "Hashmi");
Name anotherName = new Name("Hammad", "", "Rajjoub");
PhoneNumber ph1 = new PhoneNumber(92, 21, 888222);
PhoneNumber ph2 = new PhoneNumber(92, 21, 777333);
Record rec1 = new Record();
rec1.name = hisName;
rec1.phoneNumber = ph1;
Record rec2 = new Record();
rec2.name = anotherName;
rec2.phoneNumber = ph2;
oMain.Add(rec1);
oMain.Add(rec2);
XmlSerializer ser = new XmlSerializer(typeof(Class1));
StreamWriter sw = new StreamWriter("data.xml");
ser.Serialize(sw,oMain);
sw.Close();
}
}


I was a bit miserly with comments on this, which is rare. The reason is that I had a couple of other programs to do today, and I also had to do extra time on campus this morning to finish off some unfinished business.
If you're as lazy as I am, or even beat me, which is difficult, because lazy people are too lazy to beat others, just download the source code. I am including the XML file for good measure. Dont blame me for the stupid application, I am a lousy developer.

29 days to go!

29 days left. The countdown continues!

More than just a man

You traveled the world, now you must journey inwards, to what you fear. There's no turning back. Feeling is nothing, will is everything. If you make yourself more than just a man, if you devote yourself to an ideal, you become something else entirely. Are you ready to begin?

Batman begins

Configuring Cisco router through ASP.NET 2.0

OK, so how do you do interactive programming of the Cisco device using the cool .NET 2.0 System.IO.Ports.SerialPort class? Here's a sample. This sample will work with Visual Studio 2005 Beta 2. I built a website project based on the filesystem (i.e., not on IIS or FTP). The web page gives you a dropdown list to choose from amongst COM1, COM2, and COM3, lets you type the enable password in a text box and the command to run. It then displays the result of the command into a label. What you can do with it is only limited by your imagination. Enjoy again!

Mobile Web App to configure Cisco router/switch

Some of my students were working on this project, so I brought home a Cisco 2950 catalyst switch and built on top of the telnet C# library, a mobile web application project, which I have uploaded to my web site. Just download the zip file, uncompress it. The folder MobileWebApplication1 contains the solution file so you can place it in your Visual Studio Projects folder in your My Documents folder and place the folder MobileWebApplication1 inside it in InetPub\wwwroot\. The Telnet C# library is included with the project, so you dont have to download it separately. Enjoy!
This one is a proof of concept for configuring a Cisco router over an IP network. I'll try to upload something that does the same over a serial port soon. I'll probably use VS 2005 Beta 2 to do that, owing to its cool System.IO.Ports namespace.

Saturday, July 30, 2005

30 days to go

The count down continues. 30 days to go.

For those wanting to write a conference paper

Those of you who are wanting to write a conference, for SCONEST 2005, for instance, and wondering how to go about writing a paper, I have a presentation on my website that you can use. You can download it in PDF format or in Microsoft PowerPoint format.
You can write a paper about something that you have done, a project, your senior project maybe. You can write a paper about something you have studied from various sources. You can write a paper about something that you are going to do but havent started. You can write a paper about something that you have done partially and is not yet complete. Good luck!

My website is back

My domain has now been renewed and my site was moved to a new server. The new server seems good enough. You might be interested to know that my presentations for the PDC 2005 sessions that I gave as well as the source code for the demos are available there. Enjoy!

Thank you for your comments that I cherish

I got so many comments and such great comments to my posting titled "Got it!" that I had a very long reply. So, I decided to write a new post with replies to those comments.
Thanks loads guys and girls. I thank those who have posted there comments and those who havent. You all mean so much to me. It takes a good person to see good in others. It is not me that is good, but you, who see me as good.
Adnan, I understand your feelings, and you understand mine. We make a great team. I have a feeling that some day soon, we'll be working together, and I'll wait for that day anxiously, because it will be source of pride and honor for me.
Faisal, it is an honor and a great source of pride for anyone to know that one has been an inspiration for someone. In fact it is something that people dream of, it is an achievement of a lifetime. It greatly humbles me to know that I have been able to touch someone's life and influence it in such a subtle way. Yes, I hope you'd see me on research journals time and again. Yes, I have a very long way to go. Lots of hard work to be done.
Khurram, it takes a good man to appreciate someone, and I assure you, you are the good man, I am just a guy who's doing his job.
Yahya, I'm glad you feel that way. I wish I could say that I deserved it. I certainly worked hard, but I do strongly feel that the prayers had a lot to do with it.
Nouman, I understand your feelings, but I am certain that I am leaving with several very competent teachers behind. I always want to be different, I want to make a difference, in someone's life, at my workplace. I see several people who will still be here, who want to do the same. I fought a battle, they will continue.
Kanizeh, that is a huge huge thing that you said. I thank you immensely for that. Me, in that league, at this age? That is quite something. I am happy that you and some other people feel that way. My bottom line has always been, I am not doing anything great. I am only trying to execute my job. I feel that as a teacher, it is my responsibility to assist my students in and out of class, with my subject, or on anything else. That is the job duty that I have tried to execute. Quite honestly, and not being humble, I always feel that I have not been able to do that to the extent that I am supposed to. So, I just keep trying my best, without the greed for any rewards. Fortunately, many many rewards have come my way, the greatest of which are the statements like yours and Faisal's that, to me, are greater than the MVP award, greater than a million rupees a month salary, an airconditioned office and a car, greater than anything else. I once again say, it takes a great person to appreciate someone. I am not great, I am not good, it is you who is/are great to see the good in me and not the bad. Thank you all, and know that I am not going anywhere. I am happy to know and say that I will be right here for you, in your hearts and minds, anytime. Just think of me, and I'll be thinking of you. We're inseparable.

Friday, July 29, 2005

Configuring device from serial port in ASP.NET

Well, you could either do that over a DNS name or IP address, for which there are a number of methods as I have already blogged earlier. Otherwise, if you are connected over a serial port, try this, an article by a fellow MVP. Better still, a blog entry by the same guy for working with .NET 2.0.

31 days to go

The count down has begun. 31 days left.

I fall in love over and over

Every time I see her after I wash her and rinse her, I cant take my eyes off her. Sitting there, an object of beauty. I just cant decide whether I love my Nissan more than I used to love my Civic 89. What exactly were you thinking?

This entry rated PG-13

This blog entry is rated PG-13. Dont read it unless you're comfortable with it. I cant help posting on this. Meera just keeps coming on the screen so outrageously in that new Lux advertisement saying "khoobsoortee ka raaz. Janna chahtay hain?" And the way she is in that adv, every single time I see that adv, I fear that she's about to tell the truth. No, no, I dont wanna know the secret of your beauty. I already know it. Since when did we start getting silicone advertisements. What is wrong with our advertising industry?

Another C# Telnet

I found another C# Telnet solution.

I'm a very strange fellow!

Back in 1991, when I was applying for admission to high schools, I had a difficult case because I had been out of country for one year and the board of intermediate education had just recently erected a rule that people who have a gap of one year after their secondary school certificate will not be admitted to colleges on priority. I was determined that I wanted admission at DJ Science College. My percentage was good, but I didnt want to even apply at Adamjee College, which was reputed top in the city at that time. I only applied at DJ Science College. My folks kept asking me to apply elsewhere, but I strongly said, DJ or nothing. In the end, when the merit list was displayed, my name was not there. We were shocked. We went to see the principal and showed him my passport and my marks statement. He was a thorough gentleman and was impressed by my marks statement. He immediately instructed one of the teachers, Mr. Raju Kalip to accompany me to the admission office and have me admitted immediately. Mr. Raju Kalip is another fine gentleman that I can not forget not just because he helped me out, but because he was an excellent teacher. He taught us maths with a zeal and excitement that I cant even come close to attain even when teaching my favorite courses. I heard later that Mr. Raju Kalip had developed diabetes. I hope that he is well, wherever he is.
This time again, for PhD, I only applied at LUMS. There came a moment, when I started thinking, I should've applied to other universities, but alhamdulillah, in the end I not only got admission, but much more. Allah is nice to me. Allah is nice to us all, we just dont notice it.

See, I told you!

See, I told you the wind of change was about to blow! If you dont know what I mean, read by blog entry "Got it!" But the wind of change has only started to blow. There's much more, so just wait and see.

Got it!

I am blogging from a few hundred light years above cloud number nine thousand nine hundred and ninety nine. I received news today of what I had been dreaming of so desperately. I was admitted to LUMS Department of Computer Science and Engineering in the MS/PhD program and have been granted full fee waiver and a monthly stipend. They offer that to the top five candidates. That's quite an achievement. I'm sure you can never understand what or how much it means to me. Just think of it this way. Ever have so much joy that you wanted to cry and couldnt stop crying for a long time?
First position in eighth grade. Highest percentage record at my school in ninth and tenth grade. Second position in my first year at NED University. Assistantship two weeks into my first semester at Wichita State. A freelance software development that gave me so much to learn. Student Activities Committee Chair for IEEE Karachi Section. IEEE Student Branch Counselor at NED University. INETA Pakistan Country Leader. Invited to Dubai twice to attend Microsoft Research .NET 3-day Crash Course, everytime with free air travel, five star hotel stay and meals. Invited to Turkey to attend and speak at INETA MEA Country Leaders' Summit. Nominated Secretary IEEE Karachi Section, which I turned down. Elected Secretary/Treasurer IEEE Communications Society, Karachi Chapter. Endless list of praises and appreciations from so many people and platforms. All for an incompetent and incapable person like me. I can not attribute it to anything but my parents' prayers. And there is so much more that Allah has blessed me with that I cant even begin to encompass. If I would be in prayers for my entire life, I could thank Allah enough for all that I have been given.
Success is a journey and not a destination. This is only the first step to a journey. I have a daunting task ahead of me to live up to the confidence placed in me by the gentlemen at LUMS. My faculty advisor is Dr. Shahid Masud who is a graduate from Queen's University of Belfast. He maintains a home page, too. With teachers like the ones over there, I am ready for action, ready to take on the world.
I pray for strength at this crucial juncture of my life. Pray for me, like you have prayed for me before.

Yea, right!

As I was dialing up to my ISP, I heard tones that indicated a noisy line and an imminent slow connection. When the connection completed, Windows showed me a balloon saying connected at 115.2 kbps. Yea, right! I have a 56kbps modem. Maybe everything goes right together just as everything goes wrong together.

Thursday, July 28, 2005

The wind of change

The wind of change is starting to blow. I have a feeling it is going to be very strong.

Goodness is its own reward

Doing good with the greed of a reward is probably very much like doing evil. Goodness is its own reward. I had this argument with a few faculty members from some so-called universities in Karachi, back in 2003. We were talking about the community activities that I have been involved in for quite some time, and the formidable donations that I had brought to my department at NED University through my networking and efforts. The "faculty" immediately asked me if the university recognizes my efforts. I said, "no, and I dont care." They said, "no, you should care and you should be acknowledged."
I told them and I tell you, I do what is right because that is the thing to do. If you're doing right, that's nothing extraordinary. If you're doing wrong, that should be denounced. It's like if someone returns someone else's lost wallet, the whole thing gets published in the newspapers. Give me a break, he did what he was supposed to do, he did what he should've done. Do you get a reward for working 9 am to 5 pm? No. You get a reward if you work beyond your job description. Well, quite frankly, I've been working beyond the call of duty, but that's because it brings me satisfaction. I dont care about the fact that I earned four to five times less than what my class fellows did.
By our society's standards, I am a nut case. A student of mine, an year or so back, when he noticed how and what I've been doing admitted exactly that. He said, "Sir, you're a nut case." I said, "damned right I am a nut case. And we need as many such nut cases as we can get." If you find any, get me in touch with them.