NameValueList classes - Telling the difference

NameValueList classes - Telling the difference

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


razorkai posted on Tuesday, May 09, 2006

Hi guys.

Probably a stupid question, but not sure what the answer is.  I have several NameValueList business objects that are being associated with the Tag property on a bunch of ListViewItems that represent them in the UI.

This is achieved by doing something like this (hope the code is right as I'm not at work!)

foreach (MyListClass.NameValuePair nvp in _myListClassInstance)
{
   ListViewItem item = myListView.Items.Add(nvp.Value.ToString());
   item.Tag = nvp;
}

Then I am trying to implement some fairly generic code that needs to be able to check what type the BO is that is associated with the Tag property of a given item.  I thought I could do the folowing:-

if (_myListViewItemInstance.Tag is MyListClass.NameValuePair)
   do something
else if (_myListViewItemInstance.Tag is MySecondListClass.NameValuePair)
   do something else
etc

But it seems that no matter what class the item in the Tag field is they all go down the first branch of my if statement.  I assume that this is because most of the NameValueList classes have int as their key and string as their value types.  So how can I associate a Tag with a specific list item and know what type the NameValuePair came from?  Can't think of a way at all, or am I missing something?

John.

Jav replied on Tuesday, May 09, 2006

I am pretty sure I am not fully comprehending what you are trying to do.  I use lots and lots of NameValueList type thingies.  Most of my lists need to have 3 strings in each item.  So I create my own base class patterned after Rocky's NameValueList, except that mine allows for 3 strings instead of 2.

After that, every type of list I need, I create a class inherited from my base class.  These classes have names like CustomerList, EmployerList etc.  Each has one or more Factory methods, a Criteria class but only  if needed, and a DataPortal_Fetch method.  I use these lists everywhere in the UI without any problem.

Jav

 

razorkai replied on Wednesday, May 10, 2006

Jav.

What I am trying to do is have ListViewItems represent individual items in a NameValueList.  I have been associating the NameValuePair of each item with the Tag of a ListViewItem, but hit problems as described.  Hope that is clearer.

dean replied on Tuesday, May 09, 2006

Instead of using the tag what about inheriting from the listviewitem class and adding the nvp that way?
in vb something like:

public class MyListViewITem
inherits ListViewItem

private mNVP as namevaluepair

public property NVP as namevaluepair
    get
      return mNVP
    end get
    set
       mNVP = value
    end set
end property

end class

Dean

razorkai replied on Wednesday, May 10, 2006

Dean

That is kind of the way I was thinking on the drive in to work this morning.  I assume that you meant that my inherited class would also have a property to store a reference to the Type of NameValueList? Otherwise I would still have the same problem of not knowing which class the NameValuePair came from e.g. CustomerList or EmployeeList.

Copyright (c) Marimer LLC