Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
is it possbile to create a table in html using string in c#??
C#
 public void Paging(ref StringBuilder strHtml, int pageIndex, int pagingCount, int rowCount, string listView, string controller, int position)
        {

            try
            {
                strHtml.Append("<table class='Dsply' border='0' cellspacing='0' 
}
cellpadding='0'><tr><th>");


Instead of the string builder if i use a string will i be able to create one table?
Posted
Comments
Sergey Alexandrovich Kryukov 13-Jun-12 0:21am    
What's wrong with StringBuilder?
--SA

1 solution

You can do it using either a string or a StringBuilder, but a string is a lot less efficient because each time you add new data to it, it creates a new string and returns that - the old string is untouched (because strings are immutable - the cannot be changed once they are created). StringBuilders are different - they can be changed, until they run out of space, at which point they are replaced with a bigger one. That is why it is considered a bad idea to use a string for this kind of operation.

If you do use a string, then you need a different syntax:
C#
strHtml = strHtml + "new string";

or
C#
strHtml += "new string";

The string class does not have an Append method.

If you use a StringBuilder, you do not need the ref parameter - you do if you want to "change" the string.

Personally, I would ditch the ref parameter, use a StringBuilder and return it as the result of the method anyway:
C#
public StringBuilder Paging(StringBuilder strHtml, int pageIndex, int pagingCount, int rowCount, string listView, string controller, int position)
   {
   strHtml.Append("<table class="Dsply" border="0" cellspacing="0" cellpadding="0"><tr><th>");
   return strHtml;
   }</th></tr></table>
 
Share this answer
 
Comments
Arjun Menon U.K 13-Jun-12 4:34am    
Thanks Original Grifff .. I knew that strings dont have append function but i wasn't aware about the efficiency factor... Cheers bro
OriginalGriff 13-Jun-12 4:39am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900