473,480 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Custom array index. [c#]

Markus
6,050 Recognized Expert Expert
In PHP we can do this to use a custom array index:
Expand|Select|Wrap|Line Numbers
  1. $array = array("my_index" => "my_index's value");
  2.  
and access it by:
Expand|Select|Wrap|Line Numbers
  1. echo $array['my_index'];
  2.  
I've had a look around, but I can't seem to find a way of doing this in C#.

Is it possible to do that?
Nov 30 '08 #1
4 8197
tlhintoq
3,525 Recognized Expert Specialist
I'm not sure what you mean by a 'custom' index. Is this somehow different than a regular array index?

Expand|Select|Wrap|Line Numbers
  1.             int myIndex = 2;
  2.             string[] myStringArray = { "Alpha", "Bravo", "Charlie" };
  3.             Console.WriteLine(myStringArray[myIndex]); // Should produce "Charlie" sine arrays are zero indexed
  4.  
Dec 1 '08 #2
mldisibio
190 Recognized Expert New Member
I think these are called "associative arrays" where the index is named. No, you cannot do that with CLR arrays (out of the box), but you can achieve close to the same with a Hashtable:
Expand|Select|Wrap|Line Numbers
  1. Hashtable array = new Hashtable();
  2. array.Add("my_index", "my_index's value");
  3. Console.WriteLine(array["my_index"]);
Also, a generic Dictionary<string, string> and several other collection types will do it.
Another option is to write your own Collection<T> and add a custom Indexer which accepts a string index value instead of the default int.
Dec 1 '08 #3
Plater
7,872 Recognized Expert Expert
Yeah, the Dictionary object is your ticket.
If you just want a string index, the NameValueCollection might be more usefull.
Its roughly just Dictionary<string,string>
Dictionary<string,object> is roughly what is used for the Session(and Application) object collection in web applications
Dec 1 '08 #4
Markus
6,050 Recognized Expert Expert
Thanks for that, guys.
Dec 2 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2376
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
2
2700
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx....
0
2471
by: big A | last post by:
I am receiving an error stating that File or Assembly name <filname.dll>, or one of its dependencies, was not found In one assembly I have three abstract classes In another I have three...
1
1625
by: womber | last post by:
I am getting XML from a dataset that has been populated via a storedprocedure no schemas have been applied nor any relationships. But the correct table names have been given to match the table(s)...
4
4112
by: Romeo Colacitti | last post by:
I have a need to make a custom quasi-memory allocator, and I remembered a simple ons in K&R2. Looking at the code for it now, I think I notice a "fault" in the design, and I was wondering if...
4
1376
by: Jannik Anker | last post by:
I have a standard CollectionBase implementation called UnitCollection. And yes, the collection items are class Unit ;-) Code: public class Row { public Row() { ... }
3
1354
by: Nathan Sokalski | last post by:
I have an array declared as follows: Dim ButtonList() As NavButtonInfo and a Class defined as follows: Public Class NavButtonInfo Public Shared name As String
0
1817
by: a | last post by:
I need to create an instance of a custom object 'School.Teacher' and use it in a Profile object. I'm developing a bad case of "Pretzel Logic" thinking about this. Filling the custom object ...
6
2742
by: Erick | last post by:
I've created a class called Procs and a collection class called Processes which uses a hastable object to store the Procs. Now i want to enumerate with the "For each" to extract all the Procs in...
0
7054
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6756
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5357
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,...
1
4798
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3008
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3000
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1310
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
570
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.