Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add item when using List (Of)?

Tags:

vb.net

I created a class where i declared some properties.

Public Class BlogPost

    Dim _postTitleUrl As String = String.Empty
    Dim _pageGUID As String = String.Empty


    Property postTitleUrl() As String
        Get
            Return _postTitleUrl
        End Get
        Set(ByVal value As String)
            _postTitleUrl = value
        End Set
    End Property
    Property pageGUID() As String
        Get
            Return _pageGUID
        End Get
        Set(ByVal value As String)
            _pageGUID = value
        End Set
    End Property
End Class

Now, I have another class where I want to set the values.

Public Class SetBlogData

  Public blogPostList As New List(Of BlogPost)
  Public dataCounter as integer = 0

  blogPostList(dataCounter).pageGUID = mainBlogSPWeb.ID.ToString

....

This gives me an error about Index was out of range. Hpw can I properly access the properties in BlogPost class?

like image 419
JADE Avatar asked Jan 05 '12 04:01

JADE


People also ask

How do I add an item to a list from another list?

append() adds the new elements as another list, by appending the object to the end. To actually concatenate (add) lists together, and combine all items from one list to another, you need to use the . extend() method.

How do I add an item to a specific index in a list?

Inserting an element in list at specific index using list. insert() In python list provides a member function insert() i.e. It accepts a position and an element and inserts the element at given position in the list.

How do I add a single item to a list?

The append() method adds a single element to the end of the list . Where, element is the element to be added to the list.


2 Answers

Because your list has nothing .

You should use add method to add your new item. Like ...

        Dim blogPostList = New List(Of BlogPost)
        Dim blogPost = New BlogPost
        blogPost.pageGUID = mainBlogSPWeb.ID.ToString
        blogPostList.Add(blogPost)
like image 147
shenhengbin Avatar answered Oct 04 '22 04:10

shenhengbin


You need to put a BlogPost in your list by writing blogPost.List.Add(New BlogPost())

like image 35
SLaks Avatar answered Oct 04 '22 02:10

SLaks