Sunday, February 13, 2011

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

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!
}

Thanks

  • 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?

    From dove
  • Try - foreach (string propertyName in result.Properties.PropertyNames) { MessageBox.Show(properyName.ToString()); <--Wrong! }

    From steve
  • 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.

  • 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());
    }
    

    Thanks

  • 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.

  • 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.

    From thismat
  • Hi all,

    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.

  • 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... :)

0 comments:

Post a Comment