Hi
I was wondering if someone could tell me why when I use the Contains method which is defined as part of the IList<T> interface of my lazy collection, it can't find the item I'm looking for even if it's in the list. I info on how to solve this would be appreciated.
Thanking you in advance
Francis
Here's the output from my test case.
q1.Id : 1
q1.Text : What?
q1.GetHashCode() : 1539
quiz.Name : Quiz 1 (UpdatedYdaniel)
q.Id : 1
q.Text : What?
q.GetHashCode() : 1539
== : True
con : False <------------------------------------------- This is the problem
Here's the test case code
Question q1 = Question.GetById(1);
Response.Write("q1.Id : " + q1.Id + "<br />");
Response.Write("q1.Text : " + q1.Text + "<br />");
Response.Write("q1.GetHashCode() : " + q1.GetHashCode() + "<br />");
Response.Write("<br />");
Response.Write("<br />");
Quiz quiz = Quiz.GetById(1);
Response.Write("quiz.Name : " + quiz.Name + "<br />");
Response.Write("<br />");
foreach(Question q in quiz.Questions)
{
Response.Write("q.Id : " + q.Id + "<br />");
Response.Write("q.Text : " + q.Text + "<br />");
Response.Write("q.GetHashCode() : " + q.GetHashCode() + "<br />");
Response.Write("<br />");
bool result = q1 == q;
//bool result = 1 == 2;
bool con = quiz.Questions.Contains(q1);
Response.Write("== : " + result + "<br />");
Response.Write("con : " + con + "<br />");
}
Here's are my domain classes
/*
using MyGeneration/Template/NHibernate (c) by
lujan99@usa.net
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
//using Econcordia.Quiz.Test;
namespace Econcordia.Quiz
{
/// <summary>
/// Quiz object for NHibernate mapped table 'Quizzes'.
/// </summary>
[Serializable]
public partial class Quiz : INotifyPropertyChanged
{
#region Member Variables
protected long _id;
protected string _name;
// works
protected IList<Question> _questions;
// works
//protected IQuestionList _questions;
//protected IList _questions = new QuestionList();
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Constructors
public Quiz() {}
public Quiz(string name)
{
this._name= name;
}
#endregion
#region Public Properties
public virtual long Id
{
get { return _id; }
set {if (value != this._id){_id= value;NotifyPropertyChanged("Id");}}
}
public virtual string Name
{
get { return _name; }
set {
if ( value != null && value.Length > 255)
throw new ArgumentOutOfRangeException("value", value.ToString(), "Name cannot contain more than 255 characters");
if (value != this._name){_name= value;NotifyPropertyChanged("Name");}}
}
// works
public virtual IList<Question> Questions
{
get { return _questions; }
set { _questions = value; }
}
//works
//public virtual IQuestionList Questions
//{
// get { return _questions; }
// set { _questions = value; }
//}
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
#region Equals And HashCode Overrides
/// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals( object obj )
{
if( this == obj ) return true;
if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
Quiz castObj = (Quiz)obj;
return ( castObj != null ) &&
this._id == castObj.Id;
}
/// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()
{
int hash = 57;
hash = 27 * hash * _id.GetHashCode();
return hash;
}
#endregion
}
}
/*
using MyGeneration/Template/NHibernate (c) by
lujan99@usa.net
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
namespace Econcordia.Quiz
{
/// <summary>
/// Question object for NHibernate mapped table 'Questions'.
/// </summary>
[Serializable]
public partial class Question : INotifyPropertyChanged
{
#region Member Variables
protected long _id;
protected string _text;
protected IList<Quiz> _quizzes;
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Constructors
public Question() {}
public Question(string text)
{
this._text= text;
}
#endregion
#region Public Properties
public virtual long Id
{
get { return _id; }
set {if (value != this._id){_id= value;NotifyPropertyChanged("Id");}}
}
public virtual string Text
{
get { return _text; }
set {
if ( value != null && value.Length > 255)
throw new ArgumentOutOfRangeException("value", value.ToString(), "Text cannot contain more than 255 characters");
if (value != this._text){_text= value;NotifyPropertyChanged("Text");}}
}
public virtual IList<Quiz> Quizzes
{
get { return _quizzes; }
set {_quizzes= value; }
}
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
#region Equals And HashCode Overrides
/// <summary>
/// local implementation of Equals based on unique value members
/// </summary>
public override bool Equals( object obj )
{
if( this == obj ) return true;
if( ( obj == null ) || ( obj.GetType() != this.GetType() ) ) return false;
Question castObj = (Question)obj;
return ( castObj != null ) &&
this._id == castObj.Id;
}
/// <summary>
/// local implementation of GetHashCode based on unique value members
/// </summary>
public override int GetHashCode()
{
int hash = 57;
hash = 27 * hash * _id.GetHashCode();
return hash;
}
#endregion
}
}
Here are my mapping files
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<!--Build: with
lujan99@usa.net Nhibernate template-->
<class name="Econcordia.Quiz.Question,Econcordia.Quiz" table="Questions" lazy="true">
<id name="Id" column="id" type="long">
<generator class="native" />
</id>
<property name="Text" column="text" type="string" />
<bag name="Quizzes" inverse="false" table="Quiz_Question" lazy="true" cascade="delete">
<key column="question_id" />
<many-to-many class="Econcordia.Quiz.Quiz,Econcordia.Quiz">
<column name="quiz_id" />
</many-to-many>
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<!--Build: with
lujan99@usa.net Nhibernate template-->
<class name="Econcordia.Quiz.Quiz,Econcordia.Quiz" table="Quizzes" lazy="true">
<id name="Id" column="id" type="long">
<generator class="native" />
</id>
<property name="Name" column="name" type="string" />
<bag
name="Questions" inverse="false" table="Quiz_Question" lazy="true" cascade="delete">
<key column="quiz_id" />
<many-to-many class="Econcordia.Quiz.Question,Econcordia.Quiz">
<column name="question_id" />
</many-to-many>
</bag>
</class>
</hibernate-mapping>