Re: comparing objects



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.com> wrote in message
news:O85ur7W9GHA.2288@xxxxxxxxxxxxxxxxxxxxxxx
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" <newsabcabc@xxxxxxxxxxx> wrote in message
news:uci2$mS9GHA.4196@xxxxxxxxxxxxxxxxxxxxxxx
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]).getAllSalesRegionsThatAreSelectedInTheActiveCollection();
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" <arne@xxxxxxxxxx> wrote in message
news:jSp_g.22672$2g4.4823@xxxxxxxxxxxxx
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<A> lst1 = new List<A>();
lst1.Add(new A("test"));
Console.WriteLine(lst1.Contains(new A("test")));
List<B> lst2 = 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






.



Relevant Pages

  • Re: comparing objects
    ... The cache is quite different from the session and may actually be causing the ... ArrayList salesRegions = SalesRegions.getAllActiveSalesRegionsUncached; ... private string s; ... public override string ToString() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: public static/shared read only properties in global.asax
    ... I am trying to simplify the use of the Session, Application and Cache ... Most of our developers are just breaking ... >> Public Shared ReadOnly Property TitleAs String ...
    (microsoft.public.dotnet.framework)
  • Re: How best to use php5 objects between pages?
    ... > or cache storage (PHP-APC cache can provide this, ... 1.He can use APC cache instead of dealing shared memory by himself. ... session files or databases. ... Session files are relatively secure. ...
    (comp.lang.php)
  • Re: Avoiding generating redo logs
    ... a query is executed which ... We store the results in the "cache" and then the ... outside of the session (as ours is a web app over http a new session is ... regard to this specific question regqarding NOLOGGING ...
    (comp.databases.oracle.server)
  • Re: 32-bit programs on Windows x64
    ... THE AMOUNT OF RAM ON THE MACHINE IS COMPLETELY, ... on thinking about physical memory as being an operational ... to compare its input string against every possible valid ... Note that the L2 cache is ...
    (microsoft.public.vc.mfc)

Loading