newbie blues : 'System.NullReferenceException"

  • Thread starter Thread starter baret bonden
  • Start date Start date
B

baret bonden

I get System.NullReferenceException on this line
Public vcar As String = ListBox1.SelectedItem.ToString()

Wanted to declare a public variable such that I could pass data to another
form.
 
baret bonden said:
I get System.NullReferenceException on this line
Public vcar As String = ListBox1.SelectedItem.ToString()

Wanted to declare a public variable such that I could pass
data to another form.

\\\
If Not ListBox1.SelectedItem Is Nothing Then
... = ListBox1.SelectedItem.ToString()
End If
///
 
Baret,

I do not like this way of referencing, however when you want to do it, than
can you set it only to its value when there is a value.

The declaring part is to early for that.

This can be done in the listbox1.selectedindexchange event.
\\\
vcar = listbox.selecteditem.tostring
///
It is even posible that you should protect that with a boolean, because the
index is changed even before the listbox is filled and you use that in the
other form before that.

Than you can do
\\\
Friend vcar as string
Private formloaded as boolean
///
and set in the form load event

formloaded = true and than in that indexchangeevent
\\\
if formloaded
vcar = listbox.selecteditem.tostring
end if
///
And than probably
\\\
myextraform.show 'after that setting of vcar
///
I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top