elimiaw
elimiaw

Reputation: 1

Difference between Public and Dim

The result are same, but the coding is totally different. Could someone explain what is the different between these two? Which one works better?

★★ This is the first one using 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

★★ And the second one is using Public ★★

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

I wondered what is the different between both, for me its 100% same because the result are totally the same.

Upvotes: 0

Views: 159

Answers (1)

Thakur
Thakur

Reputation: 557

Dim is same as Private and not same as Public.

When you declare the class level variable using DIM, it is private to that class only, you cannot access it from outside the class.

To make variable available outside the class, you need Public specifier.

for more information please see

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

Upvotes: 1

Related Questions