473,387 Members | 1,700 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,387 software developers and data experts.

Generic type constraint complation error

I have a compilation error and do not know how to fix it. This is the
first time I've tried to use a generic type constraint.

What have I done wrong?

public sealed class ClassA : IInput
{
...
}

public class ClassB<Twhere T : IInput
{

public ClassB() {...}

public IInput[] Generate()
{
Engine<Tengine = new Engine<T>();
IInput[] lst = engine.ReadFile(someFileName); <===== ERROR:
return lst;
}
}
ERROR: Cannot implicitly convert type 'T[]' to 'IInput[]'
Oct 27 '08 #1
2 1910
Lars explained :
I have a compilation error and do not know how to fix it. This is the
first time I've tried to use a generic type constraint.

What have I done wrong?

public sealed class ClassA : IInput
{
...
}

public class ClassB<Twhere T : IInput
{

public ClassB() {...}

public IInput[] Generate()
{
Engine<Tengine = new Engine<T>();
IInput[] lst = engine.ReadFile(someFileName); <===== ERROR:
return lst;
}
}
ERROR: Cannot implicitly convert type 'T[]' to 'IInput[]'
The errormessage says is all:
your 'engine.ReadFile()' method apparently returns a 'T[]' and you are
trying to put that into a 'IInput[]' variable.
Try to use explicit casting (IInput[] lst =
(IInput[])engine.ReadFile(someFileName)), although I don't know whether
that will succeed.
If it fails, store the result in a T[] (maybe you can return that?),
create a new IInput[] of the correct size and copy ann values.

Hans Kesting
Oct 27 '08 #2
"Hans Kesting" <ne*********@spamgourmet.comwrote in message
news:uj**************@TK2MSFTNGP05.phx.gbl...
Lars explained :
>I have a compilation error and do not know how to fix it. This is the
first time I've tried to use a generic type constraint.

What have I done wrong?

public sealed class ClassA : IInput
{
...
}

public class ClassB<Twhere T : IInput
{

public ClassB() {...}

public IInput[] Generate()
{
Engine<Tengine = new Engine<T>();
IInput[] lst = engine.ReadFile(someFileName); <===== ERROR:
return lst;
}
}
ERROR: Cannot implicitly convert type 'T[]' to 'IInput[]'

The errormessage says is all:
your 'engine.ReadFile()' method apparently returns a 'T[]' and you are
trying to put that into a 'IInput[]' variable.
Try to use explicit casting (IInput[] lst =
(IInput[])engine.ReadFile(someFileName)), although I don't know whether
that will succeed.
If it fails, store the result in a T[] (maybe you can return that?),
create a new IInput[] of the correct size and copy ann values.
The Engine class need to constrain its T to IInput as well for this to work.
That being the case it would probably be best the ReadFile returns IInput[]
instead of T[] anyway.

--
Anthony Jones - MVP ASP/ASP.NET

Oct 27 '08 #3

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: Jethro Guo | last post by:
C++ template use constraint by signature,It's very flexible to programmer but complex for complier, and at most time programmer can not get clear error message from complier if error occur. C#...
3
by: Brad Wood | last post by:
I have this method declaration: private SomeType getSomething<T>( T spec ) where T: String, MemoryStream The compiler error I get is: 'string' is not a valid constraint. A type used as a...
6
by: Dan Holmes | last post by:
I have a class that i need a constraint of int, string, float or bool. I have tried the following but can't make VS accept it. I read the docs and they showed that any value type can be used...
4
by: Hyun-jik Bae | last post by:
Is that not allowed to assume generic type numeric type? As far as I've tried, I got an error with the following code: public class AAA<T> { public int Foo(T a) { return a; // error: Cannot...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
3
by: BombDrop | last post by:
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...
4
by: Bliss | last post by:
I have some problems with my generic. Part of code: using System; using System.Collections.Generic; using System.Text; namespace Gnrc { public class Gnrc<T>
2
by: puzzlecracker | last post by:
Unlike C++, in Csharp you're only allowed to compare a generic type T with null, if the method it's passed in not implementing IComparable<Tor , IEquatable<T(still don't know why we need these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.