How do I extract the value of a property in a PropertyCollection?

If I drill down on the 'Properties' in the line below is visual studion I can see the value but how do I read it?

foreach (string propertyName in result.Properties.PropertyNames)
{
  MessageBox.Show(ProperyNames[0].Value.ToString()); <--Wrong!
}

8 Answers

0
dove On

is that propertynames meant to be upper case within function?

Reading again, i have to admit to be a little confused exactly what you're after with all these properties. Is this the class property value or an instance you're after?

-1
steve On

Try:

foreach (string propertyName in result.Properties.PropertyNames)
{  MessageBox.Show(properyName.ToString()); <--Wrong!
}
2
Joel Coehoorn On

Try this:

foreach (string propertyName in result.Properties.PropertyNames)
{
    MessageBox.Show(result.Properties[propertyName].ToString());
}

Or this:

foreach (object prop in result.Properties)
{
     MessageBox.Show(prop.ToString());
}

Also: there are a couple different PropertyCollections classes in the framework. These examples are based on the System.Data class, but you might also be using the System.DirectoryServices class. However, neither of those classes are really "reflection". Reflection refers to something different- namely the System.Reflection namespace plus a couple special operators.

0
Community On

The PropertyNames is not in uppercase elsewhere, the code below works and would show the name of the property but I want to read the value. 'PropertyName' is just a string.

foreach (string propertyName in result.Properties.PropertyNames)
{
  MessageBox.Show(PropertyName.ToString());
}
0
Stu Mackellar On

I'm not certain what you're asking for, but I think the problem is that you're seeing the property names instead of their values?

If so, the reason is that you're enumerating through the PropertyCollection.PropertyNames collection and not the PropertyCollection.Values collection. Try something like this instead:

foreach (object value in result.Properties.Values)
{  
    MessageBox.Show(property.ToString());
}

I was assuming that this question referred to the System.DirectoryServices.PropertyCollection class and not System.Data.PropertyCollection because of the reference to PropertyNames, but now I'm not so sure. If the question is about the System.Data version then disregard this answer.

0
thismat On

Vb.NET

For Each prop As String In result.Properties.PropertyNames
                MessageBox.Show(result.Properties(prop).Item(0), result.Item(i).Properties(prt).Item(0))
Next

I think C# looks like this...

foreach (string property in result.Properties.PropertyNames)
{
  MessageBox.Show(result.Properties[property].Item[0]);
}

As noted above, there are a few different property collections in the framework.

3
Community On

Using a few hints from above I managed to get what I needed using the code below:

   ResultPropertyValueCollection values = result.Properties[propertyName];
    if (propertyName == "abctest")
    { 
      MessageBox.Show(values[0].ToString());
    }

Thanks to all.

0
GalacticCowboy On

If you put the value collection inside your "if", you would only retrieve it when you actually need it rather than every time through the loop. Just a suggestion... :)