Comparing List<string>

Getting the wrong resluts with current code

Page 1 of 1

4 Replies - 6289 Views - Last Post: 22 May 2009 - 02:06 PM Rate Topic: -----

#1 GregBryant   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-May 09

Comparing List<string>

Posted 20 May 2009 - 03:38 PM

		   int h = 0;

			foreach(string hitem in Hitems)
		{
			if (Kitems.Contains(Hitems[h]) == false)
			{
				textBox3.Text = textBox3.Text + Hitems[h] + Environment.NewLine;
			}
			h = h + 1;
		}



Kitems and Hitems are List<string> type but when comparing the strings I get the wrong results any help?

This is just a Program I'm trying to get working for a Web Browser game to see what Items one would be missing.


Never mind I had a slow moment I was using the List Backwards Sorry.

This post has been edited by GregBryant: 20 May 2009 - 03:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Comparing List<string>

#2 fixo   User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Comparing List<string>

Posted 21 May 2009 - 12:05 AM

View PostGregBryant, on 20 May, 2009 - 02:38 PM, said:



Why you were used indexing in the foreach statement,
Try this way

			foreach(string hitem in Hitems)
		{
			if (!Kitems.Contains(hitem)
			{
				textBox3.Text += hitem  + Environment.NewLine;
			}
		}


~'J'~
Was This Post Helpful? 0
  • +
  • -

#3 VCSKicks   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-May 09

Re: Comparing List<string>

Posted 22 May 2009 - 12:12 PM

If you rather use indices, do something like

int h = 0;

while (h < kItems.Count)
{
	if (Kitems.Contains(Hitems[h]) == false)
	 {
		textBox3.Text = textBox3.Text + Hitems[h] + Environment.NewLine;
	 }
	 h = h + 1;
}



Was This Post Helpful? 0
  • +
  • -

#4 Jubb   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 88
  • Joined: 06-May 09

Re: Comparing List<string>

Posted 22 May 2009 - 12:51 PM

I agree with fixo but if you want to use iterators another way would be:


   for(int i = 0; i< Hitems.Length;i++)
		{
			if (!Kitems.Contains(Hitems[i])
			{
				textBox3.Text += hitem  + Environment.NewLine;
			}
		}


This post has been edited by Jubb: 22 May 2009 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Comparing List<string>

Posted 22 May 2009 - 02:06 PM

An easier way of accomplishing this would be to use the [url=http://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx]IEnumerator Interface[/ur], of the System.Collections Namespace. Something like this may do what you're looking for

IEnumerator e = Hitems.GetEnumerator();
int i = 0;
while (e.MoveNext())
{
	if(KItems[i].ToLower() == e.Current.ToString().ToLower())
		textBox3.Text += e.Current.ToString() + Environment.NewLine;

	i += 1;
}



Just an idea :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1