Home > Blockchain >  VB how to add a list item to a class
VB how to add a list item to a class

Time:07-06

No so familiar with VB how would one add to a list in a class item ?

 public class Demo
   public Property Id as Integer
   public RevsList as List (of ProjectItem)
 end Class

 public class ProjectItem
   public Property Rev as Integer
   public Title as string
 end Class

With code i try to ad a ProjectItem to the Revslist

 Dim revision as new Demo With {.id = 1}
 revision.RevsList.Add(new ProjectItem() With {.Rev =33, .Title="description"})  '<= what is the correct syntax todo this

Hod does one add a item to such a list.

CodePudding user response:

You just missed out a separator and a dot before the title variable....

Dim revision as new Demo With {.id = 1}
revision.RevsList.Add(new ProjectItem() With {.rev =33, .title="description"})

I also notice that you have declared RevsList in your demo class, but not initialised it. You can either do that in the demo class but using the New keyword:

public class Demo
   public Property Id as Integer
   public RevsList as New List (of ProjectItem)
end Class

Or you can do it before adding anything to it:

Dim revision as new Demo With {.id = 1}
revision.RevsList = New List(of ProjectItem)
revision.RevsList.Add(new ProjectItem() With {.Rev =33, .Title="description"})

EDIT

If you are still having issues, try creating the ProjectItem before adding it. It is a bit more verbose, but is arguably easier to read:

    Dim revision As New Demo
    revision.Id = 1

    Dim NewProjectItem As New ProjectItem
    NewProjectItem.Rev = 33
    NewProjectItem.Title = "description"

    revision.RevsList.Add(NewProjectItem)
  • Related