【问题标题】:Displaying all items in another class显示另一个类中的所有项目
【发布时间】:2013-04-13 22:55:02
【问题描述】:

我的问题是我有一个 List 变量连接到另一个类,我想从该 List 中获取所有项目并将其放入一个字符串中。

在结果字符串中,我想查看 callNum, copyNum, content, author, year, title

这是我试图将其放入字符串的地方

public class CItemControl
    {
        //declare a list variable
        private List<CItem> mItems;
        private CItem mNewItem;
        //a method that instantiates the list
        public CItemControl()
        {
            mItems = new List<CItem>();
        }
        //attribute to get all items
        public List<CItem> Items
        {
            get { return mItems; }
        }
        public CItem NewItem
        {
            get { return mNewItem; }
        }
        //method to add item to the CItem list
        public void AddItem(int callNum, int copyNum, string content, string author, string year)
        {
            mNewItem = new CItem(callNum, copyNum, content, author, year);
            mItems.Add(mNewItem);
        }
        //method to return all items to a string
        public CItem ListAllItems()
        {
            string allItems;


        }

这是我试图从中获取项目的课程。后面会有变量添加。

class CItem
    {
        //declare attributes
        private string mTitle;
        private string mAuthor;
        private string mContent;
        private string mYear;
        private int mCopyNum;
        private int mCallNum;
        private bool mHold = false;
        private bool mBorrowed = false;
        private bool mShelf = false;

        //overload a constructor
        public CItem(int CallNum, int CopyNum, string Content, string Author, string Year)
        {
            callNum = CallNum;
            copyNum = CopyNum;
            content = Content;
            author = Author;
            year = Year;
        }

        //create the default constructor
        public CItem()
        {
            callNum = 0;
            copyNum = 0;
            content = "";
            author = "";
            year = "";
        }

        //set attributes
        public int callNum
        {
            get { return mCallNum; }
            set { mCallNum = value; }
        }
        public string content
        {
            get { return mContent; }
            set { mContent = value; }
        }
        public string author
        {
            get { return mAuthor; }
            set { mAuthor = value; }
        }
        public string year
        {
            get { return mYear; }
            set { mYear = value; }
        }
        public string title
        {
            get { return mTitle; }
            set { mTitle = value; }
        }
        public int copyNum
        {
            get { return mCopyNum; }
            set { mCopyNum = value; }
        }
        public bool hold
        {
            get { return mHold; }
        }
        public bool borrowed
        {
            get { return mBorrowed; }
        }
        public bool shelf
        {
            get { return mShelf; }
        }

        //display information for users
        public string displayInfo()
        {
            return "Call Number: " + callNum + ". Copy Number: " + copyNum + ". Title: " + title +
                ". Author: " + author + ". Year Published: " + year + ". Content: " + content;
        }

        //new method to display status of item
        public string displayStatus()
        {
            if (borrowed == true)
                return "Item is currently being borrowed.";
            if (shelf == true && hold == false)
                return "Item is available for borrowing.";
            else
                return "Item is on hold";
        }

非常感谢任何帮助! 提前致谢。

【问题讨论】:

  • 请注意,请检查微软为 c# 提供的一般命名约定:msdn.microsoft.com/en-us/library/vstudio/…
  • 只需勾选您最喜欢的答案即可。它有助于保持 StackOverflow 的一致和干净。我们通常不会将标题更改为 [已解决]。如果您不介意,我会还原您的更改。
  • 当然,请继续。

标签: c# list oop class


【解决方案1】:

ListAllItems 应该是这样的

public string ListAllItems()
{
    var sb = new StringBuilder();   // var is of type StringBuilder
    mItems.ForEach(item => sb.Append(item.displayInfo());
    return sb.ToString();
}

【讨论】:

  • 这将返回所有项目并将它们放入您的 sb 变量中?除此之外,我们不能把 var 换成 string 吗?
  • 代码遍历所有项目并将字符串表示附加到字符串构建器中。 StringBuilder 是一个设计用于很好地处理字符串连接的类。 varStringBuilder 类型,您可以根据需要更改它,但 var 会稍微提高可读性。
  • 对,我试试看。谢谢。
【解决方案2】:
return String.Join("; ", allItems.Select(item => item.displayInfo()));

【讨论】:

    【解决方案3】:

    您没有提供很多关于您希望在结果字符串中如何以及需要什么信息的信息。

    你不能用一个简单的循环来实现这个目标吗?

    using System.Text; 
    (...)
    public string ListAllItems()
    {
        StringBuilder allItems = new StringBuilder();
    
        foreach(CItem itm in Items){
            allItems.AppendLine(itm.displayInfo());
        } 
    
        return allItems.ToString();      
    }
    

    Stringbuilder 是可选的,但比字符串连接更快。

    【讨论】:

    • 抱歉,问题已更新至我希望在结果字符串中看到的内容。谢谢。
    【解决方案4】:

    我通常不喜欢像这样将格式化程序方法添加到属性包中。如果您希望灵活地更改有许多格式化实现,您可能希望让一个单独的类进行格式化。

    public interface IFormatter<in T>
    {
        string Format(T obj);
    }
    
    public class CItemFormatter : IFormatter<CItem>
    {
        public string Format(CItem item)
        {
            //formatting logic
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-02
      相关资源
      最近更新 更多