[Array] How Index from Name ?

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?
 
C

Cor Ligthert [MVP]

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
 
M

Mattias Sjögren

Is there a sort of "GetIndex" method

Array.IndexOf(yourArray, yourName)


Mattias
 
J

Jon Skeet [C# MVP]

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.
 
J

Jon Skeet [C# MVP]

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");
 
J

Jon Skeet [C# MVP]

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.)
 
T

tim

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 .
 
J

Jon Skeet [C# MVP]

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.
 
C

Cor Ligthert [MVP]

Jon,

I am happy that I am not the only one who misunderstood the question.

:)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top