Converting directory -VB

Value of type 'System.IO.Directory' cannot be converted to 

Page 1 of 1

2 Replies - 1505 Views - Last Post: 26 September 2009 - 02:06 AM Rate Topic: -----

#1 thadoc72  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-September 09

Converting directory -VB

Post icon  Posted 25 September 2009 - 06:52 PM

 
 Private Sub PopulateAlbumCollection(ByVal strAlbumFolderName As String)
		Dim objImgDirectory As DirectoryInfo
		Dim objDirArr As Directory()
		Dim objDirInfo As DirectoryInfo
		Dim j As Integer
		Dim strArr As String() = Nothing
		Dim strFoderName As String
		Dim i As Integer, k As Integer
		Try
			objImgDirectory = New DirectoryInfo(strAlbumFolderName)
			objDirArr = objImgDirectory.GetDirectories()
			For j = 0 To objDirArr.Length - 1
				objDirInfo = objDirArr(j)
				createAlbumFiles(objDirInfo.FullName, strArr)
				i = objDirInfo.FullName.LastIndexOf("\")
				k = objDirInfo.FullName.Length
				strFoderName = objDirInfo.FullName.Substring(i + 1, k - i - 1)
				m_objDicAlbums.Add(strFoderName, strArr)
			Next
		Catch ex As Exception
			MessageBox.Show(ex.Message)
		End Try



Is This A Good Question/Topic? 0
  • +

Replies To: Converting directory -VB

#2 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: Converting directory -VB

Posted 25 September 2009 - 09:22 PM

Possible guess...:
Dim objDirArr As Directory()


should be
Dim objDirArr() As Directory


Was This Post Helpful? 1
  • +
  • -

#3 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Converting directory -VB

Posted 26 September 2009 - 02:06 AM

Both those ways to declare an array are valid.


The problem is that GetDirectories returns an array of DirectoryInfo objects, not Directory objects. Here's its signature:

Public Function GetDirectories() As System.IO.DirectoryInfo()


I would use a for each loop to reduce the amount of code:

' Just about everything in .Net is an object, so the obj prefix doesn't add meaning.
' More modern practice is to declare variables close to where they are used rather than
' at the start of the sub. This makes it easier to read.
Dim imgDirectory As New DirectoryInfo(strAlbumFolderName)
For Each dirInfo As DirectoryInfo In imgDirectory.GetDirectories
   Dim strArr As String() = Nothing ' Use a better name that strArr - it should describe the contents or purpose of the array
   createAlbumFiles(dirInfo.FullName, strArr)
   Dim index As Integer = dirInfo.FullName.LastIndexOf("\")
   Dim length As Integer = dirInfo.FullName.Length
   Dim folderName As String = dirInfo.FullName.Substring(index + 1, length - index - 1)
   m_objDicAlbums.Add(folderName, strArr)
Next

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1