C# 将程序分为两类
我正在用C#创建一个基本数据存储程序。我是新来的,请对我宽容一点。我想把它分成两个类,这样其他人就可以从他们自己的主方法运行它。我的问题是,我不知道从哪里开始。我尝试为这些方法添加另一个.cs文件,但对数组的引用等会在程序中产生错误。这是我的C# 将程序分为两类,c#,C#,我正在用C#创建一个基本数据存储程序。我是新来的,请对我宽容一点。我想把它分成两个类,这样其他人就可以从他们自己的主方法运行它。我的问题是,我不知道从哪里开始。我尝试为这些方法添加另一个.cs文件,但对数组的引用等会在程序中产生错误。这是我的 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Basic_Item_Entry
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("This program is designed to take input and hold data for 10 items");
//make array for item #'s and add 10 values
Console.WriteLine("Enter 10 item numbers");
int[] itemNums = new int[10];
for(int i = 0; i <itemNums.Length; i++)
{
itemNums[i] = Convert.ToInt32(Console.ReadLine());
}
//make array for item descriptions
string[] itemDesc = new string[10];
for(int i = 0; i < itemNums.Length; i++)
{
Console.WriteLine("Enter the description for item number: " + itemNums[i]);
itemDesc[i] = Console.ReadLine();
}
//add contents of arrays to a file @"C:\temp\DataEntry.txt"
using (System.IO.StreamWriter file = new System.IO.StreamWriter(
@"C:\temp\DataEntry.txt"))
{
file.WriteLine("Item Data:");
for (int i = 0; i < itemNums.Length; i++)
{
file.WriteLine("Item number " + itemNums[i] + " Description: " + itemDesc[i]);
}
file.Close();
}
//finish and maybe print contents from file
Console.WriteLine("Data has been recorded to a file. Would you like to view the the contents? y/n");
//print array data from previously written to file @"C:\temp\DataEntry.txt"
try
{
if (Console.ReadLine().Equals("y"))
{
using (StreamReader stringRead = new StreamReader(@"C:\temp\DataEntry.txt"))
{
String DataEntryTXT = stringRead.ReadToEnd();
Console.WriteLine(DataEntryTXT);
}
}
//dont print anything, just exit (Still creates the file)
else
{
System.Environment.Exit(1);
}
}
catch(Exception ex)
{
Console.WriteLine("File not found");
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
命名空间基本\u项\u项
{
公共课程
{
静态void Main(字符串[]参数)
{
Console.WriteLine(“该程序设计用于获取输入并保存10项数据”);
//为项目#创建数组并添加10个值
Console.WriteLine(“输入10个项目编号”);
int[]itemNums=新int[10];
for(int i=0;iItem对象-存储项的实例(编号、说明)
模型对象-存储项的集合,包括读取和写入文件的方法
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);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间BasicDataStorageApp
{
公共类模型
{
私人物品[]\u物品;
公共项目[]项目
{
获取{return\u items;}
设置{u items=value;}
}
公共bool WriteItems(字符串文件名,bool附加)
{
如果(_items!=null)
{
对于(int i=0;i<\u items.Length;i++)
{
字符串str=_items[i].ToString();
WriteLine(str,filename,append);
}
返回true;
}
返回false;
}
公共IEnumerable可读项(字符串文件名)
{
返回FileHelper.ReadLines(文件名);
}
}
}
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;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间BasicDataStorageApp
{
公共静态类FileHelper
{
公共静态bool WriteLines(IEnumerable行、字符串文件名、bool append)
{
尝试
{
使用(StreamWriter=newstreamwriter(文件名,追加))
{
foreach(行中的var行)
{
writer.WriteLine(行);
}
}
返回true;
}
捕获(例外情况除外)
{
掷骰子;
}
}
公共静态bool WriteLine(字符串行、字符串文件名、bool追加)
{
尝试
{
使用(StreamWriter=newstreamwriter(文件名,追加))
{
writer.WriteLine(行);
}
返回true;
}
捕获(例外情况除外)
{
掷骰子;
}
}
公共静态IEnumerable可读行(字符串文件名)
{
尝试
{
变量行=新列表();
使用(StreamReader=新StreamReader(文件名))
{
字符串行=null;
而((line=reader.ReadLine())!=null)
{
行。添加(行);
}
}
回流线;
}
捕获(例外情况除外)
{
掷骰子;
}
}
}
}
程序-包括所描述的逻辑,以获取用户输入,将其写入文件并读回给用户
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间BasicDataStorageApp
{
班级计划
{
静态模型&u模型;
const int _totalInput=10;
常量字符串\u文件名=@“C:\temp\DataEntry.txt”;
静态void Main(字符串[]参数)
{
_模型=新模型();
_model.Items=新项[_totalInput];
Console.WriteLine(“该程序设计用于获取输入并保存10项数据”);
int i=0;
而(i<\u总输入)
{
整数=-1;
Console.WriteLine(\输入编号:);
字符串numberValue=Console.ReadLine();
if(Int32.TryParse(numberValue,out number))
{
_model.Items[i]=新项目(编号,空);
Console.WriteLine(\n输入说明:);
string descriptionValue=Console.ReadLine();
_model.Items[i].Description=descriptionValue;
i++;
}
}
_model.WriteItems(_filename,true);
var itemStrings=_model.ReadItems(_filename);
foreach(itemStrings中的变量s)
{
控制台。写入线(s);
}
Console.ReadLine();
}
}
}
让我看看我能做些什么。给我几分钟时间。如果我只是为你清理了你的代码,那将一无所获。相反,试试这个。无论你想在哪里“我做废话”这意味着这是一个新的想法。将其分解为一个方法。传入您正在使用的变量并返回要使用的结果值。*将static放在方法名称前面,因为您尚未创建此类的实例。谢谢Dan,我肯定不只是想要答案。我只是不知道从哪里开始,我感谢您的帮助。
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();
}
}
}