Overriding NameValuePair.ToString

Overriding NameValuePair.ToString

Old forum URL: forums.lhotka.net/forums/t/1136.aspx


Vertigo1 posted on Wednesday, September 06, 2006

Whilst this is related to CSLA I think it's probably more of a general C#/.NET question.

Basically I have a class derived from NameValueListBase which contains an integer key and a string value.

What I really want is to override the string representation of the items within this list, which are obviously of type NameValuePair<int,string>.  As NameValuePair is a nested class, I can't see how I can override the ToString function of this class to achieve this.  Is this indeed impossible or am I missing something?  Is there any other way to achieve this.

If the above isn't possible then what I need is an object which can hold an integer and string key/value pair but which returns only the string from the ToString override.  Is there such a class in C#/.NET already or will I have to create one?  The most obvious solution would be to derive a class from the KeyValuePair generic struct but it's sealed :(

Any advice appreciated.

Toby.

ajj3085 replied on Wednesday, September 06, 2006

I'm afraid that you'll have to roll your own class.  Structs are always sealed, there's no way around that.
  I suppose  you could modify NameValuePair so that it has a ToString overload which allows you to specify a format.

Copyright (c) Marimer LLC