Thursday, September 27, 2007

String And StringBuilder - A Common Mistake

Introduction


Everyone needs the type String in his daily programming. In addition, most of people do a common mistake to choose between String and StringBuilder.

Wrong Approach



String mySentence = null;
mySentence += "I";
mySentence += " ";
mySentence += "am";
mySentence += " ";
mySentence += "writing";
mySentence += " ";
mySentence += "this";
mySentence += " ";
mySentence += "interesting";
mySentence += " ";
mySentence += "article.";


This is not a good approach to deal with String because String is immutable. Therefore, every time when you concatenate the new value a new instance will be made and it will make a great performance loss if concatenation will be done many times.

Right Approach



System.Text.StringBuilder mySentence = new System.Text.StringBuilder();
mySentence.Append("I");
mySentence.Append(" ");
mySentence.Append("am");
mySentence.Append(" ");
mySentence.Append("writing");
mySentence.Append(" ");
mySentence.Append("this");
mySentence.Append(" ");
mySentence.Append("interesting");
mySentence.Append(" ");
mySentence.Append("article.");

The above code will do the same that the wrong approach did but it will lead to the great Performance boost.

Summary


Both approaches are resulting in same manner. You will not see a big difference in one look. However, remember when your code is serving for many requests the above right approach will definitely give you a performance boost.


So remember when you do not want to concatenate then surely use the String and when you have to deal with multiple concatenations then definitely use StringBuilder.

Properties In User Controls - A Tip And Trick

Introduction


I am writing this article to share with you a special tip and trick to persist the property values in user controls during postbacks.

Wrong Approach



private String myName;

public String MyName
{
get{return myName;}
set{myName = value;}
}


This wrong approach will lose your property values. In addition, whenever you want to get the value of MyName Property it will never get in postbacks and you will always get the Null Exception.

Right Approach



public String MyName
{
get
{
Object o = ViewState["MyName"];
if(o == null)
{
return null;
}
else
{
return (String)o;
}
}
set
{
ViewState["MyName"] = value;
}
}


This right approach persists your property values during postbacks.

Summary


This concludes that whenever you want to use properties in your controls, it is the best approach to use ViewState with your properties. Which will always persist you Property Values.

Random Rows In Respect To Their Priorities - Sql Query

Introduction


If you have the table rows, priority wise, and you want to generate the rows randomly with a condition that the rows with highest priority shows more times as compared to the rows with lowest priority. You will learn this task after reading this article.

Scenario


For instance, we want to make a Banner and need to select it randomly from a huge list in respect to their priorities and date.

Banner Table



GO

CREATE TABLE [dbo].[Banner]
(
[Id] [int] IDENTITY(1,1) NOT NULL CONSTRAINT PkBanner_Id PRIMARY KEY,
[Title] [varchar](52) NOT NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[Priority] [smallint] NOT NULL,
)


Sample Entries



GO

INSERT INTO
Banner
(Title, StartDate, EndDate, Priority)
VALUES
('Sample Banner 1','6/12/2007','11/12/2007',5)

GO

INSERT INTO
Banner
(Title, StartDate, EndDate, Priority)
VALUES
('Sample Banner 2','6/12/2007','6/21/2007',4)

GO

INSERT INTO
Banner
(Title, StartDate, EndDate, Priority)
VALUES
('Sample Banner 3','4/12/2007','11/12/2008',3)

GO

INSERT INTO
Banner
(Title, StartDate, EndDate, Priority)
VALUES
('Sample Banner 4','6/11/2007','11/12/2007',2)

GO

INSERT INTO
Banner
(Title, StartDate, EndDate, Priority)
VALUES
('Sample Banner 5','6/11/2007','11/12/2007',1)


Assuming that priority 5 is highest and 1 is lowest.


Please feel free to change the queries in respect to their date and priority.

Solution



SELECT TOP 1
Id,
Title,
StartDate,
EndDate,
Priority
FROM
Banner
WHERE
GetDate() BETWEEN StartDate AND EndDate
ORDER BY
RAND(CAST(CAST(NEWID() AS BINARY(4)) AS int))*Priority DESC

The above query generates the rows randomly every time when it executes in respect to their priorities and date.


It means that the banner with the highest priority shows more than the lowest priority. In addition, the current date should between StartDate And EndDate.

Summary


After reading this article you learn a query which shows the rows randomly in respect to their priorities.