Function and Listbox problem

archer_coal

Regular
Joined
Apr 4, 2003
Messages
96
im trying to use a case select in a function to select the right listbox to enter data into.
here is what i have

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim r As ListBox
Dim n As Integer

n = DateTimePicker1.Value.Day
r = get_listbox(n)

r.items.add("test string")

end Sub

'and the function.

Function get_listbox(ByVal n As Integer)
Dim tday As ListBox
Dim listbox1, listbox2, listbox3, listbox4 As Listbox

case select n

case 1
tday = listbox1
case 2
tday = listbox2
case 3
tday = listbox3
case 4
tday = listbox4


end select

'for debugging purposes i added the following line after the End Select

tday.Items.add("case select works")

return tday

end function


the error i get is this
An unhandled exception of type 'System.NullReferenceException' occurred
Additional information: Object reference not set to an instance of an object.

and it points to the line tday.Items.add("case select works")

thanks for any help or comments
 
You dont have an object reference because you just create the variables, you dont set them to the actual instances of the listboxes. Pass in all the listbox1,listbox2... listboxes to the function, dont dim them in the function cause it wont work.
 
Back
Top