• Advertisement
Sign in to follow this  

[.net] List question

This topic is 3733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have : class someclass { string s; someclass(string astring) etc..} and in Main a : List<someclass> someclass_list; and then later : someclass_list.Add(new someclass("snoopy")); someclass_list.Add(new someclass("woodstock")); Im trying to understand : someclass_list.Contains("snoopy") It should return true if the thing I pass exists there, all it gives is an error. How would you pass "snoopy" to see if any element in the list has a string set to "snoopy", or am I wrong on what Contains() does?

Share this post


Link to post
Share on other sites
Advertisement
"snoopy" is a string. The list does not contain strings, it contains objects of type someclass. Therefore, asking whether the list contains "snoopy" is silly (and always false). Provide an object of type someclass to contains.

Share this post


Link to post
Share on other sites
"someclass" is not the same type as "string". The List type instantiated with a "someclass" argument so the Contains function is expecting an instance of type "someclass" and not anything of type "string". However, the compiler might be smart enough to convert the "string" argument using the "someclass" constructor to get something of the right type. The next step is to tell the compiler how to compare two instances of the "someclass" type. You can do this by implementing the "IEquatable" interface.

Skizz

Share this post


Link to post
Share on other sites
Skizz

"The next step is to tell the compiler how to compare two instances of the "someclass" type. You can do this by implementing the "IEquatable" interface."

We have a gesture in America, where we pass a hand palm down over our head from front to back, meaning "what you said went over my head".

At least now I have a lead to google. :-)

Share this post


Link to post
Share on other sites
Quote:
Original post by ididntdoit
Skizz

"The next step is to tell the compiler how to compare two instances of the "someclass" type. You can do this by implementing the "IEquatable" interface."

We have a gesture in America, where we pass a hand palm down over our head from front to back, meaning "what you said went over my head".

At least now I have a lead to google. :-)


That guesture is well known here :-) Maybe it could be represented: /(:-(

Anyway, here's some C# code to demonstrate:

using System;
using System.Collections.Generic;

namespace CSTest
{
class SomeClass : IEquatable<SomeClass>
{
public SomeClass (string a)
{
m_data = a;
}

public bool Equals (SomeClass rhs)
{
return m_data == rhs.m_data;
}

string m_data;
}

class Program
{
static void Check (List<SomeClass> list, string item)
{
System.Diagnostics.Trace.Write ("Does list contain '" + item + "'? ");
System.Diagnostics.Trace.WriteLine (list.Contains (new SomeClass (item)) ? "Yes" : "No");
}

static void Main (string [] args)
{
List<SomeClass>
list = new List<SomeClass> ();

list.Add (new SomeClass ("Hello"));
list.Add (new SomeClass ("World"));

Check (list, "Hello");
Check (list, "there");

list.Add (new SomeClass ("there"));

Check (list, "there");
}
}
}

Skizz

Share this post


Link to post
Share on other sites
If you need to reference an object via a string it might be more appropriate to use a Dictionary<string, someclass>.

Dictionary<string, someclass> someclass_dictionary;


someclass_dictionary.Add("snoopy", new someclass("snoopy"));
someclass_dictionary.Add("woodstock", new someclass("woodstock"));

someclass_dictionary.ContainsKey("snoopy");

someclass SnoopyInstance = someclass_dictionary["snoopy"];

Share this post


Link to post
Share on other sites
Quote:
Original post by benryves
If you need to reference an object via a string it might be more appropriate to use a Dictionary<string, someclass>.


I highly recommend using Dictionary.TryGetValue() instead of checking Dictionary.Contains() then getting the value that way as it only requires one lookup.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement