469,627 Members | 1,069 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,627 developers. It's quick & easy.

comparing objects

I have a collection of objects that I store in the session. Then I look in
that collection to see if the collection.contains an object. it answers
false. If I dont store that collection in the session it answers true. Is
the session somehow modifying the collection? The objects look identical.
Oct 21 '06 #1
10 1742
netnet wrote:
I have a collection of objects that I store in the session. Then I look in
that collection to see if the collection.contains an object. it answers
false. If I dont store that collection in the session it answers true. Is
the session somehow modifying the collection? The objects look identical.
Does your class have a proper Equals method ?

Arne
Oct 21 '06 #2
No? Could you go into more detail? Im actually using the
myCol.Contains(foo) method.

Thanks for your help.
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:uQg_g.22666$2g4.22379@dukeread09...
netnet wrote:
>I have a collection of objects that I store in the session. Then I look
in that collection to see if the collection.contains an object. it
answers false. If I dont store that collection in the session it answers
true. Is the session somehow modifying the collection? The objects
look identical.

Does your class have a proper Equals method ?

Arne

Oct 21 '06 #3
NetNet,
No? Could you go into more detail? Im actually using the
myCol.Contains(foo) method.
Is it possible that you give something more in detail.

I looked to this thread and it seems to me as you don't want to give any
code at all.

Cor
"netnet" <ne********@comcast.netschreef in bericht
news:uW**************@TK2MSFTNGP04.phx.gbl...
No? Could you go into more detail? Im actually using the
myCol.Contains(foo) method.

Thanks for your help.
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:uQg_g.22666$2g4.22379@dukeread09...
>netnet wrote:
>>I have a collection of objects that I store in the session. Then I look
in that collection to see if the collection.contains an object. it
answers false. If I dont store that collection in the session it
answers true. Is the session somehow modifying the collection? The
objects look identical.

Does your class have a proper Equals method ?

Arne


Oct 21 '06 #4
netnet wrote:
No? Could you go into more detail? Im actually using the
myCol.Contains(foo) method.
Contains uses Equals to see if objects are identical (and
some other ways, but Equals is probably the most relevant for you).

If you have not implemented Equals, then it uses Object Equals,
which just test if it is the same object (same location in memory) - not
if the values are the same.

To illustrate the point try and run the following code:

using System;
using System.Collections.Generic;

namespace E
{
public class A
{
private string s;
public A() : this("")
{
}
public A(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
this.s = value;
}
}
public override string ToString()
{
return s;
}
}
public class B
{
private string s;
public B() : this("")
{
}
public B(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
s = value;
}
}
public override string ToString()
{
return s;
}
public override bool Equals(object o)
{
if (o is B)
{
if (s == ((B)o).S)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public override int GetHashCode()
{
return s.GetHashCode();
}
}
public class MainClass
{
public static void Main(string[] args)
{
List<Alst1 = new List<A>();
lst1.Add(new A("test"));
Console.WriteLine(lst1.Contains(new A("test")));
List<Blst2 = new List<B>();
lst2.Add(new B("test"));
Console.WriteLine(lst2.Contains(new B("test")));
}
}
}

(it is using generics, but that nothing to do with the effect)

Arne
Oct 21 '06 #5
Sorry I didnt provide more detail Cor....Heres an example of what im
doing...

//begin code to put collection in cache on SalesRegion class

public static IList getAllActiveSalesRegionsCached()
{
if( System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"] ==
null)
resetCacheOfAllActiveSalesRegions();

return
(ArrayList)System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"];
}

public static void resetCacheOfAllActiveSalesRegions()
{
System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"] =
SalesRegion.getAllActiveSalesRegionsQuery();
}

public static IList getAllActiveSalesRegionsUncached()
{
return getAllActiveSalesRegionsQuery();
}

private static IList getAllActiveSalesRegionsQuery()
{
string theQuery = "from SalesRegion s where s.active = true";
return Db.Session.CreateQuery(theQuery).List();
}

//end code cache logic....
//then this is where i use the cached collection in a code behind...

ArrayList salesRegions = SalesRegion.getAllActiveSalesRegionsCached();
ArrayList allSelectedRegionsInActiveCollection =
((InvoiceLineItem)i.invoiceLineItems[0]).getAllSalesRegionsThatAreSelectedInTheActiveColl ection();
for(int x=0; x<salesRegions.Count; x++)
{
if(allSelectedRegionsInActiveCollection.Contains(
((SalesRegion)salesRegions[x]) ) )
row.Add("X");
else
row.Add("");

cnt++;
}

if i use the uncached version the contains logic works fine but if i use the
cached version contains fails....

thanks again for all your help




"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:jSp_g.22672$2g4.4823@dukeread09...
netnet wrote:
>No? Could you go into more detail? Im actually using the
myCol.Contains(foo) method.

Contains uses Equals to see if objects are identical (and
some other ways, but Equals is probably the most relevant for you).

If you have not implemented Equals, then it uses Object Equals,
which just test if it is the same object (same location in memory) - not
if the values are the same.

To illustrate the point try and run the following code:

using System;
using System.Collections.Generic;

namespace E
{
public class A
{
private string s;
public A() : this("")
{
}
public A(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
this.s = value;
}
}
public override string ToString()
{
return s;
}
}
public class B
{
private string s;
public B() : this("")
{
}
public B(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
s = value;
}
}
public override string ToString()
{
return s;
}
public override bool Equals(object o)
{
if (o is B)
{
if (s == ((B)o).S)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public override int GetHashCode()
{
return s.GetHashCode();
}
}
public class MainClass
{
public static void Main(string[] args)
{
List<Alst1 = new List<A>();
lst1.Add(new A("test"));
Console.WriteLine(lst1.Contains(new A("test")));
List<Blst2 = new List<B>();
lst2.Add(new B("test"));
Console.WriteLine(lst2.Contains(new B("test")));
}
}
}

(it is using generics, but that nothing to do with the effect)

Arne

Oct 21 '06 #6
Hi,

Originally, you wrote that you were storing the collection in the session.
The cache is quite different from the session and may actually be causing the
unexpected behavior you are observing.

After the following line is executed
ArrayList salesRegions = SalesRegion.getAllActiveSalesRegionsCached();
is salesRegions null? What is the count otherwise?

If you change the line to read as follows

ArrayList salesRegions = SalesRegions.getAllActiveSalesRegionsUncached();

is salesRegions null? What is the count otherwise?
--
Dave Sexton

"netnet" <ne********@comcast.netwrote in message
news:uc**************@TK2MSFTNGP03.phx.gbl...
Sorry I didnt provide more detail Cor....Heres an example of what im
doing...

//begin code to put collection in cache on SalesRegion class

public static IList getAllActiveSalesRegionsCached()
{
if( System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"] ==
null)
resetCacheOfAllActiveSalesRegions();

return
(ArrayList)System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"];
}

public static void resetCacheOfAllActiveSalesRegions()
{
System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"] =
SalesRegion.getAllActiveSalesRegionsQuery();
}

public static IList getAllActiveSalesRegionsUncached()
{
return getAllActiveSalesRegionsQuery();
}

private static IList getAllActiveSalesRegionsQuery()
{
string theQuery = "from SalesRegion s where s.active = true";
return Db.Session.CreateQuery(theQuery).List();
}

//end code cache logic....
//then this is where i use the cached collection in a code behind...

ArrayList salesRegions = SalesRegion.getAllActiveSalesRegionsCached();
ArrayList allSelectedRegionsInActiveCollection =
((InvoiceLineItem)i.invoiceLineItems[0]).getAllSalesRegionsThatAreSelectedInTheActiveColl ection();
for(int x=0; x<salesRegions.Count; x++)
{
if(allSelectedRegionsInActiveCollection.Contains(
((SalesRegion)salesRegions[x]) ) )
row.Add("X");
else
row.Add("");

cnt++;
}

if i use the uncached version the contains logic works fine but if i use the
cached version contains fails....

thanks again for all your help




"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:jSp_g.22672$2g4.4823@dukeread09...
>netnet wrote:
>>No? Could you go into more detail? Im actually using the
myCol.Contains(foo) method.

Contains uses Equals to see if objects are identical (and
some other ways, but Equals is probably the most relevant for you).

If you have not implemented Equals, then it uses Object Equals,
which just test if it is the same object (same location in memory) - not
if the values are the same.

To illustrate the point try and run the following code:

using System;
using System.Collections.Generic;

