Tip : Code Structure C#
Feb03

Tip : Code Structure C#

Welcome to CodeSpread! As a developer, I always stress on developing a habit of following the best practices to structure the code. It helps me to easily navigate through the code and also saves a lot of effort in searching for some particular items. Below is the sample structure and example: using statements Constants / Read-only fields Private Members Enumerations Public members Protected members Constructors Properties Public methods Private methods //using statements Using System Using System.Web namespace Controllers { public class SampleController {   //Constants / Read-only fields public const int numofitems=10; //Private Members private string _samplestring = string.Empty; //Enumerations public enum months { January = 1, February, March, April, May, June, July, August, September, October, November, December };   //Public members public  int StartDate =string.Empty;   //Protected members protected int EndDate ==string.Empty; //Constructors public SampleController () { }   //Properties public string Date { get; set; } //Public methods public string GetStartDate() { } //Private methods private int numOfItems() { }     Related articles across the web C# Coding Standards CLS Compliance?...

Read More
20 Dec: Must Read Codes [6-10]
Dec20

20 Dec: Must Read Codes [6-10]

We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn’t require a full length descriptive article so combining them into groups of five. CodeReads #6, What is the difference between Dispose and Finalize? #Point 1, Dispose is used to release unmanaged resources in a deterministic fashion, like we know when to release the resource. Finalize method is also used for the purpose of releasing resources but in non-deterministic fashion like we cannot be sure as when Garbage Collector will collect the resource. #Point 2 , Finalize method has a performance drawback due to the Garbage Collector as there will be two iterations of Garbage Collector and override operation. fbsource :https://www.facebook.com/codespread/posts/550605628295996 CodeReads #7, Garbage Collector maintains managed objects in “generations.” The generation indicates the lifetime of an object like objects having short life are stored in lower generations as GC runs much more frequently to clean up objects in the lower generations. The more the objects survive a GC cleanup, the more the chances that these objects will get moved to higher generations. There will be few GC swipes on higher generations and also that will happen only if lower generations have no space left. When we talk about generations, we talk about physical memory locations divided,managed and allocated to each generation number. fbsource: https://www.facebook.com/codespread/posts/550949951594897 CodeReads #8, .data() : Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements. For more information: http://api.jquery.com/data/ fbsource: https://www.facebook.com/codespread/posts/370621346386549 CodeReads #9, Elements of a UML Class Diagram are: Class Operations Relationships -Associations -Generalization -Dependency -Realization Constraint Rules and Notes fbsource: https://www.facebook.com/codespread/posts/552364564786769 CodeReads #10, Scaffolding is a technique in which the programmer may write a specification that describes how the application database may be used. With the help of this concept,programmer can create a functional Web site for viewing and editing data based on the schema of the data. ASP.NET Dynamic Data supports scaffolding. Refer the below link. For more information: http://msdn.microsoft.com/en-IN/library/ee845452%28v=vs.100%29.aspx fbsource:...

Read More
19 Dec: Must Read Codes [1-5]
Dec19

19 Dec: Must Read Codes [1-5]

We are sharing few must read codes/concepts which are required now and then. Each of these codereads doesn’t require a full length descriptive article so combining them into groups of five. CodeReads #1, Usage of ‘this’ keyword in C#. To qualify members hidden by similar name. To have an object pass itself as a parameter to other methods. To have an object return itself from a method. To declare indexers. To declare extension methods. To pass parameters between constructors. To internally reassign value type (struct) value. fbsource: https://www.facebook.com/codespread/posts/544740368882522 CodeReads #2, ISNULL and COALESCE though equivalent, can behave differently. An expression involving ISNULL with non-null parameters is considered to be NOT NULL, while expressions involving COALESCE with non-null parameters is considered to be NULL. fbsource: https://www.facebook.com/codespread/posts/545045568852002 CodeReads #3, Regular Cast: string str= (string) strToCast; Method Call: string str= strToCast.ToString(); Safe Cast: string str= strToCast as string; fbsource: https://www.facebook.com/codespread/posts/545045568852002 CodeReads #4, Control State maintains the behavior of the controls whereas View State maintains the content of the controls. Also, Control State is available even though View State for the control is disabled. fbsource: https://www.facebook.com/codespread/posts/547846855238540 CodeReads #5, In javascript, Body.Onload() event occurs only after the DOM has been loaded, plus all associated resources like images, flash etc are also completely loaded. $(document).ready() occurs when the DOM has been loaded but associated resources like images, flash etc may be pending. There can be multiple document.ready() in a page but Body.Onload() event will be only one. fbsource:...

