What does a hash do to a variable in VB?

那年仲夏 提交于 2019-12-02 14:13:27

问题


I have to refactor a VB6 program to C# and am stuck at understanding the following lines:

Set myFileSystemObject = New FileSystemObject
Set myTextStream = myFileSystemObject.OpenTextFile("myTextFile.txt")
Open sPrinterPort For Output As iFileNumber
Print #iFileNumber, myTextStream.ReadAll
Close #iFileNumber

I do know what's generally happening, but as I'm not used to the VB syntax, I'd like to know exactly what

Print #iFileNumber, myTextStream.ReadAll

does. And more specifically, what the # in front of iFileNumber does. Why is it there? Wouldn't the variable itself suffice to print on the stream?

This is merely for understanding exactly what's happening in the code.


回答1:


Print #iFileNumber, myTextStream.ReadAll prints the string returned by ReadAll into the file opened by number iFileNumber (and because there is no semicolon after the statement, it also adds vbNewLine in the end.)

The # (for "number") is there since the old times. VB6 just supports it. It does nothing execution wise. It used to assist readability and make the language more natural-like. Speak out loud:

Open "1.txt" For Input As 1

vs.

Open "1.txt" For Input As #1



回答2:


Print #iFileNumber, myTextStream.ReadAll

While trying to understand this myself, I came across this site that has a section on printing to a printer. They say #some_integer indicates a channel number:

A channel number is any integer value between 0 and 999, preceded by a pound sign (#); it indi- cates a specific channel to a device.

A channel is a connection between your program and an input or output device, such as a printer or a file.



来源:https://stackoverflow.com/questions/13701599/what-does-a-hash-do-to-a-variable-in-vb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!