-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: IList.Contains() troubles...
PostPosted: Mon May 30, 2005 11:28 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
I am having troubles with using the .Contains() method on my bags. Basically, half of the time the Contain() method matches and half the time it doesn't. I thought it had to do with the equals() method but I've added those and it still isn't working as I expect. It only seems to be a problem when I am passing a component object... as an example:

I have a Role object, a Member Object, and a MemberRole association object. In my application I have a search function that allows you to add a Member to a given role so I perform a query based on search parameters and return a list of Members. Then I load a Role from the database. I then pass each Member from the searchResults List to the Role.Members.Contains() method and like I said, half the time it works and half the time it doesn/t My test case always involves a Member that I know to be associated with the given Role.

some code follows...

Role Mapping:
Code:
<class name="Role" table="Role" proxy="Role">

   <id name="id" column="RoleID" type="Int32" unsaved-value="-1" access="field" >
      <generator class="identity" />
   </id>

   <property name="Name" column="`Name`" type="String" not-null="true" />
   <property name="AccessLevel" column="AccessLevel" type="Int32" not-null="true"  access="nosetter.camelcase" />

   <bag name="Members" table="MemberRole" cascade="none" lazy="true">
      <key column="RoleID" />
      <one-to-many class="MemberRole"/>
   </bag>
</class>


MemberRole association class:
Code:
<class name="MemberRole" table="MemberRole" proxy="MemberRole">
   <id name="id" column="`ID`" type="Int32" unsaved-value="-1" access="field">
      <generator class="identity" />
   </id>
      
   <many-to-one   name="Member"
         class="Member, AMA.Core"
         column="MemberID"
         not-null="true" />
      
   <many-to-one   name="Role"
         class="Role"
         column="RoleID"
         not-null="true" />

   <property name="DateCreated" column="DateCreated" type="DateTime" not-null="true" update="false" access="nosetter.camelcase" />
      
   <many-to-one name="Creator" class="Member" column="CreatorID" />
</class>


Application code trying to use ArrayList.Contains() method. The idea here is that if a member is already part of a role domain, then remove them from the search results list and show a message to the viewer along with the results list:
Code:
IList members = base.DomainManager.FindMembersByName(this.FirstName.Text, this.LastName.Text);

if (members.Count > 0)
{
   bool foundDuplicateMember = false;
   foreach (MemberRole mr in this._role.Members)
   {
      if (members.Contains(mr.Member))
      {
         members.Remove(mr.Member);
         foundDuplicateMember = true;
      }
   }

   if (foundDuplicateMember)
      ShowFormMessage(MessageType.Note, "Some Members are not visible because they already belong to this Role.");
         
   if (members.Count > 0)
   {
      this.SearchResults.DataSource = members;
      this.SearchResults.DataBind();
   }
}
else
{
   base.ShowFormMessage(MessageType.Note, "There are no existing Members that match your search parameters.");
}


and the equals code from the Member class. LoginID is always unique and, therefore, a business key:
Code:
#region IComparable Members
public int CompareTo(object obj)
{
   if (obj is Member)
   {
      return this.loginID.CompareTo(((Member) obj).loginID);
   }
   return 0;
}
#endregion


I realize this is not entirely an NH question, but I'm really struggling with this...

TIA,

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 30, 2005 11:59 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You have implemented IComparable, but have you actually overloaded Equals?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 30, 2005 12:02 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
whoops!!! yes I have. sorry about the ommission:

#region Common Methods
public override bool Equals(object obj)
{
if (this == obj) return true;
if (!(obj is Member)) return false;
Member m = (Member)obj;
if (!this.loginID.Equals(m.loginID)) return false;
return true;
}

public override int GetHashCode()
{
return this.loginID.GetHashCode();
}

public override string ToString()
{
return "Member ('" + this.id + "'), " +
"LoginID: '" + this.loginID + "'";
}
#endregion


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 04, 2005 7:20 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
ttt


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.