Monday, November 11, 2013

C# event – Understanding c# Event, Event Handler and event Arguments – part 2


In this post I will explain how you can declare an Event, event Handler and Event Arguments class in c#.
 
Event Arguments –
As per the example of Cricket application we have discussed in part 1 we need a class who will explain more information about the event BallInGround so that object like Fielder, Umpire and Fan can respond to it. This we will achieve by defining Event Arguments class. .NET framework provides a standard class for declaring event arguments and it is called as EventArgs. This class has no members. This class is used for passing the event arguments to event handler method. So out declaration will be as follows –

public class BallEventArgs : EventArgs 

Event –
Next we need to declare event in a class Ball. This can be declared with c# keyword “Event”. This keyword is used to inform other objects who are interested to subscribe to it. Event declaration is as follows –

public event EventHandler BallInGround; 

Events are usually public so that other interested objects can subscribe to them. In our case other objects will be Fielder, Umpire and Fan. In above event declaration we have used EventHandler keyword. For now understand it as a keyword which used to describe the event handler method signature for this event. This is defined in .NET. When you write EventHandler after an event declaration means you state that the event handler methods will have 2 parameters, an object named sender and an EventArgs class reference variable e and void return type. [EventHandler keyword here is actually a delegate but for now to keep focus on Event understand it as a keyword provided in .NET or understand it as a event of type EventHandler]. 

Event Handler –
Each object of Fielder, Umpire and Fan will subscribe to BallInGround event. Event handler methods you have been using for a long time. When you add a button on UI and double click on it a method get’s added in code behind which is nothing but an event handler method. So event handler method for BallInGround will also be similar to button click event handler method. You might have seen following type of declaration many times –  

Sunday, November 10, 2013

C# event – Understanding c# events – part 1


Event – a noun – described as “something that happens, especially one of importance”.
Example – The formula 1 race was an amazing event to see. 

C# events are no different from description stated above. The only difference being “Something happens on - Objects”. Any c# language feature can be well understood with real life example only. C# being OOP based language; let’s consider a real life example.
 
Let’s consider a popular game of Cricket to understand C# events.
 
Suppose you are developing a wonderful game of cricket for cricket lovers. Assume that this game is going to shake the world of gaming and you will be earning billions with the popularity of this game. At first to develop basic version you need Ball, Fielder, and Umpire and Fan (the person who is crazy about cricket) objects. Here are the responsibilities of every object –
 
Ball – Lets fielder know that I am in the field and catch me.

Fielder – fields or catches the ball when ball is in the ground.

Umpire – Watches the activity when Ball is in the ground and responds accordingly.

Fan – Watches the activities happening on the Ball and yells, cheers up, celebrates and many more!!!

Now everything is in place now you just need to connect everything together. So following diagram illustrates what we wish to do – 
However there is problem because Ball does not know which fielder out of 11 present on the ground will caught or grab him. Similarly Fielder only needs to know about fielding or catching a ball. Hence Ball should only get hit and should not know about the fielder; whereas fielder should only know about catching the ball.

In short – We want every object to know about it and not others. Hence we wish to separate the concerns of each object.

When the ball is hit we will use an EVENT to let fielders know about it. So those, our fielders can respond to the event of ball. Similarly we can make other objects like Umpire, Fan to know about Ball event so that they can also respond to it. So we want Ball object to raise an event. Then we will have other objects subscribed to that type of Ball event. When event is raised the subscribed objects will be notified and they can take the action the way they want. This action to be taken when an event is raised, written in Event Handler. The event handler code is executed every time the event is raised. It is just a method in the subscriber object which runs every time event is raised.

Thursday, October 17, 2013

Gmail Tips – How to avoid sending mail without attachment


You are about to send an important mail. In the body of email you describe to view the attachment to the recipient and accidently you send the mail without attaching required files. This embarrassing moment I have experienced numerous times. Thank god I use Gmail!!
Gmail has come up with excellent feature. Say for example you are drafting a mail in which you will be attaching a file or document in it. So as soon as you start writing mail; keep any of the following wordings typed in advanced – “see attached” or “see attachment”.
In such case even if you accidently click send without file attachment, based on these 2 wording Gmail automatically searches the mail for attachment files and if it does not find any, it will show a pop up message as shown below –
 

This way you get saved from embarrassment!! Cool feature. I really love these Gmail features which are making the life of users so easy and error free.
Cheers…
Happy Gmailing!!!

Thursday, September 5, 2013

Geo Tagging – What is geo tagging and risks involved in geotag photos


Internet, facebbok and twitter kind of social sites from smartphones is very common today. Photos clicked on smartphones are immediately shared on social media like facebook. The potential of sending the information to any corner of world is unbeatable and impressive. However, one needs to understand the risks involved in such spontaneous sharing of photos and information from smart phones. The feature is “Geo Tagging”. This is very useful feature of smartphone but at the same time equally dangerous. 
What is Geo Tagging?
It is the process by which geographical location information is added to various media. Location of earth at which smartphone photo is clicked is attached with photo because of geo tagging feature. So when these photos are published on internet, this location of earth information is also gets published. This information includes latitude, longitude, height, distance, name of place and many things related to location at which photo is taken. Hence people on earth now automatically know where do you stay, where are you right now and may be they can know if you are alone or not in home. So it becomes very easy for a person who wants to track or follow you. Especially Criminals may track information using geo tagging to identify next target for robbery. Action news in America few years back had conducted a program to improve overall literacy in people about geo tagging risks. Video is here - http://www.youtube.com/watch?v=N2vARzvWxwY
Avoiding the risks of geotagging is a thing to keep in mind in this day and age of decreasing privacy.
So what exactly you need to do?