Locked Adding To Array Error: Must Cast A New Object

  • Tuesday, August 21, 2012 9:00 PM
     
      Has Code

    Hello,

    I am currently having a bit of trouble trying to add a new object to an array of "Apps", the array being AppDirectory. In the below code, what I am doing is reading a file called "directory.txt"... It will read and create an array of Apps with the name, description and location of the executable, when the error occurs, it is asking me to create a new instance of the variable, even though it is already cast as a new App before the loop begins.

    Here is the code:

    Imports System.Reflection
    Imports System.Drawing
    
    Public Class App
    
        ''' <summary>
        ''' Add your application to the ColourSoft app directory.
        ''' </summary>
        ''' <param name="appTitle">The title of your application.</param>
        ''' <param name="appDescription">A description of what your application does.</param>
        ''' <param name="appLocation">The executable name of the application.</param>
        ''' <param name="appIcon">The icon of the application.</param>
        ''' <remarks></remarks>
        Public Shared Sub Add(ByVal appTitle As String, ByVal appDescription As String, ByVal appLocation As String, ByVal appIcon As Image)
            Dim _app As App = New App()
            AppCollection.Add(_app)
            Dim i As Integer = AppCollection.IndexOf(_app)
            AppCollection(i).Title = appTitle
            AppCollection(i).Description = appDescription
            AppCollection(i).AppForm = appLocation
            AppCollection(i).AppIcon = appIcon
    
        End Sub
    
        Public Shared Property AppCollection() As AppDirectory
    
        Public Shared Sub GetApps()
            Dim _CustApp As CustomApp = New CustomApp()
            For Each _app As App In AppCollection
                _CustApp.IsActive = False
                _CustApp.Title = _app.Title
                _CustApp.Description = _app.Description
                _CustApp.AssociatedForm = _app.AppForm
                _CustApp.Dock = DockStyle.Left
                _CustApp.Width = 110
                MainForm.pnlApplications.Controls.Add(_CustApp)
            Next
        End Sub
    
        Public Class AppDirectory
            Inherits Generic.List(Of App)
    
        End Class
    
        Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconExA" (ByVal lpszFile As String, ByVal nIconIndex As Integer, ByRef phiconLarge As Integer, ByRef phiconSmall As Integer, ByVal nIcons As Integer) As Integer
    
        'All private methods and variables
        Private _appTitle As String
        Private _appDescription As String
        Private _appForm As String
        Private _appIcon As Image
    
        Public Property Title() As String
            Get
                Return _appTitle
            End Get
            Set(ByVal value As String)
                _appTitle = value
            End Set
        End Property
    
        Public Property Description() As String
            Get
                Return _appDescription
            End Get
            Set(ByVal value As String)
                _appDescription = value
            End Set
        End Property
    
        Public Property AppForm() As String
            Get
                Return _appForm
            End Get
            Set(ByVal value As String)
                _appForm = value
            End Set
        End Property
    
        Public Property AppIcon() As Image
            Get
                Return _appIcon
            End Get
            Set(ByVal value As Image)
                _appIcon = value
            End Set
        End Property
    
        Public Shared Sub GetAppDirectoryFile()
            Dim file As String = My.Computer.FileSystem.ReadAllText("directory.txt")
            Dim contents() As String = file.Split(vbCrLf)
            Dim _title As String = ""
            Dim _description As String = ""
            Dim _appform As String = ""
            Dim _appicon As Image = Nothing
            For Each statement As String In contents
                Select Case statement
                    Case GetFromContains(statement, "name")
                        _title = ReadLine(statement, "=", 1)
                    Case GetFromContains(statement, "description")
                        _description = ReadLine(statement, "=", 1)
                    Case GetFromContains(statement, "location")
                        _appform = ReadLine(statement, "=", 1)
                        '_appicon = ReturnIcon(_app.AppForm, 0, True).ToBitmap
                    Case GetFromContains(statement, "end")
                        Add(_title, _description, _appform, _appicon)
                End Select
            Next
        End Sub
    
        Public Shared Function GetFromContains(ByVal value As String, ByVal valueToFind As String) As String
            If value.Contains(valueToFind) = True Then
                Return value
            Else
                Return Nothing
            End If
        End Function
    
        Public Shared Function ReadLine(ByVal value As String, ByVal splitter As Char, ByVal index As Integer) As String
            Dim str() As String = value.Split(splitter)
            Return str(index)
        End Function
    
        Public Shared Function ReadLine(ByVal value As String, ByVal splitters() As Char, ByVal indexes() As Integer) As String()
            Dim str() As String = value.Split(splitters)
            Dim strings() As String = Nothing
            For i As Integer = 0 To indexes.Count
                strings(i) = str(indexes(i))
            Next
            Return strings
        End Function
    
        Public Shared Function ReturnIcon(ByVal Path As String, ByVal Index As Integer, Optional ByVal small As Boolean = False) As Icon
            Dim bigIcon As Integer
            Dim smallIcon As Integer
            ExtractIcon(Path, Index, bigIcon, smallIcon, 1)
            If bigIcon = 0 Then
                ExtractIcon(Path, 0, bigIcon, smallIcon, 1)
            End If
            If bigIcon <> 0 Then
                If small = False Then
                    Return Icon.FromHandle(bigIcon)
                Else
                    Return Icon.FromHandle(smallIcon)
                End If
            Else
                Return Nothing
            End If
        End Function
    
    End Class
    

    I'm not entirely sure why I am being asked to cast App as a new instance of an App, since it is already cast as a new instance of that class, so I am very confused and really do not know how to solve this. Any help would be kindly appreciated.

All Replies

  • Tuesday, August 21, 2012 10:39 PM
     
     Answered
    You have not quoted the exact error message or the code at the line where the error occurs, but I suspect that the error relates to the collection, not the app.  When the error occurs, examine all the variables in the line that errors, and see if one of them is Nothing.   It appears that the collection is never being initialised.
    • Marked As Answer by brutalexcess Wednesday, August 22, 2012 1:26 PM
    •  
  • Wednesday, August 22, 2012 1:14 PM
     
     Answered Has Code

    Thank you for your effort. I found out that, as you said, the collection was not initialized as a new instance of AppDirectory.

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            App.AppCollection = New App.AppDirectory()
            Try
                App.GetAppDirectoryFile()
                App.GetApps()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            
        End Sub

    • Marked As Answer by brutalexcess Wednesday, August 22, 2012 1:26 PM
    •