开发者

How to fix casting error?

When i complile the code

class customer
{
Order getOrder;
public IEnumerable<Order> MyOrders
{
    get { return getOrder; }
    set{getOrder=value;}
 }
}

class Order:IEnumerable<Order>
{
    string itemName;

    public string ItemName
    {
        get { return itemName; }
        set { itemName = value; }
    }
    public IEnumerator<Order> GetEnumerator()
    {
        return (IEnumerator)this.GetEnumerator();
    }
  IEnumerator IEnumerable.GetEnumerator(  )     
  {                                            
      return this.GetEnumerator ();
  }
}

I receive 开发者_如何学Python Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'Order'

How to fix it?


get { return getOrder; }    
set{getOrder=value;}

value is of type 'System.Collections.Generic.IEnumerable', getOrder is of type Order. Both are in no way compatible. Maybe you want getOrder (unfortunate naming also) to be IEnumerable, too?

Moreover this looks VERY wrong

Order:IEnumerable<Order>

I guess it should be something like IEnumerable<OrderItem>


The first problem is that you have Order implement IEnumerable, which is fairly strange. You can make this work, but it's odd. Normally, you wouldn't enumerate yourself.

The second problem are here:

public IEnumerator<Order> GetEnumerator()
{
    return (IEnumerator)this.GetEnumerator();
}

You need to switch how you're implementing your enumerators. IEnumerator<T> inherits IEnumerator, but you're treating it the other way around. Switch the declarations:

public IEnumerator<Order> GetEnumerator()
{
    yield return this; // You need to create an ACTUAL enumerator here!
}
IEnumerator IEnumerable.GetEnumerator()
{
    return this.GetEnumerator(); // This can call the above, since IEnumerator<Order> is also IEnumerator
}

However, in the code you posted, you never actually implement GetEnumerator to return an enumerator. There are many options for this, but you need to actually create an enumerator (or use "yield return" and let the compiler do it for you, which I did above).


You have made an Order class that is IEnumerable<Order>, that enables you to enumerate the class, but it doesn't create a collection that you can enumerate.

You probably just want to create a colleciton of Order items:

List<Order> orders = new List<Order>();

If you want to create a class that is a collection of Order items you can simply inherit from a list:

public class OrderList : List<Order> {}

Then you create the list like this:

OrderList orders = new OrderList();


I see two issues. Your property MyOrders has a return type of IEnumerable<Order>, when the field it returns is actually of type Order.

Also, I am not sure if I read this right - Order has a base class of type IEnumerable<Order> - doesn't this create some sort of endless loop? An enumerable of type enumerable of type enumarable of type enumerable of type enumarable of type enumerable of type enumarable...


What is class Order ? Is it supposed to represent a single order, or a collection of orders? If it is the latter, derive from a built-in collection class that already has IEnumerable in it (like List<>) than rename it so that it's classname communicates that it is a collection (Orders or OrderCollection) and delete the IEnumerable and IEnumerator implementations (They come for free by deriving from List<> ...

// inheriting from List<Order> means you get Enumerable for free
class Orders: List<Order> 
{    
    string itemName;    
    public string ItemName    
    {        
        get { return itemName; }        
        set { itemName = value; }    
    }
}

Regarding implicit casting, In your case you cannot cast because a collection is not the same type as the type of object which is in the collection.

But, in general, you cannot implicitly cast a more general type to a more specific type, because it might not be the more specific type. If something cpmes in as an Animal, you cannot implicitly cast it to Dog, - it might be a Cat. (The other way around is all right You can implicitly cast a Dog to an Animal)
If this was the problem, you would fix it by explicitly casting instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