我正在使用C#创建基本数据存储程序。我很新,请放轻松我。我想将它拆分为两个类,以便其他人能够从他们自己的main方法运行它。我的问题是,我不知道从哪里开始。我尝试为方法添加另一个.cs文件,但是对程序中的数组的引用和创建错误。这就是我所拥有的。
- @model.items.each do |m|
- if m.img
= image_tag m.img.url
- else
no image
答案 0 :(得分:0)
项目对象 - 存储项目的实例(编号,描述)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BasicDataStorageApp
{
public class Item
{
private int _number;
public int Number
{
get { return _number; }
set { _number = value; }
}
private string _description;
public string Description
{
get { return _description; }
set { _description = value; }
}
public Item(int number, string description)
: this()
{
_number = number;
_description = description;
}
public Item()
{
}
public override string ToString()
{
return string.Format("Item number {0} Description {1}", _number, _description);
}
}
}
模型对象 - 存储项目集合,包括读取和写入文件的方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BasicDataStorageApp
{
public class Model
{
private Item[] _items;
public Item[] Items
{
get { return _items; }
set { _items = value; }
}
public bool WriteItems(string filename, bool append)
{
if (_items != null)
{
for (int i = 0; i < _items.Length; i++)
{
string str = _items[i].ToString();
FileHelper.WriteLine(str, filename, append);
}
return true;
}
return false;
}
public IEnumerable<string> ReadItems(string filename)
{
return FileHelper.ReadLines(filename);
}
}
}
FileHelper - 提供读写IO静态方法。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BasicDataStorageApp
{
public static class FileHelper
{
public static bool WriteLines(IEnumerable<string> lines, string filename, bool append)
{
try
{
using (StreamWriter writer = new StreamWriter(filename, append))
{
foreach (var line in lines)
{
writer.WriteLine(line);
}
}
return true;
}
catch (Exception ex)
{
throw ex;
}
}
public static bool WriteLine(string line, string filename, bool append)
{
try
{
using (StreamWriter writer = new StreamWriter(filename, append))
{
writer.WriteLine(line);
}
return true;
}
catch (Exception ex)
{
throw ex;
}
}
public static IEnumerable<string> ReadLines(string filename)
{
try
{
var lines = new List<string>();
using (StreamReader reader = new StreamReader(filename))
{
string line = null;
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
}
}
return lines;
}
catch (Exception ex)
{
throw ex;
}
}
}
}
程序 - 包括描述的逻辑以获取用户输入,将其写入文件并将其读回用户
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BasicDataStorageApp
{
class Program
{
static Model _model;
const int _totalInput = 10;
const string _filename = @"C:\temp\DataEntry.txt";
static void Main(string[] args)
{
_model = new Model();
_model.Items = new Item[_totalInput];
Console.WriteLine("This program is designed to take input and hold data for 10 items");
int i = 0;
while (i < _totalInput)
{
int number = -1;
Console.WriteLine("\nEnter number: ");
string numberValue = Console.ReadLine();
if (Int32.TryParse(numberValue, out number))
{
_model.Items[i] = new Item(number, null);
Console.WriteLine("\nEnter description: ");
string descriptionValue = Console.ReadLine();
_model.Items[i].Description = descriptionValue;
i++;
}
}
_model.WriteItems(_filename, true);
var itemStrings = _model.ReadItems(_filename);
foreach (var s in itemStrings)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
}