Posts

Showing posts from April, 2014

Generic Code with Default Keyword

  One of issues we face when we work with generic classes and methods is How to assign default value for T How to return Null in a Generic method This is because T could be a reference type or a numeric value or struct. because T=null is not valid for numeric and structs even it is valid for reference type. Default keyword is the solution for it. It will return the default value of T. public static T Get<T>( string key)          {              DataCache defaultCache = GetDataCache();              T value = default (T);              var cacheValue = defaultCache.Get(key);                if (cacheValue != null )              {                  try                  {                      value = (T) defaultCache.Get(key);                  }                    catch                  {                      value = default (T);                  }              }              return value;            } Happy Coding!!!!

Intelligence not working in vs 2013

Image
  I have faced the issue where my VS 2013’s intelligence was not working. By applying the following setting it working for me.   1. TOOLS->Import and Export Settings Wizard->Reset all settings->select “No, just reset settings, overwriting my current settings”->Choose a Default Collection of settings 2. Restart Visual Studio if that didn’t work try the solution here .

Affine Cipher in C#

Image
  The affine cipher is a type of monoalphabetic substitution cipher , wherein each letter in an alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function, and converted back to a letter. The formula used means that each letter encrypts to one other letter, and back again, meaning the cipher is essentially a standard substitution cipher with a rule governing which letter goes to which. As such, it has the weaknesses of all substitution ciphers. Each letter is enciphered with the function , where is the magnitude of the shift. For more information on affine cipher visit here Following code is written in c# which has the affine cipher algorithm   public static string AffineEncrypt( string plainText, int a, int b) {      string cipherText = "" ;        // Put Plain Text (all capitals) into Character Array      char [] chars = plainText.ToUpper().ToCharArray();        // Compute e(x) = (ax + b)(mod m) ...

Loading Processing Window in Asp.net

Image
  Some functionalities in our systems takes some seconds to execute. In such case if you want to gray out the page and load a pop up window and close it when server side processing is done following code might help you. aspx Code < html xmlns ="http://www.w3.org/1999/xhtml"> < head runat ="server">      < title ></ title >      < link href ="StyleSheet1.css" rel ="stylesheet" type ="text/css" />        < script type ="text/javascript" src ="jquery-1.8.2.min.js"></ script >      < script type ="text/javascript">          function eventInProgress() {                $( '#block' ).fadeIn();                $( '#container' ).fadeIn();          }            function CloseInprogressWindow() {              $( '#block' ).fadeOut();              $( '#container' ).fadeOut();       ...

PadLeft & PadRight in C#

Image
  PadLeft and PadRight are two string methods available in c# to align a string value. Let’s explain this with examples. PadLeft It returns a new string that right-aligns the characters in this instance by padding them on the left with a specified Unicode character, for a specified total length. string text = "John" ;                Console .WriteLine(text.PadLeft(10, '_' ));              Console .WriteLine(text.PadLeft(10, ' ' ));              Console .WriteLine(text.PadLeft(10, '0' ));              Console .WriteLine(text.PadLeft(10, '*' ));                Console .ReadLine(); Out put PadRight It returns a new string that Left-aligns the characters in this instance by padding them on the right with a specified Unicode character, for a specified total length. string text = "John" ;                Console .WriteLine(text.PadRight(10, '_' ));              Console .WriteLi...

Get Controls by ID

  I have recently come across a requirement in one of the webform asp.net project where we wanted to get the controls by ID rather accessing the control object it self. Following is the solution we came up with. I wrote a generic method which will search and return the control.   private T GetControl<T>( string controlName, int tabIndex , Control parentControl)               where T : Control            {              string name = controlName + "_TabPanel" + tabIndex.ToString();              Control control = parentControl == null ? FindControl(name) : parentControl.FindControl(name);                  return (T)control;          } Following is the code where we use the method. GetControl< TextBox >( "tbNewForecast" , selectedTabIndex, ForecastTab.ActiveTab).Text = "" ;   Hope this helps you. Happy Coding !!!!