thukralz

i can add the values to the IList or List, if I need to get these values in the way they went it...Say I added 5 addresses to the list and I want them one by one...

I can use foreach and get all, but I need a way to only get one at a time or two at the max as I need to update some textboxes...



Re: Visual C# General How to Iterate through List or IList

Adamus Turner

Use the .index(0), .index(1), etc... property

Adamus






Re: Visual C# General How to Iterate through List or IList

ahmedilyas

are you referring to the generic type if so then....

theList[Index];

index being the position you wish to obtain the value from in the list collection.






Re: Visual C# General How to Iterate through List or IList

Harsimrat

Say if I do something like this :

foreach (Phone phone in ContactPhoneNumbers)

now phone has everything I need....How can I get index from there........

or How I can do this

if(theList[0]) // This gives me an exception....





Re: Visual C# General How to Iterate through List or IList

ahmedilyas

what is the exception

usually you can index via the indexer as shown previously. I can understand C# compiler throwning an error with your if statement because theList[0] will return an object but an if statement evalutes a condition....






Re: Visual C# General How to Iterate through List or IList

Adamus Turner

Harsimrat wrote:

Say if I do something like this :

foreach (Phone phone in ContactPhoneNumbers)

now phone has everything I need....How can I get index from there........

or How I can do this

if(theList[0]) // This gives me an exception....

if(theList[0]) =

You don't need to loop through it. If you want to reference the value by the index, you must know what the index references.

Adamus






Re: Visual C# General How to Iterate through List or IList

Harsimrat

First When I do this :

if(something[0]) // Cannot implicitly convert type 'object' to 'bool'. An explicit conversion exists (are you missing a cast )

Then If I try to conevery to bool by doing this Convert.tobool(something) // Thats where it throws an exception...





Re: Visual C# General How to Iterate through List or IList

Harsimrat

That Thing contains lot of phone numbers of different types, i cannot do that directly..The only way is looping...



Re: Visual C# General How to Iterate through List or IList

frederikm

Hi

try ContactPhoneNumber.IndexOf(phone) to get the index

secondly try this

for (int i =o; i< contactphones.length; i++){

Phone phone = (phone) contactphonesIdea;

}

Hope this helps, please close this thread if it does






Re: Visual C# General How to Iterate through List or IList

Harsimrat

frederikm wrote:

Hi

try ContactPhoneNumber.IndexOf(phone) to get the index /////Throws an exception

secondly try this (Doesnt' work)

for (int i =o; i< contactphones.length; i++){

Phone phone = (phone) contactphones;

}

Hope this helps, please close this thread if it does





Re: Visual C# General How to Iterate through List or IList

frederikm

Hi

these are pretty basic things, which should work

can you post some code of both the collection, adding the elements to it and so on






Re: Visual C# General How to Iterate through List or IList

James Curran

if(something[0]) // Cannot implicitly convert type 'object' to 'bool'. An explicit conversion exists (are you missing a cast )

This is correct; something[0] is not a bool. So, why are you treating it like one

What are you actually trying to test in that if()

if (something[0] != null)

if (something[0].IsValid)






Re: Visual C# General How to Iterate through List or IList

James Curran

Stupid forum software think we want an emoticon when we write [ i ] (without the spaces)

That code should be:

for (int i =0; i< contactphones.length; i++){

Phone phone = (phone) contactphones[ i ];

}






Re: Visual C# General How to Iterate through List or IList

Harsimrat

ContactPhones is an IList and doesn't have length...



Re: Visual C# General How to Iterate through List or IList

James Curran

No, but it does have a Count property (via the ICollection interface which all ILists must implement)