[Array] How Index from Name ?

  • Thread starter Thread starter tim
  • Start date Start date
T

tim

I have an array.

I have the name of an element.
I need the related index.

Is there a sort of "GetIndex" method
or I have to use the For Next cicle
until I met the given name?
 
Tim,

The index you make yourself for an array.

by instance in VBNet
for i as integer = 0 to myArray.Length - 1
'do what you want
next

C#
For (int i=0;i<myArray.Lenght;i++){//do what you want}

Did you know by the way that there are special language newsgroups.
microsoft.public.dotnet.languages.vb or csharp or vs

I hope this helps,

Cor
 
Is there a sort of "GetIndex" method

Array.IndexOf(yourArray, yourName)


Mattias
 
tim said:
I have an array.

I have the name of an element.

What exactly do you mean by the "name" of the element? If you mean the
name of a variable which was used to populate the array, that has no
meaning to the object itself which is referred to by the element.
 
Mattias Sjögren said:
Array.IndexOf(yourArray, yourName)

That would find the index of the *value* though. I *suspect* the OP
wants to be able to do:

string x = "hello";
string y = "there";

string[] array = new string[2];
array[x] = 0;
array[y] = 1;

// The line you can't do
int index = ??? GetIndex ("x");
 
tim said:
Mattias Sjogren solved in his post.
Thanks the same

So does that mean you were actually looking for the value itself in the
array? It's worth being very clear on this - an array element doesn't
*have* a name as such. (It may have a Name property, depending on the
type, of course.)
 
I populated my array in this manner

Rome
then
Milan
then
Venice
then
Florence

Question:
I have Venice,
what position is it?

Now I solved .
 
tim said:
I populated my array in this manner

Rome
then
Milan
then
Venice
then
Florence

Question:
I have Venice,
what position is it?

Now I solved .

Right - yes, as suspected, you have the value of the element.
 
Back
Top