0

I can't seem to figure out what's going on here:

EDIT: Let's try this... here's an outline of my code...

    Class form1
    Dim table1 as DataTable


    Sub refreshdata()
         table1.load() 'this puts data in table1
    End Sub

    Sub sub1 ()
         msgbox(table1.rows.count)  'this returns the number 15
     End Sub

    Sub combobox_closed (ByVal...) Handles ComboBox1.DropDownClosed
         msgbox(table1.rows.count)   'this returns the NullReferenceException

So what's the difference between the last two subs? Why can one access the table and not the other?

JH99
  • 3
  • 1
  • 3
  • table1 is a local variable inside the RefreshData method. It doesn't exist outside of that method- – Steve Aug 26 '14 at 19:46
  • Sorry. I declared table1 outside of the method and have accessed it with other methods. I'll update the above – JH99 Aug 26 '14 at 19:50
  • 1
    `Dim table1 As New DataTable` <---- you are filling a NEW one you declared in Form Load remove the `Dim` - it is already declared – Ňɏssa Pøngjǣrdenlarp Aug 26 '14 at 19:53
  • I get the "Expression is not a method" error. ps. thanks for being patient with a newbie – JH99 Aug 26 '14 at 20:01

1 Answers1

0

In the posted code. table1 is being declared as a local variable in one subroutine, and you are trying to access it from another subroutine. Make it a class scoped private variable instead to be able to access it from both routines.

Private table1 As DataTable = Nothing

jwatts1980
  • 7,254
  • 2
  • 28
  • 44
  • Sorry for all of the confusion, everyone. I figured it out. Turns out that DataTables are not very portable through routines, but DataSets work just fine. As in... table1.rows is inaccessible where dataset.table(0).rows is perfectly accessible by everyone. Go figure. – JH99 Aug 28 '14 at 17:10