namespace E
{
public class A
{
private string s;
public A() : this("")
{
}
public A(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
this.s = value;
}
}
public override string ToString()
{
return s;
}
}
public class B
{
private string s;
public B() : this("")
{
}
public B(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
s = value;
}
}
public override string ToString()
{
return s;
}
public override bool Equals(object o)
{
if (o is B)
{
if (s == ((B)o).S)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public override int GetHashCode()
{
return s.GetHashCode();
}
}
public class MainClass
{
public static void Main(string[] args)
{
List<Alst1 = new List<A>();
lst1.Add(new A("test"));
Console.WriteLine(lst1.Contains(new A("test")));
List<Blst2 = new List<B>();
lst2.Add(new B("test"));
Console.WriteLine(lst2.Contains(new B("test")));
}
}
}

(it is using generics, but that nothing to do with the effect)

Arne


Oct 21 '06 #7
Hi Dave,
I tried both session and cache (in this scenario it wouldnt matter if it
was at an app or user level) and the results are the same. In both cases
below cached and uncached the result is 5 objects. They appear to be the
same objects but somehow using uncached the Contains logic works properly.
When using the cached version the objects are somehow different and Contains
always answers false.
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:O8**************@TK2MSFTNGP05.phx.gbl...
Hi,

Originally, you wrote that you were storing the collection in the session.
The cache is quite different from the session and may actually be causing
the unexpected behavior you are observing.

After the following line is executed
>ArrayList salesRegions = SalesRegion.getAllActiveSalesRegionsCached();

is salesRegions null? What is the count otherwise?

If you change the line to read as follows

ArrayList salesRegions =
SalesRegions.getAllActiveSalesRegionsUncached();

is salesRegions null? What is the count otherwise?
--
Dave Sexton

"netnet" <ne********@comcast.netwrote in message
news:uc**************@TK2MSFTNGP03.phx.gbl...
>Sorry I didnt provide more detail Cor....Heres an example of what im
doing...

//begin code to put collection in cache on SalesRegion class

public static IList getAllActiveSalesRegionsCached()
{
if( System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"] ==
null)
resetCacheOfAllActiveSalesRegions();

return
(ArrayList)System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"];
}

public static void resetCacheOfAllActiveSalesRegions()
{
System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"] =
SalesRegion.getAllActiveSalesRegionsQuery();
}

public static IList getAllActiveSalesRegionsUncached()
{
return getAllActiveSalesRegionsQuery();
}

private static IList getAllActiveSalesRegionsQuery()
{
string theQuery = "from SalesRegion s where s.active = true";
return Db.Session.CreateQuery(theQuery).List();
}

//end code cache logic....
//then this is where i use the cached collection in a code behind...

ArrayList salesRegions = SalesRegion.getAllActiveSalesRegionsCached();
ArrayList allSelectedRegionsInActiveCollection =
((InvoiceLineItem)i.invoiceLineItems[0]).getAllSalesRegionsThatAreSelectedInTheActiveColl ection();
for(int x=0; x<salesRegions.Count; x++)
{
if(allSelectedRegionsInActiveCollection.Contains(
((SalesRegion)salesRegions[x]) ) )
row.Add("X");
else
row.Add("");

cnt++;
}

if i use the uncached version the contains logic works fine but if i use
the cached version contains fails....

thanks again for all your help




"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:jSp_g.22672$2g4.4823@dukeread09...
>>netnet wrote:
No? Could you go into more detail? Im actually using the
myCol.Contains(foo) method.

Contains uses Equals to see if objects are identical (and
some other ways, but Equals is probably the most relevant for you).

If you have not implemented Equals, then it uses Object Equals,
which just test if it is the same object (same location in memory) - not
if the values are the same.

To illustrate the point try and run the following code:

using System;
using System.Collections.Generic;

namespace E
{
public class A
{
private string s;
public A() : this("")
{
}
public A(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
this.s = value;
}
}
public override string ToString()
{
return s;
}
}
public class B
{
private string s;
public B() : this("")
{
}
public B(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
s = value;
}
}
public override string ToString()
{
return s;
}
public override bool Equals(object o)
{
if (o is B)
{
if (s == ((B)o).S)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public override int GetHashCode()
{
return s.GetHashCode();
}
}
public class MainClass
{
public static void Main(string[] args)
{
List<Alst1 = new List<A>();
lst1.Add(new A("test"));
Console.WriteLine(lst1.Contains(new A("test")));
List<Blst2 = new List<B>();
lst2.Add(new B("test"));
Console.WriteLine(lst2.Contains(new B("test")));
}
}
}

(it is using generics, but that nothing to do with the effect)

Arne



Oct 22 '06 #8
Hi,

