我正在處理我的第一個真正的 c# 專案,但我在基于類創建 List 的方式上遇到了問題,我不知道如何解決。
我正在嘗試撰寫一些代碼,該代碼將具有多個層的多個構造的輸入檔案(txt/csv)放入我的程式中,然后將這些構造寫入新的 txt/csv 檔案中。
當層數相同時,它作業正常。但是當構造具有不同數量的層時,它會引起麻煩,并且我得到“System.IndexOutOfRangeException”。我的問題是:我可以讓我的串列所基于的類是動態的(我不知道它是否是技術術語),以便它可以處理不同數量的輸入?在將構造添加到程式時以及將其寫入新檔案時?
我的代碼是:
class Program
{
static void Main(string[] args)
{
// Filepath for the input and output file
string filePathIn_constructions = @"C:\Library\Constructions.txt";
string filePathOut = @"C:\Library\EPlus_Inputfile.txt";
// Creating a list of constructions based on the class. The list is made from the file "filePathIn_constructions"
List<Construction> allConstructions = new List<Construction>();
List<string> lines_constructions = File.ReadAllLines(filePathIn_constructions).ToList(); // add it to a list
// Adding all the data from the fil to the variable "allConstructions"
foreach (var line in lines_constructions)
{
string[] entries = line.Split(',');
Construction newConstruction = new Construction();
newConstruction.EIndex = entries[0];
newConstruction.Name = entries[1];
newConstruction.Layer1 = entries[2];
newConstruction.Layer2 = entries[3];
newConstruction.Layer3 = entries[4];
newConstruction.Layer4 = entries[5];
newConstruction.Layer5 = entries[6];
allConstructions.Add(newConstruction); // Add it to our list of constructions
}
List<string> output = new List<string>();
foreach (var x in allConstructions) // Printing the new
{
output.Add($"{x.EIndex}, {x.Name}, {x.Layer1}, {x.Layer2}, {x.Layer3}, {x.Layer4}, {x.Layer5}");
}
File.WriteAllLines(txtFilePathOut, output);
}
}
我的建筑類是
public class Construction
{
public string EIndex { get; set; }
public string Name { get; set; }
public string Layer1 { get; set; }
public string Layer2 { get; set; }
public string Layer3 { get; set; }
public string Layer4 { get; set; }
public string Layer5 { get; set; }
}
輸入/輸出檔案的一個例子可能是
Construction,ConcreteWall,Concrete;
Construction,Brickwall1,Birck,Isulation,Brick;
Construction,Brickwall2,Birck,AirGap,Isulation,Brick;
Construction,Wood/Concrete Wall,Wood,Isulation,Concrete,Gypson;
Construction,Wood Wall,Wood,AirGap,Gypson,Isulaiton,Gypson;
我希望有人能幫幫忙。謝謝。
編輯:我必須能夠單獨超過構造名稱,因為我正在使用它來進行一些排序。
uj5u.com熱心網友回復:
public class Construction
{
public string EIndex { get; set; }
public string Name { get; set; }
public List<string> Layers { get; set; } = new List<string>();
}
foreach (var line in lines_constructions)
{
string[] entries = line.Split(',');
Construction newConstruction = new Construction();
newConstruction.EIndex = entries[0];
newConstruction.Name = entries[1];
for (int i=2; i < entries.Length; i ) {
newConstruction.Layers.Add(entries[i]);
}
allConstructions.Add(newConstruction);
}
foreach(var x in allConstuctions) {
File.AppendAllText(output, $"{x.EIndex}, {x.Name}, {string.Join(", ", x.Layers)}");
}
uj5u.com熱心網友回復:
這是因為您正在嘗試訪問不存在的陣列單元格(檔案)
在您的輸入/輸出檔案中,您有包含 3 到 7 個值的行,并且您正在使用entries這些值構建一個陣列。這意味著您將擁有 3 到 7 個單元格的陣列
問題是,在創建這些陣列之后,您嘗試訪問每個陣列的單元格 0、1、2...直到第 7 個,即使對于只有 3 個單元格的陣列!
您可以通過一種簡單的方式來解決此問題,即添加列以在每行上具有相同數量的分隔符(您將行的分隔符定義為帶有 的列line.Split(','))。這樣,您將創建的每個陣列將始終有 7 個單元格,即使其中的值為 null
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367477.html
