IDictionary定制系列

发布于 2024-11-07 10:59:19 字数 11758 浏览 3 评论 0原文

谁能看看这个并告诉我我做错了什么?我在实现 GetEnumerator 方法时遇到问题,我找到了一种让 IDE 创建所有方法的方法,以便我可以学习如何重写它们。任何人都可以帮助完成 ProductCollection 类,以便我可以更好地理解它。

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Product
/// </summary>
[Serializable]
public class Product
{
    //0 _ItemNumber = null;
    //1 _Title = null;
    //2 _Author = null;
    //3 _ISBN = null;
    //4 _ItemMessage = null;
    //5 _ISBNEAN = null;
    //6 _SystemName = null;
    //7 _ItemCategory = null;
    //8 _AccessService = null;
    //9 _AlternateItem = null;
    //10 _FullTitle = null;
    //11 _SalesAnalysisCode = null;
    //12 _SACSummaryCode = null;
    //13 _OwningOrganization = null;
    //14 _ProductManager = null;
    //15 _ProductFormat = null;
    //16 _DefaultWarehouse = null;
    //17 _ItemStatusCode = null;
    //18 _AccessType = null;
    //19 _SubstitutionReason = null;
    //20 _Saleable = null;
    //21 _OKToBackOrder = null;
    //22 _Taxable = null;
    //23 _ExcludeShipping = null;
    //24 _Commissionable = null;
    //25 _Package = null;
    //26 _Royalty = null;
    //27 _PrintOnDemand = null;
    //28 _AllowDescriptionOverride = null;
    private string[] _Product = new string[29];

    /// <summary>
    /// Advantage Product Object
    /// </summary>
    public Product()
    {
    }

    public string Title
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string Author
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ItemNumber
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ISBN
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ItemMessage
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ISBNEAN
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string AccessService
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string SystemName
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ItemCategory
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string AlternateItem
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string FullTitle
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string SalesAnalysisCode
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string SACSummaryCode
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string OwningOrganization
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ProductManager
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ProductFormat
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string DefaultWarehouse
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ItemStatusCode
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string AccessType
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string SubstitutionReason
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string Saleable
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string OKToBackOrder
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string Taxable
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ExcludeShipping
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string Commissionable
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string Package
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string Royalty
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string PrintOnDemand
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string AllowDescriptionOverride
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string[] ProductArray
    {
        get
        {
            return _Product;
        }
        set
        {
            _Product = value;
        }
    }
}