Then it seems that this method is the source of your problem:
>>((InvoiceLineItem)i.invoiceLineItems[0]).getAllSalesRegionsThatAreSelectedInTheActiveColl ection();
I think that the method is creating a different collection of objects than the
getAllActiveSalesRegionsCached method, and you're trying to compare the
objects at the same indices in each collection via the Contains method.

As Arne already put it, the Contains method is using the Equals method, which
checks for reference equality on objects. If each collection contains
different object instances then Contains will not work in the way that you are
trying to use it.

Your best bet I think is to ensure that the
getAllSalesRegionsThatAreSelectedInTheActiveCollec tion uses the same object
references as the getAllActiveSalesRegionsCached method.

--
Dave Sexton

"netnet" <ne********@comcast.netwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Hi Dave,
I tried both session and cache (in this scenario it wouldnt matter if it
was at an app or user level) and the results are the same. In both cases
below cached and uncached the result is 5 objects. They appear to be the
same objects but somehow using uncached the Contains logic works properly.
When using the cached version the objects are somehow different and Contains
always answers false.
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:O8**************@TK2MSFTNGP05.phx.gbl...
>Hi,

Originally, you wrote that you were storing the collection in the session.
The cache is quite different from the session and may actually be causing
the unexpected behavior you are observing.

After the following line is executed
>>ArrayList salesRegions = SalesRegion.getAllActiveSalesRegionsCached();

is salesRegions null? What is the count otherwise?

If you change the line to read as follows

ArrayList salesRegions =
SalesRegions.getAllActiveSalesRegionsUncached() ;

is salesRegions null? What is the count otherwise?
--
Dave Sexton

"netnet" <ne********@comcast.netwrote in message
news:uc**************@TK2MSFTNGP03.phx.gbl...
>>Sorry I didnt provide more detail Cor....Heres an example of what im
doing...

//begin code to put collection in cache on SalesRegion class

public static IList getAllActiveSalesRegionsCached()
{
if( System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"] ==
null)
resetCacheOfAllActiveSalesRegions();

return
(ArrayList)System.Web.HttpContext.Current.Cach e["getAllActiveSalesRegions"];
}

public static void resetCacheOfAllActiveSalesRegions()
{
System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"] =
SalesRegion.getAllActiveSalesRegionsQuery();
}

public static IList getAllActiveSalesRegionsUncached()
{
return getAllActiveSalesRegionsQuery();
}

private static IList getAllActiveSalesRegionsQuery()
{
string theQuery = "from SalesRegion s where s.active = true";
return Db.Session.CreateQuery(theQuery).List();
}

//end code cache logic....
//then this is where i use the cached collection in a code behind...

ArrayList salesRegions = SalesRegion.getAllActiveSalesRegionsCached();
ArrayList allSelectedRegionsInActiveCollection =
((InvoiceLineItem)i.invoiceLineItems[0]).getAllSalesRegionsThatAreSelectedInTheActiveColl ection();
for(int x=0; x<salesRegions.Count; x++)
{
if(allSelectedRegionsInActiveCollection.Contains(
((SalesRegion)salesRegions[x]) ) )
row.Add("X");
else
row.Add("");

cnt++;
}

if i use the uncached version the contains logic works fine but if i use
the cached version contains fails....

thanks again for all your help




"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:jSp_g.22672$2g4.4823@dukeread09...
netnet wrote:
No? Could you go into more detail? Im actually using the
myCol.Contains(foo) method.

Contains uses Equals to see if objects are identical (and
some other ways, but Equals is probably the most relevant for you).

If you have not implemented Equals, then it uses Object Equals,
which just test if it is the same object (same location in memory) - not
if the values are the same.

To illustrate the point try and run the following code:

using System;
using System.Collections.Generic;

namespace E
{
public class A
{
private string s;
public A() : this("")
{
}
public A(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
this.s = value;
}
}
public override string ToString()
{
return s;
}
}
public class B
{
private string s;
public B() : this("")
{
}
public B(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
s = value;
}
}
public override string ToString()
{
return s;
}
public override bool Equals(object o)
{
if (o is B)
{
if (s == ((B)o).S)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public override int GetHashCode()
{
return s.GetHashCode();
}
}
public class MainClass
{
public static void Main(string[] args)
{
List<Alst1 = new List<A>();
lst1.Add(new A("test"));
Console.WriteLine(lst1.Contains(new A("test")));
List<Blst2 = new List<B>();
lst2.Add(new B("test"));
Console.WriteLine(lst2.Contains(new B("test")));
}
}
}