Read More
Consider Structs
Dec17

Consider Structs

While taking birth in programming world, we all had our own experiences of learning; begin working on a new language or OOPS concepts or experimenting with a new tool or compilers. One of such experiences is already available here. Let’s share one more personal experience and an embarrassing one. J I was just caught up with c# and begin traversing the beautiful journey of programming. I was very overwhelmed with my OOPS knowledge and coding skills. I was making applications, enjoying cups of coffees and playing ping-pong with my team. One day, for a requirement, I was asked to create a program and I created it using lot of meaningless lines of codes. Then during review, I came to know that it could have been done by structs. It was quite embarrassing that I could not recollect structs, just because we do not use it in our daily programming. It was my early phase so was not crucified. Happy go lucky!! I realized that my instant reaction to almost any requirement was class, oops and objects as though it is limited water in the pond. But programming is an ocean. So just to prove that I know structs, I will share structs syntax and knowledge here. What is Structs? A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. [msdn] struct Example { public int x, y; public Example (int x, int y) { this.x = x; this.y = y; } } Few points about Structs: Structs are value types. Struct values are stored on the stack. Inheritance is not supported. Structs can implement interfaces. Structs can improve memory allocations in case of lightweight objects. Structs cannot have a default (parameterless) constructor. Let’s see an example to see structs’ value type nature. For the same struct declared above, we will use it in the below program. Class StructExample { public void AssignStruct(Example instance) { instance.x = 1; instance.y = 2; }   public static void Main() { Example example = new Example (); example.x = 3; example.y = 4; AssignStruct(example); Console.WriteLine(example.x); Console.WriteLine(example.y); } }   The result will be 3 4 Here, struct “Example” being a value type creates a copy and was passed to the AssignStruct method. So value of the struct field remains same in the output and will not have effect of AssignStruct Method. Few lines of code and lightweight object is what the requirement might be, so always consider structs before jumping to class based...

Read More
RegisterStartupScript, RegisterOnSubmitStatement
Dec14

RegisterStartupScript, RegisterOnSubmitStatement

We have discussed RegisterClientScriptBlock, RegisterClientScriptInclude in our previous article here. There are two more methods to client script dynamically from server side. We will discuss these methods: RegisterStartupScript, RegisterOnSubmitStatement. RegisterStartupScript: This method adds a script block into the page at the end of page i.e. just before the form closing tag. It has an advantage over other methods where if the script requires any elements to be available on the page then as the script is already loaded/rendered, the elements can be easily referenced and script will not throw any error. Basic steps will remain same as previous methods. Steps: Create the script as a string. Pass it to one of the overloaded methods. There are two overloaded methods: RegisterStartupScript(Type type,string key,string script) RegisterStartupScript(Type type,string key,string script, bool addScriptTags) Before adding a script with RegisterStartupScript, we can actually check if it is already been added somewhere else in our code, IsStartupScriptRegistered . This method will check if script with a given key and type pair is already registered on the page. ClientScriptManager csManager = Page.ClientScript; Type vartype = this.GetType(); if (!csManager.IsStartupScriptRegistered (vartype ,"buttonClick")) StringBuilder messagetxt = new StringBuilder(); messagetxt.Append(("<script type="text/javascript">alert('Hello World);</script> "); csManager. RegisterStartupScript (vartype, “buttonClick” , messagetxt.ToString() ); RegisterOnSubmitStatement: This method adds script that executes in response to the page’s onsubmit event. It provides an opportunity of validation before accepting the user’s submission. RegisterOnSubmitStatement(Type type,string key,string script) Basic steps will remain same as above. Before adding a script with RegisterOnSubmitStatement,we can actually check if it is already been added somewhere else in our code, IsOnSubmitStatementRegistered.This method will check if script with a given key and type pair is already registered on the page. ClientScriptManager csManager = Page.ClientScript; Type vartype = this.GetType(); if (!csManager . IsOnSubmitStatementRegistered (vartype ,"buttonClick")) StringBuilder messagetxt = new StringBuilder(); messagetxt.Append("<script type="text/javascript">alert('Hello World);</script> "); csManager.RegisterOnSubmitStatement(vartype, “buttonClick” , messagetxt.ToString() ); “These methods can add an excellent flavor towards the client model for a better UI experience”...

Read More
Page 1 of 8123456...Last »