Working on Code from a VB Book; Help Interpreting Errors

R

ryguy7272

I found the example of code in a book:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim drives() As String
drives = System.IO.Directory.GetLogicalDrives
Dim iDrive As Integer
DrivesList.Items.Clear()
On Error Resume Next
For iDrive = 0 To drives.GetUpperBound(0)
DrivesList.Items.Add(drives(iDrive))
Next
DrivesList.SelectedIndex = 1
End Sub


Private Sub DrivesList_SelectedIndexChanged(ByVal sender As
System.Object, _
ByVal e As System.EventArgs) Handles DrivesList.SelectedIndexChanged
Dim directories() As String
Try
directories = System.IO.Directory.GetDirectories(DrivesList.Text)
Catch drvException As Exception
MsgBox(drvException.Message)
Exit Sub
End Try
Dim dir As String
FoldersList.Items.Clear()
For Each dir In directories
FoldersList.Items.Add(dir)
Next
FilesList.Items.Clear()
End Sub

Private Sub FoldersList_DoubleClick(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles FoldersList.DoubleClick
Dim selDir As String
Dim Directory As IO.Directory
Dim parentDir As IO.Directory
selDir = FoldersList.Text
Dim dirs(), files() As String
If selDir = ".." Then
dirs = IO.Directory.GetDirectories(parentDir)
files = IO.Directory.GetFiles(parentDir)
Try
parentDir = IO.Directory.GetParent(parentDir).FullName
Catch exc As Exception
parentDir = Nothing
End Try
Else
dirs = IO.Directory.GetDirectories(selDir)
files = IO.Directory.GetFiles(selDir)
Try
parentDir = IO.Directory.GetParent(selDir).FullName
Catch exc As Exception
parentDir = Nothing
End Try
End If
Dim dir As String
FoldersList.Items.Clear()
If Not parentDir Is Nothing Then
FoldersList.Items.Add("..")
End If
For Each dir In dirs
FoldersList.Items.Add(dir)
Next
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
FilesList.Items.Clear()
If Button1.Text = "Scan Selected Folder" Then
Button1.Text = "Stop Scanning"
interrupt = False
Else
Button1.Text = "Scan Selected Folder"
interrupt = True
Me.Text = "INTERRUPTED"
End If
Try
If FoldersList.Text = "" Then
ScanFolder(DrivesList.Text)
Else
ScanFolder(FoldersList.Text)
End If
Catch scanException As Exception
MsgBox(scanException.Message)
End Try
Button1.Text = "Scan Selected Folder"
End Sub


Sub ScanFolder(ByVal currDir As String)
'If interrupt Then Exit Sub
Dim Dir As String
Dim File As String
Dim FI As FileInfo
Dim countFiles As Integer
Dim countFolders As Integer
Label1.Text = "scanning " & currDir
For Each File In Directory.GetFiles(currDir)
FI = New FileInfo(File)
FilesList.Items.Add(FI.Name & vbTab & FI.Length & vbTab & _
FI.CreationTime)
Next
countFiles += Directory.GetFiles(currDir).Length
Me.Text = "Scanning " & FoldersList.Text & " — Processed " &
countFiles & _
" files in " & countFolders & " folders..."
For Each Dir In Directory.GetDirectories(currDir)
countFolders += 1
Application.DoEvents()
ScanFolder(Dir)
Next
End Sub

End Class

I added the appropriate controls to the Form. I seem to be missing
something though, because I have squiggle lines under the following:
#1)
parentDir

#2)
IO.Directory.GetParent(selDir).FullName

#3)
Interrupt

#4)
FileInfo

#5)
Directory

A squiggle line means VB can’t interpret what you mean, right. What is the
procedure for debuggin this? How can I tell what the problem is? How can I
tell what VB wants?

For these:
parentDir
IO.Directory.GetParent(selDir).FullName

I mouse-over and see a tooltip that reads ‘value of type ‘String’ cannot be
converted to System.IO.Directory’’

What does that mean?

For this:
Interrupt
Directory

The tooltip reads ‘name not declared’
What does it mean?

Finally, for this:
FileInfo

Tooltip reads ‘Type ‘FileInfo’ is not defined’
Again, I’m not sure what this means.

I’d love to figure out what these errors mean and I’d love to get this
working. Can anyone here decipher this stuff doe me?

Thanks so much!
Ryan---
 
A

Armin Zingler

ryguy7272 said:
I found the example of code in a book:
Application.DoEvents()

If this is the way to teach people how to do things, then forget the book.
I added the appropriate controls to the Form. I seem to be missing
something though, because I have squiggle lines under the following:


Make the following changes:

1. Add

Imports System.IO

add the top of the file.

2. Declare

private parentDir As String

as a field of the class (straight after "class Form1")

3. Delete the declarations of variables "Directory" and "ParentDir"
from Sub FoldersList_DoubleClick.

4. Declare variable interrupt As Boolean inside Sub Button1_Click.
However, currently, it's not clear what the variable is used for.
The assignment itself doesn't make sense. If it's used somewhere
else too, you'd have to declare it as a field, too.
 
A

Alex Clark

Ryan,

What book is this?

The code appears to be a badly translated effort from VB6. In VB.NET,
Application.DoEvents and On Error are deprecated, not recommended, and IMO
very poor coding practice.

Regards,
Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top