0

I have a project I am working on which runs a process if a value is identified in a file

if (read_txt.Contains("one") == true)
   (do.something)
else if ((read_txt.Contains("two") == true)
   (do.something.else)
else
   (do.last.thing)

The (do.something) and (do.something.else) contains lots of things, like processes, if statements etc.

for example, (do.something) contains;

if (read_txt.Contains("one") == true)
   write_log
   process
   read_file
   if file.contains
        write_log
   else
        write_log
        process
   process
   if file.contains
        write_log
   else
        write_log

The issue I have is that if the file in 'read_txt' contains both "one" and "two", i want to be able to run both elements, (do.something) and (do.something.else), without copying the code out again as there is quite a bit.

What would be the best way to do this?

I am a beginner with C# but this project is helping me learn quite quickly!!

4

3 回答 3

2

我遇到的问题是,如果“read_txt”中的文件同时包含“一个”和“两个”,我希望能够运行两个元素(do.something)和(do.something.else),而无需复制再次编码,因为有很多。

这很容易,只是不要让它成为一个else if,只要有两个if语句。

bool foundNone = true;

if(read_txt.Contains("one"))
{
    DoFirstThing();
    foundNone = false;
}

if(read_txt.Contains("two"))
{
    DoSecondThing();
    foundNone = false;
}

if(foundNone)
{
   DoThirdThing();
}

这意味着它将为找到的每个值运行代码,并且在找到一个值时不会停止,但如果没有其他选项被命中,它仍然只做最后一件事。

于 2013-08-30T15:00:36.803 回答
1
bool not12 = true;
if (read_txt.Contains("one")) { (do.something); not12 = false;}
if (read_txt.Contains("two")) {(do.something.else); not12 = false;}   
if(not12) (do.last.thing);
于 2013-08-30T14:58:35.640 回答
0

从 (do.something) 中编写一个函数和/或让您的第一个 if 语句同时检查两者是否为真,例如:

if(read_txt.Contains("one") && read_txt.Contains("two"))
于 2013-08-30T15:00:51.637 回答