How to remove a dynamically created container?

Below are the codes that I am testing with, the problem I am facing is when I tried to close a webcontainer when it was opened, it leads to an NilObjectException. Any ideas?

Tony Lam

dim d as DistrictContainer
dim s as StreetContainer
dim DistrictOpened, StreetOpened as boolean

if DistrictOpened = true then
DistrictOpened = false
d.close
end if
if StreetOpened = true then
StreetOpened = false
s.close
end if

Select case Item.Name
Case “MasterFilesMenu”
Select case Choice.Text
Case “District”
DistrictOpened = true
d = new DistrictContainer
d.EmbedWithin(Self, (MainPage.Width- d.Width) / 2, (MainPage.Height - d.Height) / 2, d.Width, d.Height)
Case “Street”
StreetOpened = true
s = new StreetContainer
s.EmbedWithin(Self, (MainPage.Width- s.Width) / 2, (MainPage.Height - s.Height) / 2, s.Width, s.Height)
End Select
End Select

That’s because d is defined locally and not assigned to anything before you call d.close.

If you were to make d a property of the WebPage this would work. You might want to make it a little more descriptive though.