(it is using generics, but that nothing to do with the effect)

Arne




Oct 22 '06 #9
NetNet,

Why do you not serialize/deserialize first.

This is a VB.Net sample however acts the same.

http://www.vb-tips.com/dbPages.aspx?...c-61641f5c8d9d

I hope this helps,
Cor

"netnet" <ne********@comcast.netschreef in bericht
news:uc**************@TK2MSFTNGP03.phx.gbl...
Sorry I didnt provide more detail Cor....Heres an example of what im
doing...

//begin code to put collection in cache on SalesRegion class

public static IList getAllActiveSalesRegionsCached()
{
if( System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"] ==
null)
resetCacheOfAllActiveSalesRegions();

return
(ArrayList)System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"];
}

public static void resetCacheOfAllActiveSalesRegions()
{
System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"] =
SalesRegion.getAllActiveSalesRegionsQuery();
}

public static IList getAllActiveSalesRegionsUncached()
{
return getAllActiveSalesRegionsQuery();
}

private static IList getAllActiveSalesRegionsQuery()
{
string theQuery = "from SalesRegion s where s.active = true";
return Db.Session.CreateQuery(theQuery).List();
}

//end code cache logic....
//then this is where i use the cached collection in a code behind...

ArrayList salesRegions = SalesRegion.getAllActiveSalesRegionsCached();
ArrayList allSelectedRegionsInActiveCollection =
((InvoiceLineItem)i.invoiceLineItems[0]).getAllSalesRegionsThatAreSelectedInTheActiveColl ection();
for(int x=0; x<salesRegions.Count; x++)
{
if(allSelectedRegionsInActiveCollection.Contains(
((SalesRegion)salesRegions[x]) ) )
row.Add("X");
else
row.Add("");

cnt++;
}

if i use the uncached version the contains logic works fine but if i use
the cached version contains fails....

thanks again for all your help




"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:jSp_g.22672$2g4.4823@dukeread09...
>netnet wrote:
>>No? Could you go into more detail? Im actually using the
myCol.Contains(foo) method.

Contains uses Equals to see if objects are identical (and
some other ways, but Equals is probably the most relevant for you).

If you have not implemented Equals, then it uses Object Equals,
which just test if it is the same object (same location in memory) - not
if the values are the same.

To illustrate the point try and run the following code:

using System;
using System.Collections.Generic;

namespace E
{
public class A
{
private string s;
public A() : this("")
{
}
public A(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
this.s = value;
}
}
public override string ToString()
{
return s;
}
}
public class B
{
private string s;
public B() : this("")
{
}
public B(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
s = value;
}
}
public override string ToString()
{
return s;
}
public override bool Equals(object o)
{
if (o is B)
{
if (s == ((B)o).S)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public override int GetHashCode()
{
return s.GetHashCode();
}
}
public class MainClass
{
public static void Main(string[] args)
{
List<Alst1 = new List<A>();
lst1.Add(new A("test"));
Console.WriteLine(lst1.Contains(new A("test")));
List<Blst2 = new List<B>();
lst2.Add(new B("test"));
Console.WriteLine(lst2.Contains(new B("test")));
}
}
}

(it is using generics, but that nothing to do with the effect)

Arne


Oct 23 '06 #10
Arne / Dave,
Thanks for your feedback. I override the Equal method on SalesRegion
and all works as expected. Thanks Again.

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:Oh**************@TK2MSFTNGP05.phx.gbl...
Hi,

Then it seems that this method is the source of your problem:
>>>((InvoiceLineItem)i.invoiceLineItems[0]).getAllSalesRegionsThatAreSelectedInTheActiveColl ection();

I think that the method is creating a different collection of objects than
the
getAllActiveSalesRegionsCached method, and you're trying to compare the
objects at the same indices in each collection via the Contains method.

As Arne already put it, the Contains method is using the Equals method,
which
checks for reference equality on objects. If each collection contains
different object instances then Contains will not work in the way that you
are
trying to use it.

Your best bet I think is to ensure that the
getAllSalesRegionsThatAreSelectedInTheActiveCollec tion uses the same
object
references as the getAllActiveSalesRegionsCached method.

--
Dave Sexton

