Add string to List(of String) in an Object

Hobbyist_programmer 621 Reputation points
2021-02-18T07:49:44.313+00:00

Hallo,

I have a following object and i want to add a string to a list but i am getting error. What am i doing wrong?

    Public Class ItemsColl

            Public Property Coll1 as List(of String)

    End Class


Public CollList as New ItemsColl

CollList.Coll1.Add("StringValue")

Above code gives me 'Object reference not set to an instance of an object.' error.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,714 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 116.6K Reputation points
    2021-02-18T08:31:31.88+00:00

    Try adding 'New':

    Public Class ItemsColl
       Public Property Coll1 as New List(of String)
    End Class
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Hobbyist_programmer 621 Reputation points
    2021-02-18T08:39:52.537+00:00

    Thanks Viorel Adding New worked.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.