1

I have an generic List-Class and I want to use my Method CorrectData() from this class. At the moment I have implementated the Method CorrectData() into the class LoadListe. If I put this into the generic List then I get an compiler error CS1061.

Whats wrong?

Thanks Steffen

using System.Collections.ObjectModel;

namespace WindowsFormsGenerics
{
    /// <summary>Basisklasse für alle Elemente </summary>
    public class Base
    {
        /// <summary> Elementname </summary>
        public string Name { get; set; }

        /// <summary> Beschreibung </summary>
        public string Description { get; set; }
    }
    public class Knoten : Base { }
    public class OneNodeElement : Base
    {
        /// <summary> KnotenOne des Elementes </summary>
        public Knoten KnotenOne { get; set; }
        /// <summary> Schaltzustand am KnotenOne </summary>
        public bool SwitchOne { get; set; }
    }
    public class Load : OneNodeElement
    {
        public void CorrectData(){}
    }
    public sealed class LoadListe : Liste<Load>
    {
        public void CorrectData()
        {
            foreach (Load item in this)
            {
                item.CorrectData();
            }
        }
    }

    public abstract class Liste<T> : Collection<T> where T : Base
    {
        public T GetItem(string searchsString, string parameter = "NAME")
        {
            return null;
        }

        public void CorrectData()
        {
            foreach (T item in this)
            {
                item.CorrectData();
            }
        }
    }
} 
Steffen_dd
  • 59
  • 8
  • Is it too hard to give also the error message? And indicate on which line you got the error. You are wasting our time by hiding us information you already know! – Phil1970 Sep 06 '16 at 00:45

2 Answers2

1

You define where T : Base and then want to call CorrectData() on an item of that type. Such a method is not implemented in the Base class.

That method is implemented in the Load class, so you could use where T: Load instead or add the CorrectData() method in the Base class.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
0

You are missing CorrectData implementation or at least definition inside Base class. You could define your Base class like this:

public class Base
{
    /// <summary> Elementname </summary>
    public string Name { get; set; }

    /// <summary> Beschreibung </summary>
    public string Description { get; set; }

    public virtual void CorrectData() { }
}

And then override CorrectData method in descendants, like this:

public class Load : OneNodeElement
{
    public override void CorrectData() { }
}
Jure
  • 1,156
  • 5
  • 15