473,785 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with Generic Collection class.

I'm working on a system that has 4 different types of client they
have mostly the same attributes so I decided to create an interface
IClient for them to implement and then just add the differing
attributes as needed to each client class.

Now I get the Client information from a DB and Load them into a Generic
dictionary

The class looks like so

class Class1<T: Dictionary<int, Twhere T:IClient

{

private IndemnityScheme scheme;

public Class1(Indemnit yScheme scheme)

{

this.scheme = scheme;

}

public int Load()

{

// "procIndemnityC lientSelect";

DataBaseAccess dba = new DataBaseAccess( this.scheme);

SqlDataReader reader =
dba.LoadData("p rocPrsymClientS elect");

SmartDataReader smartReader = new SmartDataReader (reader);

while (smartReader.Re ad()) {

AceClient newClient = new AceClient(this. scheme);

newClient.Clien tshort = smartReader.Get String("Client
Short Name");

newClient.Clien tname = smartReader.Get String("Client
Name");

newClient.Clien tref = smartReader.Get Int32("client
ref");

newClient.Addre ssLine1 = smartReader.Get String("Address
Line 1");

newClient.Addre ssLine2 = smartReader.Get String("Address
Line 2");

newClient.Addre ssLine3 = smartReader.Get String("Address
Line 3");

newClient.Addre ssLine4 = smartReader.Get String("Address
Line 4");

newClient.Addre ssLine5 = smartReader.Get String("Address
Line 5");

newClient.Addre ssLine6 = smartReader.Get String("Address
Line 6");

newClient.Telep hone = smartReader.Get String("Telepho ne
No");

newClient.PostC ode = smartReader.Get String("post
code");

newClient.Conta ct = smartReader.Get String("Contact ");

// newClient.Handl er = smartReader.Get String("Handler ");

newClient.Joind ate = smartReader.Get String("Join
Date");

newClient.Leavi ngdate = smartReader.Get String("leaving
Date");

newClient.Salut ation =
smartReader.Get String("salutat ion");

newClient.Email = smartReader.Get String("Email") ;

newClient.Comme nts = smartReader.Get String("comment s");

base.Add(newCli ent.Clientref, newClient);

}

smartReader.Clo se();

smartReader = null;

dba = null;

return base.Count;



}

}

I call the class in my program like so

AceClient c = new AceClient(Indem nityScheme.ACE) ;

Class1<AceClien tcl = new
Class1<AceClien t>(IndemnitySch eme.ACE);

cl.Load();

foreach (AceClient ac in cl.Values) {

System.Diagnost ics.Debug.Write Line(ac.Clients hort);

}

Nothing fancy just want to loop throught the collection but on trying
to complie I get an error.

This is the line of code that errors

base.Add(newCli ent.Clientref, newClient);

Error 24 The best overloaded method match for
'System.Collect ions.Generic.Di ctionary<int,T> .Add(int, T)' has some
invalid arguments

Error 25 Argument '2': cannot convert from 'Prsym.AceClien t'
to 'T'

Can anyone tell me what I am doing wrong??

Jan 18 '07 #1
3 1744
Does 'Prsym.AceClien t' implement the IClient interface?


"BombDrop" <bu************ *@gmail.comwrot e in message
news:11******** **************@ 11g2000cwr.goog legroups.com...
I'm working on a system that has 4 different types of client they
have mostly the same attributes so I decided to create an interface
IClient for them to implement and then just add the differing
attributes as needed to each client class.

Now I get the Client information from a DB and Load them into a Generic
dictionary

The class looks like so

class Class1<T: Dictionary<int, Twhere T:IClient

{

private IndemnityScheme scheme;

public Class1(Indemnit yScheme scheme)

{

this.scheme = scheme;

}

public int Load()

{

// "procIndemnityC lientSelect";

DataBaseAccess dba = new DataBaseAccess( this.scheme);

SqlDataReader reader =
dba.LoadData("p rocPrsymClientS elect");

SmartDataReader smartReader = new SmartDataReader (reader);

while (smartReader.Re ad()) {

AceClient newClient = new AceClient(this. scheme);

newClient.Clien tshort = smartReader.Get String("Client
Short Name");

newClient.Clien tname = smartReader.Get String("Client
Name");

newClient.Clien tref = smartReader.Get Int32("client
ref");

newClient.Addre ssLine1 = smartReader.Get String("Address
Line 1");

newClient.Addre ssLine2 = smartReader.Get String("Address
Line 2");

newClient.Addre ssLine3 = smartReader.Get String("Address
Line 3");

newClient.Addre ssLine4 = smartReader.Get String("Address
Line 4");

newClient.Addre ssLine5 = smartReader.Get String("Address
Line 5");

newClient.Addre ssLine6 = smartReader.Get String("Address
Line 6");

newClient.Telep hone = smartReader.Get String("Telepho ne
No");

newClient.PostC ode = smartReader.Get String("post
code");

newClient.Conta ct = smartReader.Get String("Contact ");

// newClient.Handl er = smartReader.Get String("Handler ");

newClient.Joind ate = smartReader.Get String("Join
Date");

newClient.Leavi ngdate = smartReader.Get String("leaving
Date");

newClient.Salut ation =
smartReader.Get String("salutat ion");

newClient.Email = smartReader.Get String("Email") ;

newClient.Comme nts = smartReader.Get String("comment s");

base.Add(newCli ent.Clientref, newClient);

}

smartReader.Clo se();

smartReader = null;

dba = null;

return base.Count;



}

}

I call the class in my program like so

AceClient c = new AceClient(Indem nityScheme.ACE) ;

Class1<AceClien tcl = new
Class1<AceClien t>(IndemnitySch eme.ACE);

cl.Load();

foreach (AceClient ac in cl.Values) {

System.Diagnost ics.Debug.Write Line(ac.Clients hort);

}

Nothing fancy just want to loop throught the collection but on trying
to complie I get an error.

This is the line of code that errors

base.Add(newCli ent.Clientref, newClient);

Error 24 The best overloaded method match for
'System.Collect ions.Generic.Di ctionary<int,T> .Add(int, T)' has some
invalid arguments

Error 25 Argument '2': cannot convert from 'Prsym.AceClien t'
to 'T'

Can anyone tell me what I am doing wrong??

Jan 18 '07 #2
BombDrop wrote:
I'm working on a system that has 4 different types of client they
have mostly the same attributes so I decided to create an interface
IClient for them to implement and then just add the differing
attributes as needed to each client class.
Sounds OK.
Now I get the Client information from a DB and Load them into a Generic
dictionary

The class looks like so

class Class1<T: Dictionary<int, Twhere T:IClient
OK, so a Class1<Tis a Dictionary<int, Twith some extras.
public int Load()
{
[...]
while (smartReader.Re ad()) {

AceClient newClient = new AceClient(this. scheme);
Hmm. We are in Class1<Tso we could be dealing with *any* of the
various implementers of IClient, but we always create a AceClient?
newClient.Clien tshort = smartReader.Get String("Client
Short Name");
[...]
base.Add(newCli ent.Clientref, newClient);
base here is a Dictionary<int, T>. Only if T is AceClient will this
addition be possible.
I call the class in my program like so
AceClient c = new AceClient(Indem nityScheme.ACE) ;

Class1<AceClien tcl = new
Class1<AceClien t>(IndemnitySch eme.ACE);

cl.Load();
[...]
Nothing fancy just want to loop throught the collection but on trying
to complie I get an error.
So at run time *in this case*, T will be AceClient; but the compiler
isn't to know that.
This is the line of code that errors
base.Add(newCli ent.Clientref, newClient);
Error 25 Argument '2': cannot convert from 'Prsym.AceClien t'
to 'T'
Can anyone tell me what I am doing wrong??
Since the compiler wants *compile time* type safety, it doesn't matter
that you do actually have T as an AceClient in your particular case.

The solution:
- give all IClient implementors a parameterless constructor. Move the
code in the scheme-accepting ctor to the 'set' part of a property for
the scheme.
- add the 'new' constraint in the declaration of Class1 thus:

class Class1<T: Dictionary<int, Twhere T:IClient, new()
// "When you use the new() constraint with other constraints, it must be
specified last"

- in the Load method, create a new T (rather than a new AceClient) and
set its scheme.

Note that (unfortunately) you can't have a constraint that says 'type T
must have a ctor that accepts a scheme'; you can only require T to have
a parameterless ctor. Which is why you'll have to move the
scheme-setting code.

--
Larry Lard
la*******@googl email.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Jan 18 '07 #3
BombDrop wrote:
I'm working on a system that has 4 different types of client they
have mostly the same attributes so I decided to create an interface
IClient for them to implement and then just add the differing
attributes as needed to each client class.

Now I get the Client information from a DB and Load them into a Generic
dictionary
<snip>
Can anyone tell me what I am doing wrong??
Yes - you're trying to add an AceClient to a dictionary which may not
be of type Dictionary<int, AceClient>.

I suspect you actually just want it to be a Dictionary<int, IClientor
Dictionary<int, AceClientinstea d of keeping it generic, given that
what you're actually adding is always an AceClient.

Jon

Jan 18 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1833
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; namespace DeterminedGenericCollection { // I got tired of copy and pasting IDisposable
2
1770
by: dcew | last post by:
Here's what I'm trying to understand; how can you store a generic collection in a variable/field? If I have an abstract generic collection class as follows... public abstract class BizCollection<T> : Collection<T> where T : BizBase { // Collection Implementation override void InsertItem(int index, T item)
8
3283
by: Steven Cummings | last post by:
Hello, I've scoured this usenet group and didn't find anything specific to my problem, so hopefully this won't be a repeated question. I'm all but certain it's not. I would like to *declare* (not just instantiate at runtime) a generic collection whose element-type is a generic class too. But I don't want to declare what the element-type's generic parameters are.
2
9867
by: AdawayNoSpam | last post by:
Said that I have the following class Class MyRootClass(Of T) End Class Class MySubClass1(Of T) Inherits MyRootClass(Of T) End Class
0
5930
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting problem serializing my stuff to XML when it comes to collections. I'm currently using .net2 with generic lists to prevent users putting all sorts of stuff in the arrays (Although im sure i'll be the only user of the classes but not the game, anyway)....
2
1490
by: Angel Mateos | last post by:
I have this structure: Class ElemBase Class Elem1 : Inherits ElemBase Class ColecBase(Of GenElem As {ElemBase, New}) : Inherits System.ComponentModel.BindingList(Of GenElem) Class Colec1 : Inherits ColecBase(Of Elem1)
4
7569
by: =?Utf-8?B?QkogU2FmZGll?= | last post by:
We have a class that has a public property that is of type List<T>. FXCop generates a DoNotExposeGenericLists error, indicating "System.Collections.Generic.List<Tis a generic collection designed for performance not inheritance and, therefore, does not contain any virtual members. The following generic collections are designed for inheritance and should be exposed instead of System.Collections.Generic.List<T>. *...
5
2168
by: sloan | last post by:
I've noticed alot of people tacking on a "T" for a generic abled version of an older class. Ex: 1.1 Code IDataStore
2
4186
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of types derived from that base type, or arrays or generic collections of instances of types derived from that base type. All is well until I come to the properties which are generic collections, I don't seem to be able to find an elegant way of...
0
10329
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
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
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
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.