The class you didn’t know existed

Jan 16, 2007

…Or at least a class I didn’t know existed, but let’s see if I’m right or not. The class resides in the System.Web.UI namespace and is used various places in ASP.NET. Among those places are the inner workings of the ViewState. The reason you (might) don’t know the class could be because of its not so strongly typed nature. In other words, if you want to use the class you have to do a lot of boxing and casting.

As you haven’t already guessed, I’m talking about the Triplet class. I’ve thought about how to use the class instead of creating my own type-safe little structures, and I came up with nothing. I don’t know what to do with the class.

Here is how you could use it

Triplet triplet = new Triplet();

triplet.First = "string";

triplet.Second = 34;

triplet.Third = DateTime.Now;

If you have any good ideas to how you would use this class, please let me know. My own imagination is limited when it comes to the Triplet class.

>* $4.95/month ASP.NET Hosting with FREE SQL 2012 DB! – Click Here!

Comments (10) -

Michiel
Michiel
1/16/2007 3:39:40 PM #

Well, it looks like a usefull structure, but that is because of my functional programming background.
Never wrote a method where it would be nice to return multiple results, and they aren't nicely connectable in an object? But one could do that in with the out keyword in C# .....

 Michal Talaga
Michal Talaga
1/16/2007 3:52:47 PM #

Ha! I knew about it. I have just create a special entry to explain a little where you can benefit from this class.
It is here:
vaultofthoughts.net/...IsAPairAndTripletClass.aspx

 Jens
Jens
1/16/2007 6:44:10 PM #

Each control's ViewState is stored in a Triplet (System.Web.UI.Triplet) with the first object being a Pair (System.Web.UI.Pair) (or an Array of Pairs) of ArrayLists of related name-values. The second object of the Triplet is an ArrayList of that control's child indices in control tree, and the third object is an ArrayList of the similar associated Triplets of those child controls.

See also:
msdn2.microsoft.com/en-us/library/ms972976.aspx about 3/4 down the page is a description on how SavePageViewState() uses Triplet, in the section Parsing the View State

Mads Kristensen
Mads Kristensen
1/16/2007 6:54:40 PM #

I know the Triplet is used in the workings of ViewState, that was also what I wrote. But since it's a public class, one could think that this class was also ment for us developers to use in our own solutions. Sure we can use it when we deal with ViewState, but the question is if there are any other uses for this class?

 Jens
Jens
1/16/2007 7:36:09 PM #

I guess it's very dependent on what kind of programming tasks your everyday has to offer.
I can't think of any examples just now, but I know for certain I have used Triplet on numerous occasions. A little note on Triplet is that it does not encapsulate its object references in properties, it exposes them directly as public class fields. A little less overhead...

Others seem to need it as well, though they do not know what they do ;)
http://thedailywtf.com/Articles/Three's_a_Crowd.aspx

PS: I have to post more thatn once...

 Manu Temmerman-Uyttenbroeck
Manu Temmerman-Uyttenbroeck
1/17/2007 5:27:54 AM #

I think I'll stick to creating my own type safe structures / classes...

Mads F
Mads F
1/17/2007 7:24:34 AM #

I saw a class just like that... I think it was in something called Power Collections or something.
The class was called Triple, and seem to be identical with the Triplet class.

Mads Kristensen
Mads Kristensen
1/17/2007 7:29:58 AM #

I think I have heard about the Power Collections before. If I remember correctly, they are a Generic collections and classes. If the Triplet would have been Generic then it would be useful, because then you didn't have to do all the boxing and casting.

Mads F
Mads F
1/17/2007 7:37:35 AM #

Yea, you're right. I remember it now. They are Generic.

www.gotdotnet.com/.../default.aspx

 Christoph Richter
Christoph Richter
1/17/2007 9:18:44 AM #

It's public, because you can write your own viewstate resolver. therefore you would need to know what to do with the "object".
there are some cases, where specially the Pair can be useful, but i would prefer to use this with an Generic class and inheritance. that is much more better.

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.