Willem Odendaal

the coder's point of view

<July 2008>
SuMoTuWeThFrSa
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789


Navigation

I Read

Subscriptions

Post Categories



Tuesday, January 04, 2005 - Posts

Determine size of object in ViewState

On numerous occasions I've wanted to know how 'big' objects would be in ViewState. For example - imagine you wanted to compare an array of strings to a hashtable or a dataTable. Which would be the most efficient to store in ViewState?

I know that ViewState is optimized for a few primitive types - int, string, bool... even Hashtable and ArrayList. But other than that I've always had to copy/paste the actual __VIEWSTATE value from my Html source and count the characters. Pretty primitive.

Then I stumbled accross a post by Victor Garcia entitled “Don't let the BinaryFormatter get at it!“. It's a great post. Basically objects are serialized to ViewState using the LosFormatter. This formatter is optimized for the above mentioned primitive types.

Now, the topic of this post. How to determine size of object in ViewState? Simply use the LosFormatter in a method like this... And call it with the object you wish to evaluate.

private int GetViewStateSize(object obj)
{
     LosFormatter los =
new
LosFormatter();
     StringWriter sw =
new
StringWriter();
     los.Serialize (sw, obj);

     int size = sw.GetStringBuilder().ToString().Length;
    
return size;
}

Thanks, Victor. Be sure to read his post for more info on ViewState and the LosFormatter.

posted Tuesday, January 04, 2005 5:04 AM by willemo

Zombie Working

My thought for the day...

Working when you are tired is a real waste of time. I've experienced this time and time again. On Monday you struggle for hours to solve a problem with no success. You code like I imagine a zombie would. Uuuuggghhh....

Then on Tuesday, well rested, it takes you less than half an hour to sort out your problems.

I think a good worker is one who makes sure he or she is well rested. After all, that's our job - pimping our minds. But a good worker should also have the guts to go home and rest when he's useless. And naturally - a company that really understands development should encourage this.

posted Tuesday, January 04, 2005 1:11 AM by willemo




Powered by Dot Net Junkies, by Telligent Systems