Sign up to the jQuery Grid Subscription list.

How to use Collections and Inheritence (with listbox)

I have come up with several difficulties when I want to create a list on a list box that contains objects. (Each element have several data).

Collections and Inheritence solved this problem for me.
Just I create a class that inherit from BaseCollection and add some methods to it, like "add", "remove"... to manage items.

This will make it easier to add and remove items from Listbox, to get the object from the selected element on the list box and also to save the listbox contect on an XML file.
I'm going to explain you more about this project and also to give you some source code and the application file to download.

Our application is very simple, it consits of 
  • Entre a person data (firstname, lastname, age)
  • Show the persons list on a text box
  • Show the selected person data (Complete name, age)
  • Save the the whole data to an XML file
  • Reload the XML file to memory
If you are new with Inheritence and Collections so this is almost impossible for you.
You have a list of classes, how to deal with the list box and also saving and reloading from the XML file.

   1. Designing the user interface

First of all, we need to create a lcean user interface. Simple and easy to use.
  • Buttons (Add, Remove, Show, Save, Load)
  • Text Boxes (firstname, lastname, age)
  • Listbox
Here's an example of what you should get
Now let's divide our project into parts
  • The person class
  • The listbox Collection class
  • Methods (Add, Remove, Show)
  • Saving and loading to an XML File (will be discussed in another post)
  2- Creating the Person Class

The person class consist of "First name", "Lastname" and "age"
We can put those as fields or as properties. As our application is very simple, so we are going to use fields.
Also we need a method called "Complete name" that displat first and last name at once.
We can also use constructors just to make things easier and faster.
The following code can be used for the Person Class. To do, create a new Class file and name it portfolio.
You should get this code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace person_portfolio
{
class portfolio
{
}
}


Now you can delete the portfolio class, I just wanted to make the classes file separated than other files.
The final code should be like this


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace person_portfolio
{
class person
{
// Declare fields
public string firstname;
public string lastname;
public int age;
// constructor
public person(string _firstname, string _lastname, int _age)
{
firstname = _firstname;
lastname = _lastname;
age = _age;
}
// Override and return complete name
public override string ToString()
{
return firstname + " " + lastname;
}
}
}


You may ask why I did override the Tostring() Method. I'll answer that question later.

 3- Creating the Collection class

After creating the person class, we need to create a class that collects those classes (persons). We can do that without creating the Collection Class, but by doing so we can have more possibilities and customize the way the class works.


class plist : System.Collections.CollectionBase
{
public person_portfolio.person this[int index]
{
get
{
return ((person_portfolio.person)List[index]);
}

}
public void Add(person_portfolio.person pers)
{
List.Add(pers);
}
public void Remove(person_portfolio.person pers)
{
List.Remove(pers);
}

}


The collection class is simple. It inherits from the 'CollectionBase' class and contain an 'add' and 'remove' method. And also a method to get the object through its index.

 4- Creating the user interface

As the code is too long, then you'd better download the project source.
The user interface should contain an Add and Remove button. Also a show button to display Person data.

Here's the form code with the method


public partial class Form1 : Form
{
person_portfolio.plist pers_list = new plist();
public Form1()
{
InitializeComponent();
}

private void btn_add_Click(object sender, EventArgs e)
{
person new_person = new person(tb_fn .Text , tb_ln .Text , System.Convert .ToInt32(tb_age .Text.ToString ()));
pers_list.Add(new_person);
refresh_list();

}
private void refresh_list()
{
listbox.Items.Clear();
for (int i = 0; i < pers_list.Count; i++)
{
listbox.Items.Add(pers_list[i]);
}
}

private void btn_show_Click(object sender, EventArgs e)
{
person get_person = (person)listbox.Items [listbox .SelectedIndex ];
MessageBox.Show("First name: " + get_person .firstname .ToString () +Environment .NewLine + "Last name: " + get_person .lastname .ToString () + Environment .NewLine + "Age: " + get_person.age.ToString());
}

private void btn_remove_Click(object sender, EventArgs e)
{
int index = listbox.SelectedIndex;
listbox.Items.RemoveAt(index);
pers_list.RemoveAt(index);
}
}

