我正在为我的学习做控制台应用。我刚刚开始编程c#。我有班级文件,书籍,汤姆,报纸。 我创建了类DocumentList,它存储了我拥有的所有文档 方法那里添加单个文档。我想在我的控制台类中访问此列表有一个问题。我运行调试并添加到List是正确的。 我在互联网上寻找这个例子
Access List from another class 但没有成功:
程序甚至不会进入这个if else语句。可能我在这行中做错了因为在显示方法中,calledList为空。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zad1;
namespace Konsolowa
{
internal class Program
{
public DocumentList lista = new DocumentList();
private int choice;
public Program()
{ }
public void options(int wybor)
{
Program a = new Program();
Book bk = new Book();
Console.WriteLine("1. Add");
Console.WriteLine("2. Display ");
Console.WriteLine("3. Release");
Console.WriteLine("4. Delete");
if (wybor == 1)
{
Console.WriteLine("Your choose adding: ");
Console.WriteLine("Title: ");
String Title = Console.ReadLine();
Console.WriteLine("Press: ");
String Press = Console.ReadLine();
Console.WriteLine("Author: ");
String Author = Console.ReadLine();
Console.WriteLine("SiteCounter: ");
int SiteCounter = int.Parse(Console.ReadLine());
Console.WriteLine("name: ");
Console.WriteLine("Year of Publish: ");
int year = int.Parse(Console.ReadLine());
Document w = new Book(Title, Press, SiteCounter, Author, year);
try
{
lista.AddDocument(w);
}
catch (TitleExistException e)
{
Console.WriteLine(e);
}
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("");
Console.Write("Do you want to continue ?");
wybor = int.Parse(Console.ReadLine());
}
if (wybor == 2)
{
a.display();
Console.Write("Do you want to continue ? ");
wybor = int.Parse(Console.ReadLine());
}
if (wybor == 3)
{
Console.Write("Give title ");
String Title = (Console.ReadLine());
lista.RemovyByTitle(Title);
Console.Write("List");
a.display();
Console.Write("Do you want to continue ?");
wybor = int.Parse(Console.ReadLine());
}
}
public void display()
{
Console.WriteLine("List");
List<Document> calledList = lista.Documents;
Console.WriteLine(calledList.Count());
foreach (Document p in calledList)
{
Console.WriteLine("omg");
Console.WriteLine(p.Title);
Console.WriteLine(p.SiteCounter);
Console.WriteLine(p.Press);
}
Console.ReadLine();
}
private static void Main(string[] args)
{
Program a = new Program();
int choice;
Console.Write("Your Choice? ");
choice = int.Parse(Console.ReadLine());
a.options(choice);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace zad1
{
public abstract class Document
{
public Document(String Title, String Press, int SiteCounter)
{
this.Title = Title;
this.Press = Press;
this.SiteCounter = SiteCounter;
}
public Document()
{ }
public String Title { get; set; }
public String Press { get; set; }
public int SiteCounter { get; set; }
public override string ToString()
{
return base.ToString();
}
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace zad1
{
public class DocumentList
{
private List<Document> documents = new List<Document>();
private List<Tom> toms = new List<Tom>();
private List<Czasopismo> czasopismo = new List<Czasopismo>();
private List<Book> books = new List<Book>();
public List<Document> Documents { get { return documents; } }
public List<Book> Book { get { return Book; } }
public List<Tom> Toms { get { return Toms; } }
public List<Czasopismo> Czasopismo { get { return Czasopismo; } }
public DocumentList()
{ }
public Document RemovyByTitle(String Title)
{
Document k = documents.Single(kk => kk.Title.Equals(Title));
documents.Remove(k);
return k;
}
public void AddDocument(Document document)
{
if (documents.Contains(document))
{
throw new TitleExistException("Dodanie dokumentu o tytule ktory juz istnieje");
}
documents.Add(document);
}
public void AddTom(Tom tom)
{
foreach (Tom t in toms)
{
if (toms.Equals(t.nrTom))
{
throw new NrOfTomExistException("Dodanie dokumentu o tytule ktory juz istnieje");
}
else if (t.nrTom > t.allTom)
{
throw new nrTomBiggerThanSerieException("Wiekszy niz niz seria");
}
}
toms.Add(tom);
}
public void AddBook(Book book)
{
foreach (Book b in books)
{
if (b.YearOfPublish < 1440)
{
throw new YearException("Czlowieku nie istnieje druk");
}
}
books.Add(book);
}
public List<Document> SearchWithPhares(String Phrase)
{
List<Document> temp = new List<Document>();
foreach (Document d in documents)
{
if (d.Title.Contains(Phrase))
temp.Add(d);
}
return temp;
}
public List<Czasopismo> ListWithFreq(Freq freq)
{
List<Czasopismo> temp = new List<Czasopismo>();
foreach (Document d in documents)
{
if ((d.Equals(freq) && d is Czasopismo))
temp.Add((Czasopismo)d);
}
return temp;
}
public List<Tom> AllTomForTitle(String Title)
{
List<Tom> temp = new List<Tom>();
foreach (Document d in documents)
{
if ((d.Title == Title && d is Tom))
temp.Add((Tom)d);
}
return temp;
}
}
}
namespace zad1
{
public class Book : Document
{
public Book()
{ }
public String Author { get; set; }
public int YearOfPublish { get; set; }
public Book(String Title, String Press, int SiteCounter, String Author, int YearOfPublish)
: base(Title, Press, SiteCounter)
{
this.Author = Author;
this.YearOfPublish = YearOfPublish;
}
public override string ToString()
{
return String.Format("Author {0} yearofpublish {1} ", base.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace zad1
{
public class Czasopismo : Document
{
public int Nmb { get; set; }
public Czasopismo(String Title, String Press, int SiteCounter, int Nmb, Freq freq)
: base(Title, Press, SiteCounter)
{
this.Nmb = Nmb;
}
}
}
namespace zad1
{
public enum Freq
{
None,
Week,
Month
}
}
namespace zad1
{
public class Tom : Book
{
public int nrTom { get; set; }
public int allTom { get; set; }
public Tom(String Title, String Press, int SiteCounter, String Author, int YearOfPublish, int nrTom, int allTom)
: base(Title, Press, SiteCounter, Author, YearOfPublish)
{
this.nrTom = nrTom;
this.allTom = allTom;
}
public override string ToString()
{
return String.Format("nrTomu {0} all Tom {1} ", base.ToString());
}
}
}
namespace zad1
{
[Serializable]
public class TitleExistException : ApplicationException
{
public TitleExistException()
{ }
public TitleExistException(string message)
: base(message)
{ }
public TitleExistException(string message, Exception inner)
: base(message, inner)
{ }
protected TitleExistException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
}
[Serializable]
public class NrOfTomExistException : Exception
{
public NrOfTomExistException()
{ }
public NrOfTomExistException(string message)
: base(message)
{ }
public NrOfTomExistException(string message, Exception inner)
: base(message, inner)
{ }
protected NrOfTomExistException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
}
[Serializable]
public class nrTomBiggerThanSerieException : Exception
{
public nrTomBiggerThanSerieException()
{ }
public nrTomBiggerThanSerieException(string message)
: base(message)
{ }
public nrTomBiggerThanSerieException(string message, Exception inner)
: base(message, inner)
{ }
protected nrTomBiggerThanSerieException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
}
[Serializable]
public class YearException : Exception
{
public YearException()
{ }
public YearException(string message)
: base(message)
{ }
public YearException(string message, Exception inner)
: base(message, inner)
{ }
protected YearException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
}
}
答案 0 :(得分:1)
以下代码应该有所帮助,这是一个完整的控制台应用程序,您可以将其粘贴到visual studio中并开始运行。
请注意,Book Book继承自Document,base关键字允许您初始化超类中的属性。
我已经设置了属性,因此可以初始化它们。请注意,您需要将文档强制转换为Book以访问不在文档上的属性,如果您需要访问这些属性(我建议您在其他地方查找,因为这几乎是一个绝对的功课)。
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
const string title = "The title of the document";
const string press = "The press of the document";
const string author = "Some author of the document";
const int siteCounter = 10;
Document w = new Book(title, press, siteCounter, author, null);
List<Document> docList = new List<Document>();
try
{
docList.Add(w);
}
catch (TitleExistException e)
{
Console.WriteLine(e);
}
DisplayListOfDocuments(docList);
}
public static void DisplayListOfDocuments(List<Document> docList)
{
Console.WriteLine("List");
Console.WriteLine(docList.Count);
foreach (Document p in docList)
{
if (docList.Count == 0)
{
Console.WriteLine("List Empty");
}
else
{
Console.WriteLine(p.Title);
Console.WriteLine(p.SiteCounter);
Console.WriteLine(p.Press);
}
}
Console.ReadLine();
}
}
public class Book : Document
{
public Book(string title, string press, int siteCounter, string author, string rok)
: base(title, press, siteCounter)
{
Rok = rok;
}
public string Rok { get; set; }
}
public class Document
{
public Document(string title, string press, int siteCounter)
{
Title = title;
Press = press;
SiteCounter = siteCounter;
}
public Document()
{
// TODO: Complete member initialization
}
public String Title { get; set; }
public String Press { get; set; }
public int SiteCounter { get; set; }
}
public class TitleExistException : Exception
{
}
}
注意:我删除了DocumentList,因为它完全没必要。