473,499 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

position in array using a string

Tim
hi all,

not quite sure how to ask this, but here goes...

I have an array of values

a(0)="SMITH"
a(1)="JOHN"

I need to be able to reference each element using a string...

a("lastname") should return "SMITH"
a("firstname") should return "JOHN"

in much the same way as referencing a control in a collection of
controls, or an item in a collection of items.

any clues on how to achieve this?

(of course, everything is dynamic. I don't know that "lastname" and
"firstname" even exist before runtime)

cheers

Apr 25 '06 #1
5 1128

Tim wrote:
hi all,

not quite sure how to ask this, but here goes...

I have an array of values

a(0)="SMITH"
a(1)="JOHN"

I need to be able to reference each element using a string...

a("lastname") should return "SMITH"
a("firstname") should return "JOHN"

in much the same way as referencing a control in a collection of
controls, or an item in a collection of items.

any clues on how to achieve this?


The general name for the type of collection that achieves this is
"dictionary". A real-world dictionary maps 'words' to 'meanings' - in
..NET we say a dictionary maps 'keys' to 'values'. Both keys and values
can be of any type you like. Here both keys and values would be of type
String.

In 1.x (VB2002/3) you could just use a Hashtable, or to enforce strict
typing derive your own class from DictionaryBase (both in
System.Collections).

In 2.0 (VB2005) use Dictionary(Of String,String) from
System.Collections.Generic.

--
Larry Lard
Replies to group please

Apr 25 '06 #2
Tim
dictionary heh (2005), I'll investigate further.

many thanks.

any sample code?

cheers

Apr 25 '06 #3

Tim wrote:
dictionary heh (2005), I'll investigate further.

many thanks.

any sample code?


There's an example in the main doc topic for Dictionary(Of K,V) at

<http://msdn2.microsoft.com/en-us/library/xfhwa508(VS.80).aspx>

--
Larry Lard
Replies to group please

Apr 25 '06 #4
Tim,

As alternative not so nice done

\\\
Public Class Test
Public Shared Sub Main()
Dim a As New List(Of names)
a.Add(New names("John", "Smith"))
MessageBox.Show(a(0).mFirstname & " " & a(0).mLastname)
End Sub
End Class
Public Class names
Public mLastname As String 'should be as properties
Public mFirstname As String 'should be as properties
Public Sub New(ByVal firstname As String, _
ByVal lastname As String)
mLastname = lastname
mFirstname = firstname
End Sub
End Class
///

I hope this helps,

Cor
"Tim" <Ci************@gmail.com> schreef in bericht
news:11**********************@e56g2000cwe.googlegr oups.com...
hi all,

not quite sure how to ask this, but here goes...

I have an array of values

a(0)="SMITH"
a(1)="JOHN"

I need to be able to reference each element using a string...

a("lastname") should return "SMITH"
a("firstname") should return "JOHN"

in much the same way as referencing a control in a collection of
controls, or an item in a collection of items.

any clues on how to achieve this?

(of course, everything is dynamic. I don't know that "lastname" and
"firstname" even exist before runtime)

cheers

Apr 25 '06 #5
Tim
thanks guys, dictionary works a treat. I think this will get a lot of
use!

Apr 26 '06 #6

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

Similar topics

2
2435
by: Tesla | last post by:
Hey guys, I have a string like "lalala: djkahsd : dajkdassd : adasd :" Is there a function to find the position of the "I"th occurence of a character/string? Like lets say I want to find out...
5
2919
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
7
3192
by: Magnus Warker | last post by:
Hi, I want to traverse an (associative) array, starting from its current position, until a certain element is reached. Then, in certain cases, I want to be able to reset the current position of...
2
5493
by: Bob Cummings | last post by:
Greetings I am stumped. I am trying to create some initial conditions object by reading from a file. These objects have an integer ID and then a list of sub objects. The problem is I will...
1
2382
by: lexicon | last post by:
Hello; I have a text file. Assume that this file has some textual content. I want to append a text into this file at a specific position; not to the end of file. I assume that one way of doing...
24
8454
by: Donald Grove | last post by:
I want to populate an array with values from an ado recordset (multiple rows) I use the absolute position of the cursor in the recordset to define the row of my array to be populated. I have a...
9
2693
by: sunita jadhav | last post by:
my question is if i type in html textbox on key press event suppose i type 12345 values in textbox then i delete or edit any value of text box suppose i edit 3 and i insert the value 6 at 3 but i...
8
2425
by: Gary | last post by:
When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations? #include <stdio.h> int main(void)...
4
3458
by: HoangTuanSu | last post by:
I have just got a javascript code from my friend. I've modified it for my purpose but a problem happens. First, here's my code html <body> <table align="center" width="60%" height="100%"...
0
7134
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,...
0
7012
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
7180
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
7225
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
7392
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...
1
4920
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
1429
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
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
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.