The application is still missing the 'save' and 'load' functions, which I'm going to explain later.

Feel free to download the project source.
If you found any bug or problem please post a comment.

Readmore »»

How to prepare for Microsoft MCAD Certificate

It's good to work on two things at one time with one effort.

That's better than learning seperatly and re-learning for the certificate.
But before that, what are the advantages of MCAD and does it worth the trouble?

Sure if you visit the Microsoft Website they'll list a lot of advantages. But I consider the most important one that MCAD is considered by many companies world wide and it's used to filter developers when hiring.

Also it's good to show the badge on your website. And MCP have a special website on Microsoft for sharing code and support. Breif, there are many reasons and there are no reason why you should not go for it.

If you can't affrod the courses prices that Microsoft offers or other companies present, so it's time to hit the books, which are available even by buying them or downloading from pirate sites.
But first you need to understand on which things consist the MCAD Certificate.

MCAD = Microsoft Certified Application Developer.
To get the certificate you must be either
  • Windows Developer : In this case you choose Visual C# or Visual Basic
  • Web Developer : Also in this case you choose a language.
The MCAD consists of two core exams and one elective.
And here's what the first exam is about. There are four exam. Two for Windows Developer and two for web one. Each language has an exam. You must choose your path.
I choose Visual C# and those I would recommend "MCAD/MCSD Training Kit—Developing Windows-Based Applications with Microsoft Visual Basic .NET and Microsoft Visual C# .NET"
This book include both languages.

The next exam is about Web services and server components. To know the needed books to complete and pass this exam, visit Microsoft MCAD requirement.
Microsoft will also list the available courses and also self-paced books. I do consider self-paced books important and must-read as they contain the exam objectives and wrotten by Microsoft MVP members.

Once you passed the two core exams, you have one elective. This can show your knowledge on Microsoft SQL, Biztalk, implementing security or if you know both Visual Basic and C# language, you can retake the first exam on the other language, showing your ability on both of them.

What if you passed and took the certificate?

Let's be realist, some people will tell you that you can earn up to $120K a year! This is not true, unless they are living not in earth sure! MCAD won't even grantue you a job, especialy that we are in a recession.

MCAD will just increase the propability of being hired, but you'll get interviewed as sure. MCAD and other Microsoft certificates are valuable when putting them in your CV paper. When hiring, a manager will prefer to interview an MCAD better than a Developer from I don't know where. 

Look at the picture above
MCAD Payrate

The following picture shows the hourly payrate for MCAD certified in the United States.
Yet, the prices are quite low this mean MCAD is not sufficient, so you may from now think on upgrading step by step your status. Here are the other titles
  • Microsoft Certified Application Developer
  • Microsoft Certified Solution Developer
  • Microsoft Certified Technology Specialist
  • Microsoft Certified Architect
The more certificates you get, the more knowledge you have and experience you get.
Here's the pay per hour rate on some United States cities for IT Architect, the MCA or other certificates are not mentionned.

The pay has just doubled or even more! IT (Information Technology) Architect are the most valuable titles!

If you are sure you can, take the MCA directly. If you are not so go step by step and good luck!

Some useful links
If you have other links, don't hesitate to suggest them.

Readmore »»

About Visual Studio Dot Net

Those days, I become more active on Visual Studio, exactly Visual C# dot net. What's behind that? I'm just preparing for the MCAD Certificate, as it appears to me important to have to show my knowledge on Visual Studio products.


This blog will talk about anything related to Visual Studio Dot Net and also Microsoft certifications, as I'm planning to get one.

Privacy:

The code, tutorials, ideas and any other things you may find useful are delivered with no warranty. This means I'm not responsible of damage that may occur or results of using this code.

Readmore »»