私はC#で基本的なデータストレージプログラムを作成しています。私はかなり新しいです、私に簡単に行ってください。私はこれを2つのクラスに分割して、他の人が自分のメインメソッドからそれを実行できるようにしたいと思います。私の問題は、どこから始めたらいいのか分かりません。私はメソッドの別の.csファイルを追加しようとしましたが、配列への参照などでプログラムのエラーが発生します。ここに私が持っているものがあります。プログラムを2つのクラスに分割する
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();
}
}
}
私は何ができるか見てみましょうにそれを読むことを説明したロジックが含まれています。私に数分を与えてください。 –
あなたのコードを整理しただけでは何も得られません。代わりに、これを試してください。どこにいても「ここで私はやっているよ」と感じる。それは新しい考えであることを意味する。それを方法に分解してください。使用しているvarsを渡し、使用する結果値を返します。 *このクラスのインスタンスを作成していないため、静的メソッド名の前に置きます。 – FirebladeDan
ダンにありがとう、私は間違いなく答えを求めていません。私はちょうどどこから始めるべきかわからない、助けに感謝する。 – mattp341