Jump to content
  • Advertisement
Sign in to follow this  
Ranthalion

VB.Net Array question

This topic is 5021 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm messing around with a listview in VB.NET and I'm having a problem with declaring an array of ListViewItems. Here is what I have so far:
'Test function for populating information in a listview
Private Sub LoadList(ByVal ListView1 As ListView, ByVal dsRecord As ADODB.Recordset)

        Dim listitem1() As ListViewItem
        Dim ctri As Integer

        ListView1.BeginUpdate()
        ListView1.Clear()
        ListView1.Columns.Insert(0, "String", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("Test", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("Test2", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("Test3", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("Test4", 100, HorizontalAlignment.Left)

        For ctri = 0 To 9
            listitem1(ctri) = New ListViewItem() 'This line generates the error

            listitem1(ctri).SubItems.Add("subitem" & ctri)
            listitem1(ctri).SubItems.Add("subitem" & ctri)
            listitem1(ctri).SubItems.Add("subitem" & ctri)

            ListView1.Items.Add(listitem1(ctri))
        Next ctri

        ListView1.EndUpdate()

    End Sub


This is the error it generates:
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication6.exe

Additional information: Object reference not set to an instance of an object.


Can anyone here show me the correct way of coding an array of an unknown size and using it to populate my listview? Thanks Ranthalion [edited to reflect changes in code] [Edited by - Ranthalion on September 22, 2004 10:36:18 AM]

Share this post


Link to post
Share on other sites
Advertisement
Arrays in vb.net are zero based. In your for loop you are starting at 1 instead of zero. Hence element zero is null.

Share this post


Link to post
Share on other sites
VB.NET arrays are zero based and you are initializing the array starting a 1 instead of zero in the for loop. Hence you are getting a null reference at element 0.

Share this post


Link to post
Share on other sites
You've never initialized the array listitem1 - try declaring it as :
Dim listitem1(0) As ListViewItem
or
Dim listitem1() As ListViewItem = new listitem1(0){}

This will create an array with a single element.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!