1

How can i do this? (c#) Basically, i want to be able to sort of, comment out a bracket. but not really. I guess I want to close brackets out of order. That's probably not possible. I can obviously implement this in full separate if clauses, but this would considerably lighten my code.

P.S.: I am trying to place the "//do something" code, either in a foreach loop, or for a single instance with an unrelated (to that of the foreach loop) argument, depending on a condition. I hope this helps clear it up.

pseudocode (what i'm trying to do, if you could close brackets out of order):

im aware this is no where near valid code, it is pseudocode as i stated.

i havent looked at the second post yet, but the first post (Sean Bright), thank you, but the condition is unrelated to the number of entries (there will always be files in the directory, regardless of if i want the loop to execute)

extracting the //dosomething code into a function will work. I'm not sure how i overlooked that. I suppose I was wondering if there was a simpler way, but you are correct. thank you.

if (isTrue)
{
    //START LOOP
    string [] fileEntries = Directory.GetFiles(LogsDirectory, SystemLogFormat);
    foreach(string fileName in fileEntries)
    {
       using (StreamReader r = new StreamReader(fileName))
       {
    /* The previous two blocks are not closed */
}
else
{
    using (StreamReader r = new StreamReader(SingleFileName))
    {
    /* The previous block is not closed */
}

// do all sorts of things

if (isTrue)
{
    /* Close the two unclosed blocks above */
        }
    }
}
else
{
    /* Close the unclosed block above */
    }
}

thanks!

(indents are weird, sorry, its the forums doing)

5
  • 9
    Well, you've lost me Commented Aug 4, 2009 at 20:15
  • By "sort of, comment out a bracket," what do you mean? What exactly are you trying to get your code to do? I can tell you that "commenting out a bracket" or having mismatched brackets isn't going to work (since the compiler will choke), but we need more info to work around the issue you're having. Commented Aug 4, 2009 at 20:17
  • Is that pseudo code an example of what you're trying to do, or an example of the kind of code you want to parse? Commented Aug 4, 2009 at 20:17
  • I tried to edit the braces only to find out it's no where near syntactically valid code. You have an unmatched else as the first item in your using statement. Commented Aug 4, 2009 at 20:20
  • what are you trying to do? Reading all the files in a directory? Commented Aug 4, 2009 at 20:20

3 Answers 3

6

Refactor the file processing part into a separate function:

public void OriginalFunction() {
    if ( isTrue ) {
        string [] fileEntries = Directory.GetFiles(LogsDirectory, SystemLogFormat);
        foreach(string fileName in fileEntries) {
            ProcessFile(fileName);
        }
    } else {
        ProcessFile(SingleFileName);
    }

}

public void ProcessFile( string name ) {
    using (StreamReader r = new StreamReader(name))
    {
       // Do a bunch of stuff
    }
}
Sign up to request clarification or add additional context in comments.

Comments

6

Why not just do this:

string [] fileEntries = null;

if (isTrue) {
    fileEntries = Directory.GetFiles(LogsDirectory, SystemLogFormat);
} else {
    fileEntries = new string [] { SingleFileName };
}

foreach(string fileName in fileEntries) {
    using (StreamReader r = new StreamReader(fileName)) {
        /* Do whatever */
    }
}

1 Comment

+1 That's the approach I took to a similar problem just yesterday
0

Use your conditional to determine how you're going to create your StreamReader, then pass that in the using() like normal.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.