/// <summary>
/// 
/// </summary>
public class ProductCollection : IDictionary<int, Product>
{
private Dictionary<int, Product> dictionaryProduct;

public ProductCollection(int numberOfItem)
{
    dictionaryProduct = new Dictionary<int, Product>(numberOfItem);
}

void IDictionary<int, Product>.Add(int productKey, Product productValue)
{
    dictionaryProduct.Add(productKey, productValue);
}

bool IDictionary<int, Product>.ContainsKey(int productKey)
{
    return dictionaryProduct.ContainsKey(productKey);
}

ICollection<int> IDictionary<int, Product>.Keys
{
    get
    {
        return dictionaryProduct.Keys;
    }
}

bool IDictionary<int, Product>.Remove(int productKey)
{
    return (dictionaryProduct.Remove(productKey));
}

bool IDictionary<int, Product>.TryGetValue(int productKey, out Product productValue)
{
    return (dictionaryProduct.TryGetValue(productKey, out productValue));
}

ICollection<Product> IDictionary<int, Product>.Values
{
    get
    {
        return dictionaryProduct.Values;
    }
}

Product IDictionary<int, Product>.this[int productKey]
{
    get
    {
        return dictionaryProduct[productKey];
    }
    set
    {
        dictionaryProduct[productKey] = value;
    }
}

void ICollection<KeyValuePair<int, Product>>.Add(KeyValuePair<int, Product> productItem)
{
    throw new NotImplementedException();
}

void ICollection<KeyValuePair<int, Product>>.Clear()
{
    dictionaryProduct.Clear();
}

bool ICollection<KeyValuePair<int, Product>>.Contains(KeyValuePair<int, Product> productItem)
{
    return (dictionaryProduct.Contains(productItem));
}

void ICollection<KeyValuePair<int, Product>>.CopyTo(KeyValuePair<int, Product>[] productArray, int arrayIndex)
{
    throw new NotImplementedException();
}

int ICollection<KeyValuePair<int, Product>>.Count
{
    get 
    {
        return dictionaryProduct.Count;
    }
}

bool ICollection<KeyValuePair<int, Product>>.IsReadOnly
{
    get 
    {
        throw new NotImplementedException();
    }
}

bool ICollection<KeyValuePair<int, Product>>.Remove(KeyValuePair<int, Product> productItem)
{
    throw new NotImplementedException();
}

IEnumerator<KeyValuePair<int, Product>> IEnumerable<KeyValuePair<int, Product>>.GetEnumerator()
{
    throw new NotImplementedException();
}

public IEnumerator<KeyValuePair<int, Product>> GetEnumerator()
{
    throw new NotImplementedException();
}

IEnumerator IEnumerable.GetEnumerator()
{
    throw new NotImplementedException();
}


一个多星期后,我有了一个产品的自定义字典类对象,可以将其绑定到数据源而无需转换 还有其他人看到问题或改进方法吗?

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

[Serializable]
public class Product
{
// 0 _ItemNumber = null;
// 1 _Title = null;
// 2 _Author = null;
// 3 _ItemNumber = null;
// ..etc

private string[] _Product = new string[29];

public Product()
{
}

public string Title
{
    get
    {
        return _Product[1];
    }
    set
    {
        _Product[1] = value;
    }
}

public string Author
{
    get
    {
        return _Product[2];
    }
    set
    {
        _Product[2] = value;
    }
}

public string ISBN
{
    get
    {
        return _Product[3];
    }
    set
    {
        _Product[3] = value;
    }
}

public string ItemNumber
{
    get
    {
        return _Product[4];
    }
    set
    {
        _Product[4] = value;
    }
}

}

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 
/// </summary>
public class ProductCollection : DictionaryBase, IEnumerable
{
public ProductCollection this[int productKey]
{
    get
    {
        return ((ProductCollection)(Dictionary[productKey]));
    }
    set
    {
        Dictionary[productKey] = value;
    }
}

public bool Contains(Product objectProduct)
{
    return Dictionary.Contains(objectProduct);
}

public void Add(Product objectProduct)
{
    Dictionary.Add(objectProduct.ItemNumber, objectProduct);
}

public void Remove(int productKey)
{
    Dictionary.Remove(productKey);
}

IEnumerator IEnumerable.GetEnumerator()
{
    foreach (Product productObject in this.Dictionary.Values)
    {
        yield return productObject;
    }
}
}

protected void Page_Load(object sender, EventArgs e)
{
    Product objectProduct = new Product();
    objectProduct.Title = "Hello Title";
    objectProduct.Author = "Hello Author";
    objectProduct.ISBN = "Hello ISBN";
    objectProduct.ItemNumber = "Hello ItemNumber";

    ProductCollection productCollection = new ProductCollection();
    productCollection.Add(objectProduct);

    rpProducts.DataSource = productCollection;
    rpProducts.DataBind();
}

<asp:Repeater ID="rpProducts" runat="server">
        <ItemTemplate>
            <%# DataBinder.Eval(Container.DataItem, "Title") %>
            <br />
            <%# DataBinder.Eval(Container.DataItem, "Author") %>
        </ItemTemplate>
    </asp:Repeater>

Can anyone look at this and tell me what I'm doing wrong? I am having issue implementing the GetEnumerator methods, I have figured out a way to have the IDE create all of the methods so I can learn how to override them. Can anyone help complete the ProductCollection class so I can understand it better.

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Product
/// </summary>
[Serializable]
public class Product
{
    //0 _ItemNumber = null;
    //1 _Title = null;
    //2 _Author = null;
    //3 _ISBN = null;
    //4 _ItemMessage = null;
    //5 _ISBNEAN = null;
    //6 _SystemName = null;
    //7 _ItemCategory = null;
    //8 _AccessService = null;
    //9 _AlternateItem = null;
    //10 _FullTitle = null;
    //11 _SalesAnalysisCode = null;
    //12 _SACSummaryCode = null;
    //13 _OwningOrganization = null;
    //14 _ProductManager = null;
    //15 _ProductFormat = null;
    //16 _DefaultWarehouse = null;
    //17 _ItemStatusCode = null;
    //18 _AccessType = null;
    //19 _SubstitutionReason = null;
    //20 _Saleable = null;
    //21 _OKToBackOrder = null;
    //22 _Taxable = null;
    //23 _ExcludeShipping = null;
    //24 _Commissionable = null;
    //25 _Package = null;
    //26 _Royalty = null;
    //27 _PrintOnDemand = null;
    //28 _AllowDescriptionOverride = null;
    private string[] _Product = new string[29];

