2

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

在结果字符串中,我想查看 callNum、copyNum、内容、作者、年份、标题

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

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";
        }

任何帮助深表感谢!提前致谢。

4

4 回答 4

1
return String.Join("; ", allItems.Select(item => item.displayInfo()));
于 2013-04-20T17:58:40.267 回答
1

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

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

using System.Text; 
(...)
public string ListAllItems()
{
    StringBuilder allItems = new StringBuilder();

    foreach(CItem itm in Items){
        allItems.AppendLine(itm.displayInfo());
    } 

    return allItems.ToString();      
}

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

于 2013-04-20T18:01:51.237 回答
1

ListAllItems看起来像这样

public string ListAllItems()
{
    var sb = new StringBuilder();   // var is of type StringBuilder
    mItems.ForEach(item => sb.Append(item.displayInfo());
    return sb.ToString();
}
于 2013-04-20T17:55:03.677 回答
0

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

public interface IFormatter<in T>
{
    string Format(T obj);
}

public class CItemFormatter : IFormatter<CItem>
{
    public string Format(CItem item)
    {
        //formatting logic
    }
}
于 2013-04-20T18:12:20.960 回答