By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,931 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,931 IT Pros & Developers. It's quick & easy.

Comparing generic value/string types

P: n/a
I need to create generic table field level cache.
Table primary key (PrimaryKeyStructType) can be int, string or struct
containing int and string fields.
FieldName contains table field name to be cached.

Remove() should remove table row from cache. Row is identified by table
primary key.
Trying compile class causes error

Operator '==' cannot be applied to operands of type

'PrimaryKeyStructType' and 'PrimaryKeyStructType'

How to fix this ?

Andrus.

using System.Collections.Generic;

class Cache<PrimaryKeyStructType{
struct Key {
public PrimaryKeyStructType PrimaryKey1Value;
public string FieldName;
}

Dictionary<Key, objectStore = new Dictionary<Key, object>();

/// <summary>
/// Removes table row from cache by removing all fields for this row
/// </summary>
/// <param name="id">Primary key value for row to be removed</param>
void Remove(PrimaryKeyStructType id) {
foreach (Key k in Store.Keys)
// Error 1 Operator '==' cannot be applied to operands of type
// 'PrimaryKeyStructType' and 'PrimaryKeyStructType'
if (k.PrimaryKey1Value == id)
Store.Remove(k);
}
}

Jul 1 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Andrus,

In this case, you will have to create an equality (==) operator for the
type. In addition to doing that, you are going to want to consider
overriding the Equals and GetHashCode methods on the type as well.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrus" <ko********@hot.eewrote in message
news:eG**************@TK2MSFTNGP02.phx.gbl...
>I need to create generic table field level cache.
Table primary key (PrimaryKeyStructType) can be int, string or struct
containing int and string fields.
FieldName contains table field name to be cached.

Remove() should remove table row from cache. Row is identified by table
primary key.
Trying compile class causes error

Operator '==' cannot be applied to operands of type

'PrimaryKeyStructType' and 'PrimaryKeyStructType'

How to fix this ?

Andrus.

using System.Collections.Generic;

class Cache<PrimaryKeyStructType{
struct Key {
public PrimaryKeyStructType PrimaryKey1Value;
public string FieldName;
}

Dictionary<Key, objectStore = new Dictionary<Key, object>();

/// <summary>
/// Removes table row from cache by removing all fields for this row
/// </summary>
/// <param name="id">Primary key value for row to be removed</param>
void Remove(PrimaryKeyStructType id) {
foreach (Key k in Store.Keys)
// Error 1 Operator '==' cannot be applied to operands of type
// 'PrimaryKeyStructType' and 'PrimaryKeyStructType'
if (k.PrimaryKey1Value == id)
Store.Remove(k);
}
}


Jul 1 '07 #2

P: n/a
Nicholas,

this is generic type.
Ho to create equality operator and override Equals and GetHashCode for
generic type ?
This can be done only for convrete class.

Andrus.
In this case, you will have to create an equality (==) operator for the
type. In addition to doing that, you are going to want to consider
overriding the Equals and GetHashCode methods on the type as well.

Jul 2 '07 #3

P: n/a
On Jul 2, 9:47 am, "Andrus" <kobrule...@hot.eewrote:
this is generic type.
Ho to create equality operator and override Equals and GetHashCode for
generic type ?
This can be done only for convrete class.
No, you can override both of these in generic types, just as normal.

The easiest way to implement them is to use
EqualityComparer<T>.Default for each field (replacing the T
appropriately) to take IComparable etc into account.

Jon

Jul 2 '07 #4

P: n/a
>this is generic type.
>Ho to create equality operator and override Equals and GetHashCode for
generic type ?
This can be done only for convrete class.

No, you can override both of these in generic types, just as normal.

The easiest way to implement them is to use
EqualityComparer<T>.Default for each field (replacing the T
appropriately) to take IComparable etc into account.
Jon,

thank you.

If generic type can be int or string, how to implement this ?
Shound I use GetTypeCode() to determine is it int or string and then
cast variable to int or string to make comparison ?

Where to find example about this ?

Andrus.
Jul 2 '07 #5

P: n/a
On Jul 2, 2:32 pm, "Andrus" <kobrule...@hot.eewrote:
If generic type can be int or string, how to implement this ?
Shound I use GetTypeCode() to determine is it int or string and then
cast variable to int or string to make comparison ?
No, there's no need. EqualityComparer<T>.Default will do it all for
you. If you're in a generic type with type parameter of TFoo, just use
EqualityComparer<TFoo>.Default.
Where to find example about this ?
If you could give a short but *complete* program, we could probably
implement it for you in a few lines.

Jon

Jul 2 '07 #6

P: n/a
Andrus <ko********@hot.eewrote:
If you could give a short but *complete* program, we could probably
implement it for you in a few lines.
Here is complete program
Okay, a few problems:

1) Use the "T" prefix for type parameters - it makes it clearer what's
going on.

2) You shouldn't do this:

foreach (Key k in Store.Keys)
if (k.PrimaryKey1Value == id)
Store.Remove(k);

as it will blow up when it comes back to iterating after removing a
key.

You *could* do:

foreach (Key k in Store.Keys)
{
if (k.PrimaryKey1Value.Equals(id))
{
Store.Remove(k);
break;
}
}

but that's not ideal and may not work depending on what you're
expecting it to do. The trouble is it's not clear what your key is
really meant to be. Should you be able to have multiple entries for the
same PrimaryStructType?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.