错误7,参数1:不能从转换'为'' [英] Error 7, argument 1: cannot convert from ' ' to ' '

查看:163
本文介绍了错误7,参数1:不能从转换'为''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对面,我没有见过一个错误的到来。我希望有人可以提供帮助。

I am coming across an error that I have not seen before. I am hoping some one can help.

下面是我的代码:

public class MyT
{
    public int ID { get; set; }
    public MyT Set(string Line)
    {
        int x = 0;

        this.ID = Convert.ToInt32(Line);

        return this;
    }
}

public class MyList<T> : List<T> where T : MyT, new()
{
    internal T Add(T n)
    {
        Read();
        Add(n);
        return n;
    }
    internal MyList<T> Read()
    {
        Clear();
        StreamReader sr = new StreamReader(@"../../Files/" + GetType().Name + ".txt");
        while (!sr.EndOfStream)
            Add(new T().Set(sr.ReadLine())); //<----Here is my error!
        sr.Close();
        return this;
    }
}

public class Customer : MyT
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Item : MyT
{
    public int ID { get; set; }
    public string Category { get; set; }
    public string Name { get; set; }
    public double Price { get; set; }
}

public class MyClass
{
    MyList<Customer> Customers = new MyList<Customer>();
    MyList<Item> Items = new MyList<Item>();
}

在那说,加入(新T()集(行sr.ReadLine()));我得到错误7,参数1:无法从Simple_Reservation_System.MyT'到'T'转换。有人可以帮我解决这个问题。

On the line that says, "Add(new T().Set(sr.ReadLine()));" I get "Error 7, Argument 1: cannot convert from 'Simple_Reservation_System.MyT' to 'T'". Can someone please help me fix this.

推荐答案

您添加参数使用泛型类型T.你Set方法返回一个具体的类MYT 。这不等于T.事实上,即使你把这个:

Your Add parameter takes the generic type T. Your Set method return a concrete class MyT. It is not equal to T. In fact even if you call this:

添加(新MYT())

它会返回一个错误。

我还想补充一点,这只是当你是MYLIST类中的错误。如果你调用从一个不同的类,将工作的方法相同。

I would also like to add that this is an error only while you are inside the MyList class. If you call the same method from a different class it will work.

这篇关于错误7,参数1:不能从转换'为''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