Get index of arraylist classed object

  • Thread starter Thread starter Rodusa
  • Start date Start date
R

Rodusa

How can I update the rank object inside the arraylist based on its
index?

ContainerKeywords k = new ContainerKeywords();
int rank = 0;
if (ArItems.Contains(sItem)==false) // if did not find
{
k.item_id = sItem.ToString();
k.rank = rank;
ArItems.Add(k);
}else{ // if found
rank++
// I want to update rank here based on the arraylist position that
is found
}

I don't have any clue of how to do it.

Thank you

Rod
 
Rodusa said:
How can I update the rank object inside the arraylist based on its
index?

ContainerKeywords k = new ContainerKeywords();
int rank = 0;
if (ArItems.Contains(sItem)==false) // if did not find
{
k.item_id = sItem.ToString();
k.rank = rank;
ArItems.Add(k);
}else{ // if found
rank++
// I want to update rank here based on the arraylist position that
is found
}

I don't have any clue of how to do it.

The answer is in one of the member methods of the ArrayList class. Do you
have access to that documentation?

John Saunders
 
Yes I do. I tried
int idx = ArItems.IndexOf(sItem);
before contains, but it always returns -1. Seems like indexof is not
looking inside the typed object, and therefore is always returning -1.

I need help. I don't know where else to look for info.
 
Rodusa said:
Yes I do. I tried
int idx = ArItems.IndexOf(sItem);
before contains, but it always returns -1. Seems like indexof is not
looking inside the typed object, and therefore is always returning -1.

Thanks for the detail. It all helps.

What happens if you call IndexOf after the Contains returns true? It should
return an index.

Unless Contains never returns true?

John Saunders
 
Do I need to cast sItem?
Something like this:
string sItem = ((ContainerKeywords) ArItems[1]).item_id.ToString();
 
Rodusa said:
Do I need to cast sItem?
Something like this:
string sItem = ((ContainerKeywords) ArItems[1]).item_id.ToString();

If I were you, I'd get into the debugger and start looking at the arraylist.

Otherwise, you can start using Response.Write sorts of things, for instance
to check the number of items in the list, to go through the list and display
each item, etc.

John Saunders
 
Thank you so very much John,

It looks like that contains is looking at a string instead of looking
at the class of ArItems

Below, a little bit of more information
public class ContainerKeywords
{
private int _rank;
public int rank
{
get { return _rank; }
set { _rank = value; }
}

private string _item_id;
public string item_id
{
get { return _item_id; }
set { _item_id = value; }
}
}

Is there any way to specify in the contains method which column for the
search to happen?


ContainerKeywords k = new ContainerKeywords();
int rank = 0;
if (ArItems.Contains(sItem)==false) // if did not find
{

k.item_id = sItem.ToString();
k.rank = rank;
ArItems.Add(k);


Rod
 
Rodusa said:
Thank you so very much John,

It looks like that contains is looking at a string instead of looking
at the class of ArItems

Below, a little bit of more information
public class ContainerKeywords
{
private int _rank;
public int rank
{
get { return _rank; }
set { _rank = value; }
}

private string _item_id;
public string item_id
{
get { return _item_id; }
set { _item_id = value; }
}
}

Is there any way to specify in the contains method which column for the
search to happen?


ContainerKeywords k = new ContainerKeywords();
int rank = 0;
if (ArItems.Contains(sItem)==false) // if did not find
{

k.item_id = sItem.ToString();
k.rank = rank;
ArItems.Add(k);

I'm really not seeing what your code is meant to do. What is "sItem"?

John Saunders
 
Back
Top