Guest pritesh_b Posted September 24, 2002 Posted September 24, 2002 :confused: When I try to add the following to : Public Shared Sub ProcessFile(ByVal path As String) Dim i As Integer For i = 0 To 100000 _files(i) = path Next End Sub 'ProcessFile ReadOnly Property speed() Get Return _files() End Get End Property I get the following error: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. I have found some code which is exactly what I need but I don't know how to get a return value i.e. add the filenames to a variable. Can some one please explain to me how this is done? Thanks Here is the code I found: ' For Directory.GetFiles and Directory.GetDirectories ' For File.Exists, Directory.Exists Imports System Imports System.IO Imports System.Collections ' Takes an array of file names or directory names on the command line. ' Determines what kind of name it is and processes it appropriately Public Class RecursiveFileProcessor Dim _fileLocations As String 'Entry point which delegates to C-style main function Public Overloads Shared Sub Main() 'Main(System.Environment.GetCommandLineArgs()) 'Dim path = "M:\static\core\ssi\V3.3.4" End Sub Public Overloads Shared Sub Main(ByVal args() As String) Dim path As String For Each path In args If File.Exists(path) Then ' This path is a file ProcessFile(path) Else If Directory.Exists(path) Then ' This path is a directory ProcessDirectory(path) Else Console.WriteLine("{0} is not a valid file or directory.", path) End If End If Next path End Sub 'Main ' Process all files in the directory passed in, and recurse on any directories ' that are found to process the files they contain Public Shared Sub ProcessDirectory(ByVal targetDirectory As String) Dim fileEntries As String() = Directory.GetFiles(targetDirectory) ' Process the list of files found in the directory Dim fileName As String For Each fileName In fileEntries ProcessFile(fileName) Next fileName Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory) ' Recurse into subdirectories of this directory Dim subdirectory As String For Each subdirectory In subdirectoryEntries ProcessDirectory(subdirectory) Next subdirectory End Sub 'ProcessDirectory ' Real logic for processing found files would go here. Public Shared Sub ProcessFile(ByVal path As String) Console.WriteLine("Processed file '{0}'.", path) End Sub 'ProcessFile End Class 'RecursiveFileProcessor --------------------------------------------------- Pritesh Quote
*Gurus* divil Posted September 24, 2002 *Gurus* Posted September 24, 2002 Your processfile sub is shared. If you remove the shared keyword, it should work, but I don't know why you made it shared in the first place. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Guest pritesh_b Posted September 24, 2002 Posted September 24, 2002 Hi Thank you for your reply and that seems to sorted that problem out. The reason why i did this was because i used an example from the MSDN site and i very new to VB.NET. how ever i now receive another error : An unhandled exception of type 'System.NullReferenceException' occurred in RecursiveFiles.exe Additional information: Object reference not set to an instance of an object. I have attached the vb.class code but this exception happens here : Public Sub ProcessFile(ByVal path As String) Dim i As Integer Do While path <> "" _files(i) = path '****** EXCEPTION******* i = 1 + i Loop the code i use from my application to invoke this: Dim path As String = "D:\workarea\directoryInfo" Dim File() As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myRecur As New RecursiveFileProcessor() myRecur.ProcessFile(path) 'File(1) = myRecur.FilesReturn MsgBox(myRecur.FilesReturn.ToString) End Sub Quote
*Gurus* Derek Stone Posted September 24, 2002 *Gurus* Posted September 24, 2002 How is _files being declared? Are you redimensioning it to accomodate the numerous array elements? Quote Posting Guidelines
Guest pritesh_b Posted September 25, 2002 Posted September 25, 2002 Thank you for your reply. I have now managed to get the list into a arrayList which i can easily add to the listbox. However is there a way to add each item of the arraylist into a string which i can then use to check for values. I tried this code but it doesn't work: Heres my code: Dim i As Integer Dim uppListValue As Integer Dim strMyItem As String myRecur.ProcessDirectory(path) uppListValue = myRecur.filesArrayList.ToArray.GetLength(0) MsgBox(uppListValue.ToString) For i = 0 To uppListValue myRecur.filesArrayList(i).CopyTo(strMyItem.ToString) Debug.WriteLine(strMyItem) Next Quote
*Gurus* Derek Stone Posted September 25, 2002 *Gurus* Posted September 25, 2002 You need to create an IEnumerator interface to read the values out of the ArrayList. ArrayLists, along with other collections, implement IEnumerable.GetEnumerator which means you need to retrieve the values using IEnumerator. Dim myEnumerator As System.Collections.IEnumerator = _ myList.GetEnumerator() While myEnumerator.MoveNext() Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current) End While Quote Posting Guidelines
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.