27 March 2011

Compatibility between Public fields and properties

If your class wants to expose some data, you have two possibilities:
  1. Use a public field
    public class Person { public string Name; }
  2. Use a public property that is backed by a private field
    public class Person { private string name; public string Name { get { return name; } set { name = value; } } }
The general advise is that you should not have any public fields, thus you should always use properties. However some people suggest that you can start out with a public field and that you can easily change it into a property when the need arises.
But this is not true for two reasons:
  1. A public field and a property are binary incompatible. When you change your public field into a property you will have to recompile all the clients of your class.
  2. A public field and a propery are also incompatible at source-code level. A public field can be passed as a ref- or out-parameter while a property cannot. So when you change your public field into a property, you may have to actually change the client code.
So my advise: unless you have *very* strict performance considerations always use properties.
Regarding performance, keep in mind that a simple propery that is not marked as virtual (or that is part of a sealed class) will likely be inlined by the compiler so there shouldn't be any performance impact at all (in release-builds).

20 March 2011

Validation Control

Validation Server ControlDescription
CompareValidatorCompares the value of one input control to the value of another input control or to a fixed value
CustomValidatorAllows you to write a method to handle the validation of the value entered
RangeValidatorChecks that the user enters a value that falls between two values
RegularExpressionValidatorEnsures that the value of an input control matches a specified pattern
RequiredFieldValidatorMakes an input control a required field
ValidationSummaryDisplays a report of all validation errors occurred in a Web page

08 March 2011

Introduction to SEQUENCE - Sql Server 2011

SQL Server 2011 will contain one of the very interesting feature called SEQUENCE that allows us to define a single point of repository where SQL Server will maintain in memory counter.

First Create a sequence First as
CREATE SEQUENCE [BlogSequence]AS [int]
Once the sequence is defined, it can be fetched using following method.
-- First Run
SELECT NEXT VALUE FOR BlogSequence, b.BlogTitleFROM SukeshMarlaBlogs b
-- Second Run
SELECT NEXT VALUE FOR BlogSequence, b.BlogTitleFROM SukeshMarlaSqlBlogs b
-->Every single time new incremental value is provided, irrespective of sessions.
-->Sequence will generate values till the max value specified. Once the max value is reached, query will stop and will return error message.

Msg 11728, Level 16, State 1, Line 2
To Overcome this We can restart the sequence from any particular value and it will work fine.
-- Restart the Sequence
Sequence Restarted
SELECT NEXT VALUE FOR BlogSequence, b.BlogTitleFROM SukeshMarlaSqlBlogs b
Final clean up.
-- Clean Up
DROP SEQUENCE [BlogSequence]

02 March 2011

Pascal, Camel and Hungarian Notations

Pascal Notation- In this naming convention all starting letter of the words are in Upper Case and other characters are lower case.
Example: SupplierCode
Camel Notation- In this naming convention first character of all words, except the first word are Upper Case and other characters are lower case.
Example: supplierCode
Hungarian notation - In this naming convention the variable name starts with group of small letter which indicate data type.
Example: bLoop ( b indicates its a boolean type),iSum ( i indicated its a integer data type).

Things are upgraded

My Dear readers, I am really thankful for being supportive all these years. This site was the first blog site I ever created in my life...