First off, let me provide you with my code:
Public Property Number() As Integer
Get
Return m_Number
End Get
Set(value As Integer)
m_Number = value
End Set
End Property
Private m_Number As Integer
Dim MyList As New List(Of Class1)(New Class1() {Number = 5})
I tried to convert this C Sharp code:
public int Number { get; set; }
public List<class1> MyList = new List<class1>()
{
new class1(){ Number = 5 }
};
And everytime it giving me the error:
Error 1 Value of type 'Boolean' cannot be converted to 'VB_Project.Class1'.
The compiler is trying as hard as it can to try to understand what you are trying to do. Desparately trying to turn the expression into an Integer or IEnumerable so it can call the proper List constructor. But flails at it badly, choking on the equality operator first.
You cannot add an element to a List like that. Consider the plain jane approach with a constructor:
Dim MyList As New List(Of Class1)()
Public Sub New()
MyList.Add(New Class1() With {.Number = 5})
End Sub
Or use the proper initializer syntax with the From
keyword:
Dim MyList As New List(Of Class1) From {New Class1() With {.Number = 5}}
Try this:
Dim MyList As New List(Of Class1)(New Class1() With {.Number = 5})
(note the .
before Number
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.