4

I am converting a VB6 project to C#. I have come across some VB6 that I don't totally understand (and I don't have the ability to debug at all). It is a loop which exits immediately, before doing anything:

For Each objSubFolder In objFolder.SubFolders Exit For Next 

Can anyone explain that? I'm sure it does something. I'm guessing it is assigning a variable, or something. If so, does it do it only once?

5
  • 3
    Is there a Dim objSubFolder As something before the For Each? Is there a objSubFolder used somewhere after the For Each? I don't remember much of VB6, but I was thinking that it might be a devious way to assign the first element in SubFolders to objSubFolder. Commented Mar 6, 2012 at 22:51
  • This was probably a stub that never got used. Commented Mar 6, 2012 at 23:21
  • @Paolo There is a objSubFolder declared before the loop (objSubFolder as folder) and it is used after the loop. My guess was the same as yours. A language-specific, devious assignment to that variable. Commented Mar 7, 2012 at 4:54
  • 2
    @Paolo Devious, yes. But also the only practical way, since accessing iterators explicitly in VB6 is a major pain in the *ss. Needless to say, this should have been properly documented in the source code. Commented Mar 7, 2012 at 10:19
  • @KonradRudolph sorry, I meant "devious" with the eye of somebody who has seen the last VB6 quite a few years ago. For all I know it could well be very idiomatic code and might have looked very obvious to me at the time I was using VB6. Commented Mar 7, 2012 at 10:38

3 Answers 3

6

If objSubFolder exists outside the scope of the For Each (as per you comment), the code would be roughly the equivalent of this c#

var folders = Directory.GetDirectories(@"c:\someFolder"); var firstFolder=folders.FirstOrDefault(); 

i.e. find the first subfolder of a given folder (if it exists).

Sign up to request clarification or add additional context in comments.

Comments

4

This code is mostly a complete no-op. The only potentially significant effect this has one the code is that it will execute the objFolder.SubFolders property or method. If this member has a note worthy side effect then this code could be significant.

It's the rough equivalent of the following C# code

object objSubFolder = null; using (var e = objFolder.SubFolders.GetEnumerator()) { if (e.MoveNext()) { objSubFolder = e.Current(); } } 

Note this is still not necessarily a 1-1 translation.

  • if objSubFolder was previously defined in the method this would incorrectly overwrite it's value on an empty folder collection
  • if the VB code had Option Explicit Off then you'd possibly need to convert objFolder.SubFolders to dynamic to get close to the same behavior.

2 Comments

Thanks Jared. I think it is assigning the first subdirectory object to the objSubFolder variable, which is declared as a folder earlier in the function.
It's absolutely not equivalent to that C# code. objSubFolder will still be in scope and available after the loop and will be assigned to the first subfolder (if there was one). Which is probably the point of the code. I think the C# equivalent would be object objSubFolder; objforeach (objSubFolder in objFolder.SubFolders) { break; } // objSubFolder is still in scope here and pointing to the first member
3

Exit For breaks out of the loop - basically C#'s break keyword. It does not look like this loop is doing anything practical..

1 Comment

It doesn’t look like it, but it does.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.