473,662 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Comparing generic value/string types

I need to create generic table field level cache.
Table primary key (PrimaryKeyStru ctType) 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

'PrimaryKeyStru ctType' and 'PrimaryKeyStru ctType'

How to fix this ?

Andrus.

using System.Collecti ons.Generic;

class Cache<PrimaryKe yStructType{
struct Key {
public PrimaryKeyStruc tType PrimaryKey1Valu e;
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">Prima ry key value for row to be removed</param>
void Remove(PrimaryK eyStructType id) {
foreach (Key k in Store.Keys)
// Error 1 Operator '==' cannot be applied to operands of type
// 'PrimaryKeyStru ctType' and 'PrimaryKeyStru ctType'
if (k.PrimaryKey1V alue == id)
Store.Remove(k) ;
}
}

Jul 1 '07 #1
6 2915
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.co m

"Andrus" <ko********@hot .eewrote in message
news:eG******** ******@TK2MSFTN GP02.phx.gbl...
>I need to create generic table field level cache.
Table primary key (PrimaryKeyStru ctType) 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

'PrimaryKeyStru ctType' and 'PrimaryKeyStru ctType'

How to fix this ?

Andrus.

using System.Collecti ons.Generic;

class Cache<PrimaryKe yStructType{
struct Key {
public PrimaryKeyStruc tType PrimaryKey1Valu e;
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">Prima ry key value for row to be removed</param>
void Remove(PrimaryK eyStructType id) {
foreach (Key k in Store.Keys)
// Error 1 Operator '==' cannot be applied to operands of type
// 'PrimaryKeyStru ctType' and 'PrimaryKeyStru ctType'
if (k.PrimaryKey1V alue == 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
EqualityCompare r<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
EqualityCompare r<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. EqualityCompare r<T>.Default will do it all for
you. If you're in a generic type with type parameter of TFoo, just use
EqualityCompare r<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.PrimaryKey1V alue == 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.PrimaryKey1V alue.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 PrimaryStructTy pe?

--
Jon Skeet - <sk***@pobox.co m>
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
3316
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 way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even impossible to write truly generic code. For example, it seems to be impossible to write a truly generic
4
2101
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 creates a specialized copy of the | native code for each generic type instantiation with a value type, | but shares a single copy of the native code for all reference | types (since, at the native code level, references are just | pointers with...
6
5175
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 a nebulous statement, but I am looking for a generic way to convert a variable of unknown type to its actual type. Perhaps a better example would be
19
2644
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 = Color.Empty then..... or if mycolor is Color.Empty then .......
2
3379
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 checkInteger(&$value, $checks) { $err = ''; if (!is_numeric($value) || (floatval($value) != intval($value))) { $err .= 'Input must be an integer. ';
25
13019
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 *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any suggestions?
7
2036
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation: displayMsgDelegate = DisplayStatusMessage; implementation: public void DisplayStatusMessage(string statusMessage)
26
3609
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 delegate type expression in just the right place and a well-named method means we can almost read the code out loud and understand it without even thinking. Note that since 'boxing' and 'unboxing' is involved (I think), you don't get what you...
11
2542
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 generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8344
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8764
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.