views:

15

answers:

1

I have some code which gets child items for a menu via the GetChildren function which takes a list of menuData:

Dim builtMenu As New List(Of MenuData)(_rawData.FindAll(Function(item) item.GroupingID = 0))

For Each menuData As MenuData In builtMenu
             If menuData.Children IsNot Nothing Then
            menuData.Children.AddRange(GetChildren(menuData))
             End If
        Next

If I check if menudata.children isnot nothing, it always is nothing because the GetChildren function is yet to run (providing the child items, which do exist). If I remove this check and just have this code:

Dim builtMenu As New List(Of MenuData)(_rawData.FindAll(Function(item) item.GroupingID = 0))

For Each menuData As MenuData In builtMenu  
            menuData.Children.AddRange(GetChildren(menuData)) 
        Next

Then I am presented with a Object reference not set to an instance of an object error on menuData.Children.AddRange(GetChildren(menuData))

Please can you tell me how I get around this problem? Thanks a lot

+1  A: 

Your menuData.Children has never been instantiated, so it is a null (Nothing) reference.

You need to instantiate it before you use it:

Dim builtMenu As New List(Of MenuData)(_rawData.FindAll(Function(item) item.GroupingID = 0))
menuData.Children = New List(Of MenuData)

For Each menuData As MenuData In builtMenu  
    menuData.Children.AddRange(GetChildren(menuData)) 
Next
Oded
on adding menuData.Children = New List(Of MenuData) I get a Reference to a non shared member requires an object referece
Phil
@Phil - without knowing how `menuData.Children` is implemented, I can't really help more.
Oded
Thanks for the help. I've since got it working
Phil