- The smallest unit of "writable data" is a byte.
- Drop that
goto (and please don't use it ever again, for anything). Instead: Toggling a boolean variable is as easy as this:
tif = !tif;
- Doesn't help a lot here though because the change will not reflect in
buffer. After you create
buffer it keeps its value forever.
- You're using the third parameter of the
Stream.Write-method wrongly - at least judging from your described intent. It's supposed to be the amount of bytes from
buffer, starting at position 0, that should be written into the file stream. But
buffer is only 1 byte long and
bits says there should be 32 bytes. That's where you are "outside of the array bounds".
while-loop will run forever because
bits will be > 0 forever.
You should start using the debugger. Place a breakpoint at the start of your method (first line or opening brace) by placing the cursor there and pressing F9. Then start your program in debug mode by pressing F5. Then step over the execution line-by-line by pressing F10
* while you inspect the values of your variables by hovering with the mouse cursor over it. Compare the values you expect with their actual values and when there's a difference try to find out, why.
*: Take a look at further ways of stepping through your code while debugging: In the Debug-Menu and/or the Debugging-Toolbar (and the Debugger-documentation).
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson