Jason Stevens
Jason Stevens

Reputation:

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

Upvotes: 2

Views: 13379

Answers (8)

steve
steve

Reputation: 98

Try:

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

Upvotes: -1

Jason Stevens
Jason Stevens

Reputation:

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

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

Upvotes: 3

Jason Stevens
Jason Stevens

Reputation:

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

Upvotes: 0

GalacticCowboy
GalacticCowboy

Reputation: 11759

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

Upvotes: 0

Stu Mackellar
Stu Mackellar

Reputation: 11638

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.

Upvotes: 0

thismat
thismat

Reputation: 2096

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.

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415880

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.

Upvotes: 2

dove
dove

Reputation: 20674

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?

Upvotes: 0

Related Questions