473,546 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ 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 8202
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 "associativ e 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<stri ng, 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 NameValueCollec tion might be more usefull.
Its roughly just Dictionary<stri ng,string>
Dictionary<stri ng,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
2387
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 writing a usercontrol which displays the typical two listboxes and the ability to move items from one to the other. The listboxes are populated...
2
2717
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. The problem is the article is in VisualBasic. I already get the collection to be recognized as a Data Source by the IDE. It populated the DataGrid...
0
2477
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 derived classes from the abstract classes The 1st class contains the 2nd class The 2nd class is a custom collection for the 3rd class
1
1636
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) equivalent business object(s). I have a custom collection that implements ICollection and IEnumerable. Here is the collection: Imports...
4
4116
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 people would back me up on this. The design basically uses a pool of memory, allocated as a character array. Pointers into the array are retured by...
4
1388
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
1357
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
1824
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 'School.Teacher' as an ArrayList creates the proper information (see aspx code below), but I'm unable to use this ArrayList in the Profile object....
6
2746
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 my Processes class. As far as i can tell i need to implement an IEnuerator method to do this. But how ? 'Procs Class Public Class Procs...
0
7504
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7435
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7792
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5360
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
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 we have to send another system

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.