Rajnish Noonia

Category: Tips

Tips and tricks

  • Capitalization of string

    To convert string to title case (capitalize initial letters of the string), there is not string method to call. However, we can use the CultureInfo from Globalization and use the TextInfo’s ToTitleCase method.
    Here is how it can be done in C#:

    Add class StringExtensions to your root namespace and copy the code,All string variables in your project will now shows ToTitleCase method (extension method)

    
    public static class StringExtensions
    {
        public static string ToTitleCase(this string input)
        {
            System.Globalization.CultureInfo cultureInfo =
            System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Globalization.TextInfo textInfo = cultureInfo.TextInfo;
            return textInfo.ToTitleCase(input.ToLower());
        }
    }
    
    
  • Call WCF RIA Methods

    Code snippet to call WCF RIA Methods

    private void LoadExplorer(string path)
    {
        Web.Services.ExplorerContext context = new Web.Services.ExplorerContext();
        EntityQuery query = context.EnumerateDirectoryQuery(path);
        Action<LoadOperation> completeProcessing = delegate(LoadOperation loadOp)
        {
            if (!loadOp.HasError)
            {
                // Perform actions required after request complete
                foreach (DirectoryInfoItem dirInfo in loadOp.Entities)
                {
    
                }
            }
            else
            {
                LogAndNotify(loadOp.Error);
                loadOp.MarkErrorAsHandled();
            }
        };
        LoadOperation loadOperation = context.Load(query, completeProcessing, null);
    }
    
    private void LogAndNotify(Exception error)
    {
        ErrorWindow.CreateNew(error);
    }
    
  • Reading csv file – LINQ

    Let say you have csv file with two columns “Name” and “EmpID” and you want to use the values in your web based application.One option is to load the file into SQL server via some back end job and eliminate the use of file.But this will be only required (or suggested) if file is very huge.

    I was having a web based application and my csv file holds some mapping information say Name and EmpId,Now i have silvelight page which calls web server methods via RIA and need to find EmpID from provided name.Below is the implementation tips with c#

    //Read CSV file using LINQ

    public class Employee
    {
    	public string Name {get;set;}
    	public string EmpID {get;set;}
    }
    
    public class CSVReader{
    	public static List ReadAllEmployees(){
    		string fileName = HttpContext.Current.Server.MapPath("~/App_Data/Employee_Mapping.csv");
    		List employees;
    		employees = (from line in File.ReadAllLines(fileName, Encoding.Default)
                                let parts = line.Split(",".ToCharArray())
                                select new Employee() {Name=parts[0], EmpID=parts[1] }).ToList();
    		return employees;
    	}
    }

    Since the file will be used very frequently so you must hold the contents in the memory,you could add the list employee (Application_Start) into application state to achive this.

  • Button Click Event

    Here is small code to trigger button’s Click event using automation peer from source code :

    if (button is Button)
    {
    ButtonAutomationPeer peer = new ButtonAutomationPeer((Button)button);

    IInvokeProvider ip = (IInvokeProvider)peer;
    ip.Invoke();
    }

    Trick will be used in upcoming article “Smart shortcut keys” for adding accessibility to silverlight applications.