Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

I have a number of different collections that I need to show the properties of in a listview.

My idea is to implement a "Properties" property where I can get the name and value of each property no matter what type of object the current one is.

I've looked around a bit and found the "PropertyCollection" but no samples, have anyone tried it?

Can I determine what properties should be visible through the collection?

 

Any help or points of direction kindly accepted :)

/Kejpa

  • *Experts*
Posted

I can't help with what you want, exactly... I would guess you'd want to use Reflection to get the various properties exposed in your object, assuming you have them in a strongly typed collection class and not a generic collection.

 

I can suggest that you might want to use the built-in PropertyGrid control. It is the control you use in Visual Studio to change properties at Design time. It can be used on your own objects at runtime as well.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Hi,

what I want is something like the old PropertyBag. You put your values in the object one by one but you can also export them all by returning the bag.

I made some tries with the PropertyCollection but it's not good, I want to extract the values in the same order as I entered them, the first added should be the first in a For Each loop. For the moment I'm using ListDictionary which seems to be doing what I want.

 

/Kejpa

  • *Experts*
Posted

Whoops - thought you were looking for a way to display your collection in a "grid-like" control for editing, not for the collection type itself.

 

If this is truly for a Control, divil probably has a good suggestion.

If it's for a class, you might just go with implementing ISerializable (if my spelling is right) to have the class read/write it's own properties.

If this is a generic collection (name/value pairs) then why care about the order of the properties? If you really care, I'd suggest creating a small struct or class to hold the name/value pair and something like an ArrayList to store them all. But I'm not really sure what you're doing with these values - hard to guess at how to implement it :)

 

-nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
Whoops - thought you were looking for a way to display your collection in a "grid-like" control for editing' date=' not for the collection type itself.[/quote']

Yeah, it's for displaying properties. But not allowing editing, just display.

 

If this is a generic collection (name/value pairs) then why care about the order of the properties?

When displaying i.e. a Com-port I really want the Port# to be the first item and not the 15th. Using PropertyCollection gave me unpredicatble order of the properties, now when I'm using ListDirectory I get them in the order I added them in the class and thus I'm in control. (I like that ;))

 

/Kejpa

Posted

When displaying i.e. a Com-port I really want the Port# to be the first item and not the 15th. Using PropertyCollection gave me unpredicatble order of the properties, now when I'm using ListDirectory I get them in the order I added them in the class and thus I'm in control. (I like that ;))

I would also suggest looking at a hashtable since it uses key and value pairs where the value is of type Object which makes it very flexible for what is stored. That would make it easy to find what you want regardless of the internal order of the list. You would simply call it like this:

 

Dim hash as HashTable = New HashTable
Dim portnumber as Integer = 1024
hash.Add("Port", portnumber)
Messagebox.Show(hash.Item("Port").ToString)

"Programmers are tools for converting caffeine into code."

 

Madcow Inventions -- Software for the Sanity Challenged.

Posted

Hi,

PropertyCollection is a hash table wrapper, that's why it will not do for me.

Try add five different items instead of one and then do a for each and print them. You will not get the same order as when you entered them. That's not satisfactory for me at least.

Dim hash As Hashtable = New Hashtable()
Dim itm As DictionaryEntry
   hash.Add("Port", CInt(1024))
   hash.Add("Active", True)
   hash.Add("Protocol", True)
   hash.Add("Range", CDbl(700))
   hash.Add("Bipolar", False)

For Each itm In hash
   Console.WriteLine(itm.Key & "=" & itm.Value)
Next

Gives me this out put

Range=700

Port=1024

Protocol=True

Bipolar=False

Active=True

I'd like them in the order they were added. The ListDictionary does it for me.

 

/Kejpa

Posted
Then substitute SortedList for Hashtable in your declaration... it works like a hashtable but can also be reference by index. It keeps the same order you entered the items in.

"Programmers are tools for converting caffeine into code."

 

Madcow Inventions -- Software for the Sanity Challenged.

Posted

Sorry,

SortedList gives me a sorted list (surprise! ;) )

Active=True

Bipolar=False

Port=1024

Protocol=True

Range=700

I want them

Port=1024

Active=True

Protocol=True

Range=700

Bipolar=False

 

/Kejpa

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...