473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

associative array

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
Nov 17 '05 #1
3 6734
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

"Matthias Heise" wrote:
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

Nov 17 '05 #2
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
Nov 17 '05 #3
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


"Matthias Heise" wrote:
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

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
5
by: soup_or_power | last post by:
Hi I have an associative array like this: arr=30; arr=20;arr=40;arr=10; I want the sort function to sort keys in ascending order of the values on the right hand side with the following result:...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.