Generic type constraint complation error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lars

    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[]'
  • Hans Kesting

    #2
    Re: Generic type constraint complation error

    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.ReadFil e()' 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.ReadFil e(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


    Comment

    • Anthony Jones

      #3
      Re: Generic type constraint complation error

      "Hans Kesting" <news.hansdk@sp amgourmet.comwr ote in message
      news:ujRHY$EOJH A.1148@TK2MSFTN GP05.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.ReadFil e()' 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.ReadFil e(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

      Comment

      Working...