    /// <summary>
    /// Advantage Product Object
    /// </summary>
    public Product()
    {
    }

    public string Title
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string Author
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ItemNumber
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ISBN
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ItemMessage
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ISBNEAN
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string AccessService
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string SystemName
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ItemCategory
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string AlternateItem
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string FullTitle
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string SalesAnalysisCode
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string SACSummaryCode
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string OwningOrganization
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ProductManager
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ProductFormat
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string DefaultWarehouse
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ItemStatusCode
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string AccessType
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string SubstitutionReason
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string Saleable
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string OKToBackOrder
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string Taxable
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string ExcludeShipping
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string Commissionable
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string Package
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string Royalty
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string PrintOnDemand
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string AllowDescriptionOverride
    {
        get
        {
            return _Product[0];
        }
        set
        {
            _Product[0] = value;
        }
    }

    public string[] ProductArray
    {
        get
        {
            return _Product;
        }
        set
        {
            _Product = value;
        }
    }
}


/// <summary>
/// 
/// </summary>
public class ProductCollection : IDictionary<int, Product>
{
private Dictionary<int, Product> dictionaryProduct;

public ProductCollection(int numberOfItem)
{
    dictionaryProduct = new Dictionary<int, Product>(numberOfItem);
}

void IDictionary<int, Product>.Add(int productKey, Product productValue)
{
    dictionaryProduct.Add(productKey, productValue);
}

bool IDictionary<int, Product>.ContainsKey(int productKey)
{
    return dictionaryProduct.ContainsKey(productKey);
}

ICollection<int> IDictionary<int, Product>.Keys
{
    get
    {
        return dictionaryProduct.Keys;
    }
}

bool IDictionary<int, Product>.Remove(int productKey)
{
    return (dictionaryProduct.Remove(productKey));
}

bool IDictionary<int, Product>.TryGetValue(int productKey, out Product productValue)
{
    return (dictionaryProduct.TryGetValue(productKey, out productValue));
}

ICollection<Product> IDictionary<int, Product>.Values
{
    get
    {
        return dictionaryProduct.Values;
    }
}

Product IDictionary<int, Product>.this[int productKey]
{
    get
    {
        return dictionaryProduct[productKey];
    }
    set
    {
        dictionaryProduct[productKey] = value;
    }
}

void ICollection<KeyValuePair<int, Product>>.Add(KeyValuePair<int, Product> productItem)
{
    throw new NotImplementedException();
}

void ICollection<KeyValuePair<int, Product>>.Clear()
{
    dictionaryProduct.Clear();
}

bool ICollection<KeyValuePair<int, Product>>.Contains(KeyValuePair<int, Product> productItem)
{
    return (dictionaryProduct.Contains(productItem));
}

void ICollection<KeyValuePair<int, Product>>.CopyTo(KeyValuePair<int, Product>[] productArray, int arrayIndex)
{
    throw new NotImplementedException();
}

int ICollection<KeyValuePair<int, Product>>.Count
{
    get 
    {
        return dictionaryProduct.Count;
    }
}

bool ICollection<KeyValuePair<int, Product>>.IsReadOnly
{
    get 
    {
        throw new NotImplementedException();
    }
}

bool ICollection<KeyValuePair<int, Product>>.Remove(KeyValuePair<int, Product> productItem)
{
    throw new NotImplementedException();
}

IEnumerator<KeyValuePair<int, Product>> IEnumerable<KeyValuePair<int, Product>>.GetEnumerator()
{
    throw new NotImplementedException();
}

public IEnumerator<KeyValuePair<int, Product>> GetEnumerator()
{
    throw new NotImplementedException();
}

IEnumerator IEnumerable.GetEnumerator()
{
    throw new NotImplementedException();
}

}


After more then a week I have a custom dictionary class object for a product that can be binded to a datasource with out casting. Does anyone else see a problem or a way to make this better?

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

[Serializable]
public class Product
{
// 0 _ItemNumber = null;
// 1 _Title = null;
// 2 _Author = null;
// 3 _ItemNumber = null;
// ..etc

private string[] _Product = new string[29];

public Product()
{
}

public string Title
{
    get
    {
        return _Product[1];
    }
    set
    {
        _Product[1] = value;
    }
}

public string Author
{
    get
    {
        return _Product[2];
    }
    set
    {
        _Product[2] = value;
    }
}

public string ISBN
{
    get
    {
        return _Product[3];
    }
    set
    {
        _Product[3] = value;
    }
}

public string ItemNumber
{
    get
    {
        return _Product[4];
    }
    set
    {
        _Product[4] = value;
    }
}

}

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 
/// </summary>
public class ProductCollection : DictionaryBase, IEnumerable
{
public ProductCollection this[int productKey]
{
    get
    {
        return ((ProductCollection)(Dictionary[productKey]));
    }
    set
    {
        Dictionary[productKey] = value;
    }
}

public bool Contains(Product objectProduct)
{
    return Dictionary.Contains(objectProduct);
}

public void Add(Product objectProduct)
{
    Dictionary.Add(objectProduct.ItemNumber, objectProduct);
}

public void Remove(int productKey)
{
    Dictionary.Remove(productKey);
}

IEnumerator IEnumerable.GetEnumerator()
{
    foreach (Product productObject in this.Dictionary.Values)
    {
        yield return productObject;
    }
}
}

protected void Page_Load(object sender, EventArgs e)
{
    Product objectProduct = new Product();
    objectProduct.Title = "Hello Title";
    objectProduct.Author = "Hello Author";
    objectProduct.ISBN = "Hello ISBN";
    objectProduct.ItemNumber = "Hello ItemNumber";

    ProductCollection productCollection = new ProductCollection();
    productCollection.Add(objectProduct);

    rpProducts.DataSource = productCollection;
    rpProducts.DataBind();
}

<asp:Repeater ID="rpProducts" runat="server">
        <ItemTemplate>
            <%# DataBinder.Eval(Container.DataItem, "Title") %>
            <br />
            <%# DataBinder.Eval(Container.DataItem, "Author") %>
        </ItemTemplate>
    </asp:Repeater>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心是晴朗的。 2024-11-14 10:59:19

尝试将其添加到 ProductEnumerator

public IEnumerator IEnumerable.GetEnumerator()
{
   return (IEnumerator) GetEnumerator();
}

我认为它在抱怨,因为您尚未完全定义 IEnumerable 接口。

Try adding this to ProductEnumerator

public IEnumerator IEnumerable.GetEnumerator()
{
   return (IEnumerator) GetEnumerator();
}

I think it's complaining because you haven't fully defined the IEnumerable interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文