Extending NameValueListBase

Extending NameValueListBase

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


mtavares posted on Tuesday, June 20, 2006

Is it possible to inherit NameValueListBase and extend it such that it will store a third column for each item?  And if so, if someone has an example I could follow, I would greatly appreciate it, since i'm a little shaky with inheriting generics.  So what I'm hoping to achieve are classes inherited from this 3 column base class such that they are declared like:

Class MyLookupList
    Inherits MyNameValueListBase(Of Integer, String, Boolean)

End Class

instead of :

Class MyNameValueList
    Inherits NameValueListBase(Of Integer, String)

End Class

Thanks,
Mike

xal replied on Tuesday, June 20, 2006

Well... then It would stop being a name value list... It would be a Name Value Value list!! Big Smile [:D]

You should really consider a ReadOnlyList for this case, but in case you know what you're doing:

What you can do is build a structure for your purpose:

Public Struct SomeStruct
Private mSomeString as String
Private mSomeBool as Boolean

Public Readonly Property SomeString As String
...
End Property
Public Readonly Property SomeBool As Boolean
...
End Property

Public Sub New(ByVal someString as String, ByVal someBool as Boolean)
    mSomeString = someString
    mSomeBool = someBool
End Sub
End Struct

Now, you can declare your nvl like:

Class MyLookupList
    Inherits MyNameValueListBase(Of Integer, SomeStruct)

You should probably override Equals(), GetHashCode() and ToString() inside the struct so that everything works as expected. (Otherwise you're likely to have trouble when binding to ie. comboboxes)

I hope that helps.

Andrés

mtavares replied on Tuesday, June 20, 2006

Thanks for the responses. Unfortunately the other requirement for me is for the extra value to be a separate field to bind to.  I am trying to adapt the lookup to use a custom web control that  actually takes a 3rd column used to determine which row is displayed as a default.  Unless i'm mistaken, the option you gave me Andres would still only give me 2 bindable columns.

I guess my options then are either use a readonlycollection with the custom control or forget using the custom control and use the namvaluelist with a regular combobox and just use a shared method to get the default option.

If there is a way to make the NameValue list bindable to 3 columns, let me know, otherwise i'll just stick with one of the other options.

Thanks,
Mike

ajj3085 replied on Tuesday, June 20, 2006

You should be able to bind to the other colums;

You binding to Value.MyString or Value.MyBool.  It should work.. at least I'm pretty sure it does in .Net 2.0...

ajj3085 replied on Tuesday, June 20, 2006

Xal suggestion is what I would recommend (defining another class to hold the string / boolean).

Copyright (c) Marimer LLC