C#将数组或列表添加到列表中
我有一份文件清单C#将数组或列表添加到列表中,c#,arrays,list,C#,Arrays,List,我有一份文件清单 public class Document { public string[] fullFilePath; public bool isPatch; public string destPath; public Document() { } public Document(string[] fullFilePath, bool isPatch, string destPath) { this.fullFile
public class Document
{
public string[] fullFilePath;
public bool isPatch;
public string destPath;
public Document() { }
public Document(string[] fullFilePath, bool isPatch, string destPath)
{
this.fullFilePath = fullFilePath;
this.isPatch = isPatch;
this.destPath = destPath;
}
fullFilepath应该是一个路径列表或路径数组
例如:
Document 1
---> C:\1.pdf
---> C:\2.pdf
Document 2
---> C:\1.pdf
---> C:\2.pdf
---> C:\3.pdf
等等
我的问题是,如果我使用的是数组字符串,则所有文档的fullFilePath都为“null”。
如果我对fullFilePath使用列表,那么所有文档都会从上一个文档中获得相同的条目
以下是列表的填写方式:
int docCount = -1;
int i = 0;
List<Document> Documents = new List<Document>();
string[] sourceFiles = new string[1];
foreach (string file in filesCollected)
{
string bc;
string bcValue;
if (Settings.Default.barcodeEngine == "Leadtools")
{
bc = BarcodeReader.ReadBarcodeSymbology(file);
bcValue = "PatchCode";
}
else
{
bc = BarcodeReader.ReadBacrodes(file);
bcValue = "009";
}
if (bc == bcValue)
{
if(Documents.Count > 0)
{
Array.Clear(sourceFiles, 0, sourceFiles.Length);
Array.Resize<string>(ref sourceFiles, 1);
i = 0;
}
sourceFiles[i] = file ;
i++;
Array.Resize<string>(ref sourceFiles, i + 1);
Documents.Add(new Document(sourceFiles, true,""));
docCount++;
}
else
{
if (Documents.Count > 0)
{
sourceFiles[i] = file;
i++;
Array.Resize<string>(ref sourceFiles, i + 1);
Documents[docCount].fullFilePath = sourceFiles;
}
}
}
intdoccount=-1;
int i=0;
列表文档=新列表();
string[]sourceFiles=新字符串[1];
foreach(收集的文件中的字符串文件)
{
字符串bc;
字符串值;
如果(Settings.Default.barcodeEngine==“Leadtools”)
{
bc=条形码阅读器.ReadBarcodeSymbology(文件);
bcValue=“补丁代码”;
}
其他的
{
bc=barcodereder.ReadBacrodes(文件);
bcValue=“009”;
}
if(bc==bcValue)
{
如果(Documents.Count>0)
{
Array.Clear(sourceFiles,0,sourceFiles.Length);
Array.Resize(ref sourceFiles,1);
i=0;
}
sourceFiles[i]=文件;
i++;
Array.Resize(ref sourceFiles,i+1);
Documents.Add(新文档(sourceFiles,true,“”);
docCount++;
}
其他的
{
如果(Documents.Count>0)
{
sourceFiles[i]=文件;
i++;
Array.Resize(ref sourceFiles,i+1);
文档[docCount].fullFilePath=sourceFiles;
}
}
}
问题应该在这里:
string[] sourceFiles = new string[1];
如果在foreach中移动这行代码,则应该解决此问题,因为在foreach中始终使用相同的变量,因此引用相同
int docCount = -1;
int i = 0;
List<Document> Documents = new List<Document>();
foreach (string file in filesCollected)
{
string[] sourceFiles = new string[1];
string bc;
string bcValue;
if (Settings.Default.barcodeEngine == "Leadtools")
{
bc = BarcodeReader.ReadBarcodeSymbology(file);
bcValue = "PatchCode";
}
else
{
bc = BarcodeReader.ReadBacrodes(file);
bcValue = "009";
}
if (bc == bcValue)
{
if(Documents.Count > 0)
{
Array.Clear(sourceFiles, 0, sourceFiles.Length);
Array.Resize<string>(ref sourceFiles, 1);
i = 0;
}
sourceFiles[i] = file ;
i++;
Array.Resize<string>(ref sourceFiles, i + 1);
Documents.Add(new Document(sourceFiles, true,""));
docCount++;
}
else
{
if (Documents.Count > 0)
{
sourceFiles[i] = file;
i++;
Array.Resize<string>(ref sourceFiles, i + 1);
Documents[docCount].fullFilePath = sourceFiles;
}
}
}
intdoccount=-1;
int i=0;
列表文档=新列表();
foreach(收集的文件中的字符串文件)
{
string[]sourceFiles=新字符串[1];
字符串bc;
字符串值;
如果(Settings.Default.barcodeEngine==“Leadtools”)
{
bc=条形码阅读器.ReadBarcodeSymbology(文件);
bcValue=“补丁代码”;
}
其他的
{
bc=barcodereder.ReadBacrodes(文件);
bcValue=“009”;
}
if(bc==bcValue)
{
如果(Documents.Count>0)
{
Array.Clear(sourceFiles,0,sourceFiles.Length);
Array.Resize(ref sourceFiles,1);
i=0;
}
sourceFiles[i]=文件;
i++;
Array.Resize(ref sourceFiles,i+1);
Documents.Add(新文档(sourceFiles,true,“”);
docCount++;
}
其他的
{
如果(Documents.Count>0)
{
sourceFiles[i]=文件;
i++;
Array.Resize(ref sourceFiles,i+1);
文档[docCount].fullFilePath=sourceFiles;
}
}
}
每个文档都使用相同的数组实例。实例在每个内部循环中都会更新一个新的文件列表,但是数组是对内存区域的引用(我知道,为了这个答案,过于简单就足够了),如果您更改内存区域的内容,那么您将为每个文档更改它
您需要为添加到文档列表中的每个新文档创建源文件的新实例。此外,当您不确定要包含在数组中的元素数量时,最好使用通用列表并删除所有处理数组大小调整的代码
首先更改类定义
public class Document
{
public List<string> fullFilePath;
public bool isPatch;
public string destPath;
public Document() { }
public Document(List<string> fullFilePath, bool isPatch, string destPath)
{
this.fullFilePath = fullFilePath;
this.isPatch = isPatch;
this.destPath = destPath;
}
}
当然,现在在调用代码时,只传递新文件或调用AddFile,而无需检查列表初始化。您对每个文档都使用相同的sourcefiles实例。通常,您会得到相同的数据。你放在阵列中的最后一组既然你正在调整它的大小,为什么还要在这里使用
阵列
?我投了你的票!列表比数组更具动态性:-)
foreach (string file in filesCollected)
{
string bc;
string bcValue;
....
if (bc == bcValue)
{
List<string> files = new List<string>();
files.Add(file);
Documents.Add(new Document(files, true, ""));
docCount++;
}
else
Documents[docCount].fullFilePath.Add(file);
}
public class Document
{
public List<string> fullFilePath;
public bool isPatch;
public string destPath;
public Document()
{
// Every constructory initializes internally the List
fullFilePath = new List<string>();
}
public Document(string aFile, bool isPatch, string destPath)
{
// Every constructory initializes internally the List
fullFilePath = new List<string>();
this.fullFilePath.Add(aFile);
this.isPatch = isPatch;
this.destPath = destPath;
}
public void AddFile(string aFile)
{
this.fullFilePath.Add(aFile);
}
}