associative array

  • Thread starter Thread starter Matthias Heise
  • Start date Start date
M

Matthias Heise

Hello,

how can I walk through an associative Array in C#?

example array:

int[] arr = new Array();
int["bla"] = 5;
int["blabla"] = 10;
....

Now I need a method to determine which keys/indexers the array has. The
result has to be "bla" and "blabla" ... and not the values.

In PHP the function foreach($key=>$value in $arr) is used. But there's no
similar way in C#, or?

Thanks

Matthias
 
Hi,

I think you mean an ArrayList, otherwise the normal array doesn't accept
named keys, its only an indexer...

If you want to get the keys use another collection, like a SortTable to
access directly using GetKeys or a hashtable using the Keys property

Cheers
Salva
 
Thanks, normally that works fine. But here the special case where I need it
and where it does not work as you mentioned:

code:

WqlObjectQuery objectQuery = new WqlObjectQuery("select * from
Win32_LogicalDisk");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(objectQuery);

foreach (ManagementObject logicalDisk in searcher.Get())
{
Console.WriteLine("{0} has a DriveType of {1}", logicalDisk["Name"],
logicalDisk["DriveType"]);
}

I have a documentation which keys exist for the variable logicalDisk[] but
for some drive types some keys have no value or the key does not exist. So I
need to know which key-value pairs the object has. A cast from
ManagementObject to SortedList is not possible.

Matthias
 
Hi,

I didn't know that you were talking about WMI, The managed wrapper array is
not a .NET type and that's why it doesn't behave as I specified.

This array has a dictionary interface that allows you to access by name, but
it is only a wrapper. I have implemented a logicalDisk function and all the
keys exist, which one doesn't?

Also, I am checking the WMI extensions for .NET or the WMI browser and all
of them have a key, I am curious.

Cheers
Salva
 
Back
Top