We are now familiar with the definition of encapsulation but
we yet don’t know how to implement it in our code to prevent any kind of Misuse and Bugs in our code.
Let me give an example. I think everyone knows what Facebook is. In Facebook,
when we create an account we provide different kind of information, so that
another Facebook user can know a little bit about us before connecting with us.
When a user successfully creates an account, Facebook community hides your
phone number (one of additional information given at account creation) by
default. This is for your security purpose only. If we go to the account contact
section, we can easily set it back to public so that the phone number is
visible when someone searches us. So why
don’t we build a new program with this scenario in our mind. Let’s start.
- Open Visual Studio. (I’m using visual studio 2012. But other versions will be fine enough).
- Open File -> New -> Project.
- Select Windows from installed templates and select Windows Form Application.
- Give a name to your project and press Ok.
- IDE will create a form for you.
- From the toolbox on the left drag and drop controls to create the UI given below.
Fig: The main UI
So, we have created a registration form where a user can
give his name, email and phone number and click Submit button to enter his/her info
in the ListView Control. We want our new user’s phone number to be private by
default. So we create a CheckBox control with its checked property set to true.
Now,
- Select Project from menu bar and select add class or press (alt+shift+c ).
- Add a new class named User.
- Write the following code in the class.
class User
{
public string Name;
public string Email;
public string Mobile;
public bool HideMobileNo = true;
public void SetMobileNoAsPublic(bool isPublic)
{
HideMobileNo = isPublic;
}
public string GetMobileNo()
{
if (HideMobileNo == true)
{
return "N/A";
}
else
{
return Mobile;
}
}
}
}
What we are really doing here is creating four public field
variable (i.e. Name, Email, Mobile and HideMobileNo). HideMobileNo is a Boolean
variable which initial value is true. Cause we want our phone number to be
private by default. Right!
Again we have two public methods. The SetMobileNoAsPublic
method sets the HideMobileNo variable’s value to the method’s parameter value
isPublic. And the GetMobileNo method simply checks the HideMobileNo variable
value and returns N/A (not available) if HideMobileNo value is true or returns the
phone number stored in the Mobile variable.
Now let’s go to our main Form and write the following codes
public partial class Form1 : Form
{
private User user;
private ListViewItem item;
public Form1()
{
InitializeComponent();
user = new User();
user.Name = "Fiyaz";
user.Email = "fiyazhasan@yahoo.com";
user.Mobile = "0171-XXXXXXX";
//Tweak
the listview design
userListView.View = View.Details;
userListView.LabelEdit = true;
userListView.AllowColumnReorder = true;
userListView.FullRowSelect = true;
userListView.GridLines = true;
userListView.Sorting = SortOrder.Ascending;
//Creating
a listview item
item = new ListViewItem(user.Name);
item.SubItems.Add(user.Email);
item.SubItems.Add(user.GetMobileNo());
//Adding
the listview columns
userListView.Columns.Add("Name", -2, HorizontalAlignment.Left);
userListView.Columns.Add("Email", -2, HorizontalAlignment.Left);
userListView.Columns.Add("Phone", -2, HorizontalAlignment.Left);
//Add
the items to the ListView.
userListView.Items.AddRange(new ListViewItem[] {
item });
}
private void ClearTextBoxes()
{
nameTextBox.Text = "";
emailTextBox.Text = "";
mobileTextBox.Text = "";
}
private void submitButton_Click(object sender, EventArgs e)
{
//Create
a new user instance with new values
user = new User();
user.Name = nameTextBox.Text;
user.Email = emailTextBox.Text;
user.Mobile = mobileTextBox.Text;
//Create
a listview item
item = new ListViewItem(user.Name);
item.SubItems.Add(user.Email);
item.SubItems.Add(user.GetMobileNo());
//Add
the newly created item
userListView.Items.AddRange(new ListViewItem[] {
item });
//Clear
the text from the textbox
ClearTextBoxes();
}
private void setPrivateCheckBox_CheckedChanged(object sender, EventArgs e)
{
user.SetMobileNoAsPublic(setPrivateCheckBox.Checked);
}
private void cancelButton_Click(object sender, EventArgs e)
{
ClearTextBoxes();
}
}
At the top we are creating two objects. One is of our User
class and the second one is a ListViewItem object of ListViewItem class (built-in
class in .net framework). In the constructor of Form1 class, what we are doing
is
- Instantiating our user object.
- Now we are setting default values for our newly instantiated user objects.
- Next we are tweaking some deign for our ListView controls. Nothings special.
- Next as like before we are instantiating item object.
- Then we are adding user.Email as first sub item.
- Again we are setting the next sub item to the returned value of user.GetMobileNo().
- Recall that we already set a true value for our HideMobileNo variable.
- So the return value will be of course “N/A”.
- Then we set three column Titles for our Listview control
- Then add our ListViewItem item to our ListView control with defined designs.
- When we debug the program. We get the following result.
Fig: The first run
So we ended up with a ListView control with a default value
at runtime.
Next we are doing the same thing as we have done before in
the constructor. But instead we took values from relevant TextBoxes for our
appropriate user object fields. Now if we press debug we will be able to type
values in the TextBoxes and add the new item in the ListView by click on submit
button. Like this
Fig: Entering values in TextBoxes
Fig: Newly added item in ListView
The last thing we have
done is in our setPrivateCheckBox_CheckedChanged event we call our user.SetMobileNoAsPublic method
with the user selected state of our CheckBox as parameter (i.e. user.SetMobileNoAsPublic(setPrivateCheckBox.Checked);).
So we have a great working
application, Right!!! I’ll prove you wrong but in the next post. So stay tuned.
J
No comments:
Post a Comment