Изучаю C# неделю. Возникла вот такая ситуация:
Имею файл БД:
Код | 11|4.10.2005 22|7.12.2008 33|12.08.2010 -3|12.04.2007 -4|15.07.2009 -5|01.01.2001 -8|13.08.2002
|
Есть главный модуль программы:
Код | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //-------------------------Получаем путь к БД----------------------------------------// string path; // путь к БД Console.WriteLine("Укажите относительный путь до файла с данными (data\\money.txt:) "); path = Console.ReadLine(); // читаем путь к файлу БД с консоли //-----------------------------------------------------------------------------------//
//-------------------------Заполняем список из файла с БД----------------------------// StreamReader sr = File.OpenText(path); List<object> dataBase = new List<object>(); if (File.Exists(path)) { while (!sr.EndOfStream) { //MoneyEntry me = new MoneyEntry(); me.ConvertToObj(sr.ReadLine()); dataBase.Add(me); } }
sr.Close();
foreach(object value in dataBase) { Console.WriteLine("Запись: "); Console.WriteLine(value.ToString()); } Console.ReadKey(); } } }
|
И есть класс:
Код | using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ConsoleApplication1 { class MoneyEntry { private double _amount;
public MoneyEntry() { _amount = 0; EntryDate = DateTime.Now; }
public MoneyEntry(double amount, DateTime date) { _amount = amount; EntryDate = date; }
public void InitWhithString(string amount, string date) { double.TryParse(amount, out _amount);
DateTime dt; DateTime.TryParse(date, out dt); EntryDate = dt; }
/// <summary> /// Конвертация строки из файла /// в объект MoneyEntry /// </summary> /// <param name="line"></param> /// <returns></returns> public MoneyEntry ConvertToObj (string line) { string[] _temp; string _amount; string _date; _temp = line.Split('|'); _amount = _temp[0]; _date = _temp[1];
MoneyEntry me = new MoneyEntry(); DateTime dt;
double.TryParse(_amount, out this._amount); DateTime.TryParse(_date, out dt); this.EntryDate = dt;
return me; }
public override string ToString() { return string.Format("{0} от {1}", _amount, EntryDate); }
private double Amount { get { return _amount; } set { _amount = value; } }
public DateTime EntryDate { get; set; }
public bool IsDebit { get { return _amount >= 0; } set { if (value && _amount < 0) _amount = -_amount; } } } }
|
Сейчас моя задача - прочитать строки из файла БД, и создать список объектов MoneyEntry (с 2мя полями - amount и date). Тот вариант который я сделал (метод ConvertToObj) имеет ошибки. Я пока не понимаю где надо создавать новый объект, в Main или в самом методе ConvertToObj? И как его правильно вернуть из метода?
Спасибо заранее.
|