473,326 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Comparing generic value/string types

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
6 2898
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
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
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
>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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
4
by: Cool Guy | last post by:
I don't understand the third paragraph under the heading 'Generic type instantiations' on <http://msdn.microsoft.com/vcsharp/2005/overview/language/generics/>: | The .NET Common Language Runtime...
6
by: Charles Law | last post by:
I want to do something like this: obj = CType(value, Value.Type) Well, not exactly, but I think that captures the essence. I realise it won't work as I have written it, and it looks a bit like...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
7
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
11
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.