这里是新人,所以请多多关照!!当谷歌让我失望时,我在学习编程时获得了很多乐趣,并在此过程中得到了一些很大的帮助。但很可惜,我又被困住了。
我有一个看起来像这样的 C# 程序(如果有人熟悉,那就是 MWS)
我已经尝试了很多不同的方法来让它有效地循环遍历文本文件中的值列表。我遇到的问题是 Main 函数是我必须设置循环的地方,但 BuildClass 是我需要循环遍历文本文件(哨兵)中的值的地方。我已经包含了一些可能没有必要的东西,以防万一它弄乱了我的代码而我没有意识到。
这是我尝试过的:
- 在 BuildClass 中设置循环 - 没想到它会起作用,但它在到达哨兵之前抛出了异常。
- 通过将主函数哨兵中的“使用”或“变量”更改为公共来引用主函数中的哨兵 - 在视觉工作室中变成红色
- 将字符串 sentinel 移到主函数之外,以便函数和 BuildClass 能够识别它 - 主函数不再识别它。
- 我尝试了很多其他事情都没有成功。我已经让它循环使用从 BuildClass 传递给函数的相同标记值,但仅此而已。
我认为我需要什么:
- streamReader 的破坏性版本,它会在读取文本文件时从文本文件中删除值。我将这个放在BuildClass里面,这样主函数的下一个循环,下一个值会被读取并传入主函数,直到文件为空,终止循环。
- 了解为什么将哨兵更改为公开会如此严重地破坏代码。我对为什么其他尝试不起作用有一个很好的理解。
namespace MainSpace
{
public class MainClass
{
int i;
public static void Main(string[] args)
{
ClientClass client = new ClientInterface(appName, appVersion, password, config);
MainClass sample = new MainClass(client);
string sentinel;
using (var streamReader = new StreamReader(@"sample.txt", true))
while((sentinel = streamReader.ReadLine()) != null)
{
try
{
//stuff
response = sample.InvokeBuild();
Console.WriteLine("Response Stuff");
string responseXml = response.ToXML();
Console.WriteLine(responseXml);
StreamWriter FileWrite = new StreamWriter("FileTest.xml", true);
FileWrite.WriteLine(responseXml);
FileWrite.Close();
}
catch (ExceptionsClass)
{
// Exception stuff
throw ex;
}
}
}
private readonly ClientInterface client;
public MainClass(ClientInterface client)
{
this.client = client;
}
public BuildClass InvokeBuild()
{
{
using (var streamReader = new StreamReader("sample.txt", true))
{
string sentinel = streamReader.ReadLine();
Thread.Sleep(6000);
i++;
Console.WriteLine("attempt " + i);
// Create a request.
RequestClass request = new RequestClass();
//Password Stuff
request.IdType = idType;
IdListType idList = new IdListType();
idList.Id.Add(sentinel);
request.IdList = idList;
return this.client.RequestClass(request);
}
}
}
}