473,405 Members | 2,160 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,405 software developers and data experts.

Question about C# 2.0 generics

Hi,

The following code gives me InvalidCastException with the following message:

{"Unable to cast object of type 'System.Collections.Generic.List`1' to type
'System.Collections.Generic.IList`1'."}

What is the reason?

Any help would be appreciated,

Alan



public OracleDatabase ELOracleDatabase

{

get

{

if (_ELOracleDatabase == null)

{

// ****************************

// InvalidCastException happens here!

IList<IOraclePackage> oraclePackages =
(IList<IOraclePackage>)(new List<OraclePackage>());

_ELOracleDatabase = new OracleDatabase(_connectionString,
null);

}

return _ELOracleDatabase;

}

}

class OraclePackage : IOraclePackage

{

string _name, _prefix;

OraclePackage()

{

_name = string.Empty;

_prefix = string.Empty;

}

public string Name

{

get { return _name; }

set { _name = value; }

}

public string Prefix

{

get { return _prefix; }

set { _prefix = value; }

}

}

public interface IOraclePackage

{

string Name

{ get; }

string Prefix

{ get; }

}


Nov 17 '05 #1
2 2317
'IList<IOraclePackage>' is not a basetype of 'List<OraclePackage>()'
that's all.
Nov 17 '05 #2
I think you're falling foul of the lack of support for variance in C#. Give
this a read:

http://msdn2.microsoft.com/en-us/library/ms134699

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton

"A.M-SG" <al******@newsgroup.nospam> wrote in message
news:ed**************@TK2MSFTNGP14.phx.gbl...
Hi,

The following code gives me InvalidCastException with the following message:
{"Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.IList`1'."}

What is the reason?

Any help would be appreciated,

Alan



public OracleDatabase ELOracleDatabase

{

get

{

if (_ELOracleDatabase == null)

{

// ****************************

// InvalidCastException happens here!

IList<IOraclePackage> oraclePackages =
(IList<IOraclePackage>)(new List<OraclePackage>());

_ELOracleDatabase = new OracleDatabase(_connectionString,
null);

}

return _ELOracleDatabase;

}

}

class OraclePackage : IOraclePackage

{

string _name, _prefix;

OraclePackage()

{

_name = string.Empty;

_prefix = string.Empty;

}

public string Name

{

get { return _name; }

set { _name = value; }

}

public string Prefix

{

get { return _prefix; }

set { _prefix = value; }

}

}

public interface IOraclePackage

{

string Name

{ get; }

string Prefix

{ get; }

}

Nov 17 '05 #3

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

Similar topics

5
by: Matthew W. Jackson | last post by:
I had a question about the "using" statement and Generics in the next version of C#, and I was directed to this newsgroup. My question is: Will the following syntax be valid? using...
16
by: bigtexan | last post by:
I would like to do the following and cannot figure it out. public class A<T> { public delegate T GetValueDelegate(A<T> var); public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
1
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList =...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
3
by: Showjumper | last post by:
Back in asp.net 1.1 i made custom collection classes per Karl Seguin's article On the Way to Mastering ASP.NET: Introducing Custom Entity Classes to take advantage of strongly typed data. Now with...
1
by: Kevin S. Goff | last post by:
Hi, all, Hopefully this will make sense: I have 2 classes that implement the same generic interface. public interface IAgingReport<T> { T GetAgingReport(DateTime dAsOfDate); }
14
by: cwineman | last post by:
Hello, I'm hoping to do something using Generics, but I'm not sure it's possible. Let's say I want to have a bunch of business objects and a data access class cooresponding to each business...
4
by: DeveloperX | last post by:
I'm having a play with EventHandlerList but the documentation is a bit ropey and I can't find any decent examples. It also doesn't seem to do what I was led to believe it would. I was under the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.