公共与朦胧之间的区别

时间:2017-01-13 05:39:14

标签: .net vb6

结果相同,但编码完全不同。有人可以解释这两者之间有什么不同吗?哪一个效果更好?

★★这是第一个使用Dim★★

的人
Option Explicit


Sub Main()

Dim fileNo_input As Integer
Dim fileNo_output As Integer
Dim buf As String

fileNo_input = FreeFile
Open "D:\text.txt" For Input As #fileNo_input

fileNo_output = FreeFile
Open "D:\output.txt" For Append As #fileNo_output

Do While Not EOF(fileNo_input)

    Input #fileNo_input, buf

    Print #fileNo_output, buf
Loop

Close #fileNo_input
Close #fileNo_output

End Sub

★★第二个是使用公共★★

Option Explicit

Public buf(10) As String    
Public line As Integer     

Sub Main()

FILE_READ ("D:\text.txt")

FILE_WRITE ("D:\output.txt")

End Sub


Function FILE_READ(INPUT_FILENAME As String)

Dim fileNo_input As Integer

fileNo_input = FreeFile
Open INPUT_FILENAME For Input As #fileNo_input

line = 0

Do While Not EOF(fileNo_input)
    Input #fileNo_input, buf(line)
    line = line + 1
Loop

Close #fileNo_input

End Function

Function FILE_WRITE(OUTPUT_FILENAME As String)

Dim fileNo_output As Integer
Dim i As Integer

fileNo_output = FreeFile
Open OUTPUT_FILENAME For Append As #fileNo_output

For i = 0 To line - 1
    Print #fileNo_output, buf(i)
Next

Close #fileNo_output

End Function

我想知道两者之间有什么不同,对我而言100%相同,因为结果完全相同。

1 个答案:

答案 0 :(得分:1)

Dim与Private相同,与Public不同。

当您使用DIM声明类级变量时,它仅对该类是私有的,您无法从该类外部访问它。

要使变量在类外可用,您需要Public说明符。

了解更多信息,请参阅

http://www.vbforums.com/showthread.php?468662-Classic-VB-What-is-the-difference-between-Dim-Private-Public-Global-Static-Const