Dotnet Blog
Thursday, December 23, 2010
Casting using is and as operators in c#
Using Is Operator
Is operator can be used to check whether an object is of a particular type. Is
operator returns boolean value.
if(obj is string)
{
}
Here in the above example, if the obj is of type string then the is operator returns true else false. And inside the if condition the casting can be performed.
if(obj is string)
{
string str = (string)obj;
}
Below is a complete example code which explains the working of is operator.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Examples
{
class EndUser
{
static void Main(string[] args)
{
Developer d = new Developer();
Tester t = new Tester();
SeniorDeveloper sd = new SeniorDeveloper();
CastObject(d);
CastObject(t);
CastObject(sd);
Console.ReadLine();
}
static void CastObject(object obj)
{
Developer dev;
Tester testr;
// Check the type of object using is operator and then cast
if (obj is Developer)
{
dev = (Developer)obj;
Console.WriteLine("This is a Developer Object");
}
else if(obj is Tester)
{
testr = (Tester)obj;
Console.WriteLine("This is a Tester Object");
}
}
}
class Developer
{
}
class Tester
{
}
class SeniorDeveloper : Developer
{
}
}
Using As operator
As operator does the work of verifying the type and casting together at a time
compared to is operator.
string str = obj as string.
if(str!=null)
{}
Here in the example an object obj is casted to string. If the obj is of different type and cannot be casted then as operator returns null. So just verify whether str is null or not to confirm whether the casting is successful or not.
Below is a complete example code which explains the working of as operator.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CastingUsingAs
{
class Program
{
static void Main(string[] args)
{
object[] objects = new object[4];
objects[0] = new Developer();
objects[1] = new Tester();
objects[2] = new SeniorDeveloper();
objects[3] = "Hello";
CastObject(objects);
Console.ReadLine();
}
static void CastObject(object[] objects)
{
foreach (object obj in objects)
{
Developer d = obj as Developer;
if (d != null)
{
Console.WriteLine("Is a developer object");
}
else
{
Console.WriteLine("Is not a developer object");
}
}
}
}
class Developer
{
}
class Tester
{
}
class SeniorDeveloper : Developer
{
}
}
Conclusion:
Is operator can be used in places where we need to check only if an object is of any particular type. And for casting purpose As operator could be chosen over is operator since it does the work of checking the type and casting together.
Thursday, April 22, 2010
Addin for Outlook 2010
This outlook addin is a simple addin which will add a Button in the outlook and will display a Hello World message. This Addin is for Outlook 2010. The main problem which I found in Outlook 2010 was that, the Addins are coming in a separate Tab called AddTab. So every time the user wants to use the Addin, he need to navigate to this Tab and then click the Addin button.
So I have made this Addin to appear in the default MailTab which is the home tab. Have made use of ribbon xml to display the Addin button.
Sunday, August 9, 2009
Javascript calendar control with time
I have created one popup calendar control with time using javascript. If anyone is interested then download it from this link.
http://www.box.net/shared/2ax3e74leb
Thanks,
Anil.Friday, August 7, 2009
Detect browser language using c#
string browserLanguage = Request.UserLanguages[0];
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(browserLanguage);
Label1.Text = Thread.CurrentThread.CurrentCulture.DisplayName;
Call a master page Method() from content page using c#
In the Master page code behind file define the method you want to call as Public.
public partial class MasterPage : System.Web.UI.MasterPage
{
public void ShowLicenseErrorImage()
{
this.IMG_LicenseExceedWarn.Visible =
this.lblLicenseMisuse.Visible =
this.lblDetails.Visible = true;
}
public void HideLicenseErrorImage()
{
this.IMG_LicenseExceedWarn.Visible =
this.lblLicenseMisuse.Visible =
this.lblDetails.Visible = false;
}
}
Now in the content page code befind type the following.
MasterPage master = (MasterPage)this.Master;
master.ShowLicenseErrorImage();
Where “MasterPage” is the class name of class in master page.
Thats it!!!
Multi language resource file generation
When i was working for Globalization process in my project, I need to move all strings to resource files. And create different resource file for different languages. This was not a easy task. I had around 35 resource files which i was supposed to translate into 6 different languages. So instead doing this manually i found one much easier task, Using Google Translate API. You can call to google api method and translate the strings. So i created one small windows app which will help to translate resource files.

Please download this project and try using it. Upgrade or enhance it according to your requirement if necessary. Please leave your valuable comments.
You can download the exe from this link: http://www.box.net/shared/n1dqrxrpzd
You can also download the project from this link and modify according to your requirement.
Try this link- http://www.box.net/shared/zjo7mtmzvi