Reputation: 105
I am new to Visual Basic, and I need help.
I have previously used Python where you simply create a list of items by simply doing:
list = [item1, item2]
But I have no idea how I can do this in Visual Basic.
Please can someone help me to simply create a list, like you can in Python, but in Visual Basic?
Upvotes: 3
Views: 24806
Reputation: 3
Depends what you need it for, you could try creating a new data structure and then giving it properties that you desire. I also started on python and moved into visual basic and had a similar issue. Interestingly, I found that sometimes it is better to create a data structure over a list.
for example, here is a piece of code which I wrote creating a structure called 'singularity' which has some properties (I would have previously made this into a list or an array):
Public Class singularity
Public ReadOnly Property Mass As Single
Public Property Center As New Vector2(600, 600)
Public Property Velocity As Vector2
Public Sub New()
Mass = 1000
End Sub
I can then create an instance of this at any point I want and call on these properties. If I was to require the properties to change I can do that also!
I hope this gives you more insight and helps, I know others answered your question directly but I thought I'd give a bit more insight! :)
Upvotes: 0
Reputation: 75
A list in Python would essentially be an Array in Visual Basic.
You would do this by saying Dim list(x) As Integer
and then adding either the size of the list (in the parenthesis) or using a ReDim
statement to create a dynamic array if you do not know what the initial size of the array is going to be.
Upvotes: 0
Reputation: 53
Dim arraylist As New ArrayList
arraylist.Add("item")
You can use this. It's a dynamic list
Upvotes: 2
Reputation: 1724
dim list as item() = {item1, item2}
The () next to item signify that it is an array.
A working example of an integer list:
Dim list As Integer() = {1, 2, 3}
These lists are refered to as "arrays" though.
If you want an actual list, you can do:
Dim list As New List(Of Integer)({1,2,3})
This one allows you to access .Add and .AddRange, and does not hold a static capacity.
Upvotes: 6