0

The generic type uses the interface and the interface uses the type. Is that the cause of this problem? The lines that emit compile errors are marked below. Is there a simple fix?

using System;
using System.Collections.Generic;

namespace CcelBookParse.Utility
{
    public interface IListType
    {
        void Initialize(ParseListManager<IListType> value); // Error.
    }

    public class ParseListManager<IListType> : List<IListType> where IListType : new()
    {
        private int NextIndex;

        public ParseListManager() { }

        protected void Initialize()
        {
            NextIndex = 0;
        }

        protected IListType GetNext()
        {
            IListType Result;
            if (Count < NextIndex)
            {
                Result = this[NextIndex];
                Result.Initialize(this); // Error.
            }
            else if (Count == NextIndex)
            {
                Result = new IListType();
                Add(Result);
            }
            else
            {
                throw new Exception("List allocation index error.");
            }
            return Result;
        }

    }
}
5
  • What is the error message? Commented Feb 9, 2021 at 14:03
  • There is no types implementing a IListType Commented Feb 9, 2021 at 14:08
  • I think you are mixing the generic type IListType and the interface IListType. Can you please try to use something like T as generic type? Commented Feb 9, 2021 at 14:11
  • @mu88 I think you are onto something. I'm going to try modifying the class and will post back here with any updates. Commented Feb 9, 2021 at 14:25
  • I cannot seem to get the interaction between the interface and the generic type to be what I want, so I'm just going to go for a simpler approach now. I wanted to post the minor updates I made, but comments are limited to a short length, so even this short code won't fit. Thanks everyone for the help! Maybe what I want to do is not possible. Commented Feb 10, 2021 at 22:41

1 Answer 1

1

When you declare the ParseListManager, you are putting a type constraint saying that the type which needs to be used as the generic type needs to have a parameterless constructor (the new() after the where keyword).

Also, it's a good idea not to use types which already exist when defining a generic type. Most code I've seen uses something like TOutput or a simple T.

Regarding the usage, it's a bit weird what you are trying to describe. What's the purpose of the Initialize method inside the interface? My interpretation would be something like: each object implementing IListType can be initialized with a ParseListManager

A solution would be to leave the Initialize method in the interface parameterless.

public interface IListType
{
    void Initialize();
}

public class ParseListManager<TList> : List<TList> where TList : IListType, new()
{
    private int NextIndex;

    public ParseListManager() { }

    protected void Initialize()
    {
        NextIndex = 0;
    }

    protected TList GetNext()
    {
        TList Result;
        if (Count < NextIndex)
        {
            Result = this[NextIndex];
            Result.Initialize(); 
        }
        else if (Count == NextIndex)
        {
            Result = new TList(); // You cannot instantiate an interface, you need a proper implementation
            Add(Result);
        }
        else
        {
            throw new Exception("List allocation index error.");
        }
        return Result;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

You are correct. I think I mixed up the interface and the generic type a little bit. I am going to try to rework it now and will send an update.
I cannot seem to get the interaction between the interface and the generic type to be what I want, so I'm just going to go for a simpler approach now. I wanted to post the minor updates I made, but comments are limited to a short length, so even this short code won't fit. Thanks for the help! Maybe what I want to do is not possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.