"netnet" <ne********@comcast.netwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>Hi Dave,
I tried both session and cache (in this scenario it wouldnt matter if
it was at an app or user level) and the results are the same. In both
cases below cached and uncached the result is 5 objects. They appear to
be the same objects but somehow using uncached the Contains logic works
properly. When using the cached version the objects are somehow different
and Contains always answers false.
"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:O8**************@TK2MSFTNGP05.phx.gbl...
>>Hi,

Originally, you wrote that you were storing the collection in the
session. The cache is quite different from the session and may actually
be causing the unexpected behavior you are observing.

After the following line is executed

ArrayList salesRegions = SalesRegion.getAllActiveSalesRegionsCached();

is salesRegions null? What is the count otherwise?

If you change the line to read as follows

ArrayList salesRegions =
SalesRegions.getAllActiveSalesRegionsUncached( );

is salesRegions null? What is the count otherwise?
--
Dave Sexton

"netnet" <ne********@comcast.netwrote in message
news:uc**************@TK2MSFTNGP03.phx.gbl...
Sorry I didnt provide more detail Cor....Heres an example of what im
doing...

//begin code to put collection in cache on SalesRegion class

public static IList getAllActiveSalesRegionsCached()
{
if( System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"]
== null)
resetCacheOfAllActiveSalesRegions();

return
(ArrayList)System.Web.HttpContext.Current.Cac he["getAllActiveSalesRegions"];
}

public static void resetCacheOfAllActiveSalesRegions()
{
System.Web.HttpContext.Current.Cache["getAllActiveSalesRegions"] =
SalesRegion.getAllActiveSalesRegionsQuery();
}

public static IList getAllActiveSalesRegionsUncached()
{
return getAllActiveSalesRegionsQuery();
}

private static IList getAllActiveSalesRegionsQuery()
{
string theQuery = "from SalesRegion s where s.active = true";
return Db.Session.CreateQuery(theQuery).List();
}

//end code cache logic....
//then this is where i use the cached collection in a code behind...

ArrayList salesRegions = SalesRegion.getAllActiveSalesRegionsCached();
ArrayList allSelectedRegionsInActiveCollection =
((InvoiceLineItem)i.invoiceLineItems[0]).getAllSalesRegionsThatAreSelectedInTheActiveColl ection();
for(int x=0; x<salesRegions.Count; x++)
{
if(allSelectedRegionsInActiveCollection.Contains(
((SalesRegion)salesRegions[x]) ) )
row.Add("X");
else
row.Add("");

cnt++;
}

if i use the uncached version the contains logic works fine but if i
use the cached version contains fails....

thanks again for all your help




"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:jSp_g.22672$2g4.4823@dukeread09...
netnet wrote:
>No? Could you go into more detail? Im actually using the
>myCol.Contains(foo) method.
>
Contains uses Equals to see if objects are identical (and
some other ways, but Equals is probably the most relevant for you).
>
If you have not implemented Equals, then it uses Object Equals,
which just test if it is the same object (same location in memory) -
not
if the values are the same.
>
To illustrate the point try and run the following code:
>
using System;
using System.Collections.Generic;
>
namespace E
{
public class A
{
private string s;
public A() : this("")
{
}
public A(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
this.s = value;
}
}
public override string ToString()
{
return s;
}
}
public class B
{
private string s;
public B() : this("")
{
}
public B(string s)
{
this.s = s;
}
public string S
{
get
{
return s;
}
set
{
s = value;
}
}
public override string ToString()
{
return s;
}
public override bool Equals(object o)
{
if (o is B)
{
if (s == ((B)o).S)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public override int GetHashCode()
{
return s.GetHashCode();
}
}
public class MainClass
{
public static void Main(string[] args)
{
List<Alst1 = new List<A>();
lst1.Add(new A("test"));
Console.WriteLine(lst1.Contains(new A("test")));
List<Blst2 = new List<B>();
lst2.Add(new B("test"));
Console.WriteLine(lst2.Contains(new B("test")));
}
}
}
>
(it is using generics, but that nothing to do with the effect)
>
Arne




Oct 24 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by beliavsky | last post: by
5 posts views Thread by Skip Montanaro | last post: by
2 posts views Thread by Raphael Iloh | last post: by
19 posts views Thread by Dennis | last post: by
5 posts views Thread by ma740988 | last post: by
20 posts views Thread by Bill Pursell | last post: by
25 posts views Thread by J Caesar | last post: by
1 post views Thread by Kevin Blount | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.