5
Answers

Confused about Private Sub, Public Sub etc.

Hi Folks,

Silly question, but I just can't seem to get it in my head.  I've designed a form with the standard set up...

Public Class Form1

Private Sub
End Sub

Private Sub
End Sub


End Sub

and I have a few buttons & text boxes in the Private Sub sections.

Now I want to add some code to read in a text file.  I can find examples of code all over the net so I'm ok there but where do I put it in relation to the above structure.

Every where I seem to paste it in just shows up loads of errors as though I need to create another special sub section to contain it.

If someone could point me in the right direction it would be most appreciated.

Regards,

Chris
Answers (5)
1
Mike Gold

Mike Gold

NA 32k 21.3m 14y

Hi Chris,
If you want to read in the text file immediately upon creation of the form, you can do this in the constructor
Public Class Form1 
 
   
Public Sub New() 
 
       
' This call is required by the Windows Form Designer. 
        InitializeComponent() 
 
        '
Add any initialization after the InitializeComponent() call. 
 
   
End Sub 
 
or you can do so in the form load event.  (Go to the properties tab and choose the lightening bolt.  Double click on Load and it will generate a Form_Load event handler for you)

 
End Class 
0
Mike Gold

Mike Gold

NA 32k 21.3m 14y

for an array it makes more sense to use a for loop
http://weblogs.asp.net/eporter/archive/2003/08/07/22943.aspx
0
Chris Jefferies

Chris Jefferies

NA 5 2k 14y

Thanks Mike,
That's what I was stuck on.  I'm up and running now.
Now I have another question if I may.
How do I do a Do While loop for an array.  etc.
Do While TempArray() is not empty
 
Thanks,
Chris
 
Regards,

Chris
0
Chris Jefferies

Chris Jefferies

NA 5 2k 14y

Hi Mike,
Thanks for the reply.
I'm not explaining it very well I'm afraid.
Basically I don't want to have to click a button for the text file read to happen.  I want it to read in the file in the back ground.  I'm steadily following examples in learning sites but they all seem to require a button to start a process.
Having said all that I think I've figured it out.  Instead of right clicking on a button and putting the code in the relevent section, I right clicked on the whole form1 and it's created a new Private Sub.  I've put my code in there and it seems to run it without any button presses.  I presume that's how to do it.
Appears so mind boggling when you don't know it :)
0
Mike Gold

Mike Gold

NA 32k 21.3m 14y

Hi Chris,
Not sure what you are asking.  Private and Public are encapsulation keywords.  Here is some sample code in Vb to read a file into a string selected from the open file dialog.  You'll need to drag a button on the form as well as the OpenFileDialog control.  Double clicking on the button will generate the event handler you see below. 
 
Imports
System.IO
Public
Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sr As StreamReader
Dim fileContent As String
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
sr =
New StreamReader(OpenFileDialog1.FileName)
fileContent = sr.ReadToEnd()
End If
End Sub
End
Class