ManagementObjectCollection

This is a discussion on ManagementObjectCollection within the C# Programming forums, part of the General Programming Boards category; Hi all, I'm having trouble with getting an element from an Object of type ManagementObjectCollection without using a foreach statement. ...

  1. #1
    JBravo
    Guest

    Question ManagementObjectCollection

    Hi all,
    I'm having trouble with getting an element from an Object of type ManagementObjectCollection without using a foreach statement.
    I'm returning 1 ManagementObject from a WMI Query and would perfer to not use a foreach statement when only getting the 1 object.
    Does anyone know how to do this correctly?
    thanks,
    Donal

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    If you are sure there is exactly one object, then you can index it directly.

    Code:
    ManagementObjectCollection moc = ... // get this from somewhere
    
    if( moc.Count != 1 )
    {
          // failure. throw excpetion ?
    }
    
    ManagementObject mo = moc[0];
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    JBravo
    Guest
    I can't index it like you have shown:

    ManagementObject mo = moc[0];
    will throw a compiler error.

    any other ideas?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    1
    This is C# code, but C++ should be similar. You can get the first element by initializing and using the enumerator. For example:
    ManagementObjectSearcher query = new ManagementObjectSearcher(
    "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = " + tbIndex.Text);
    ManagementObjectCollection.ManagementObjectEnumera tor enumerator = query.Get().GetEnumerator();
    enumerator.MoveNext();
    ManagementObject mo = (ManagementObject)enumerator.Current;

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    What about just:
    Code:
    ManagementObject mo = moc.First();
    Any class that implements IEnumerable (such as ManagementObjectCollection) should be able to use the First() method.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    8 year old thread. Nice one.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Closed.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21