How can I instantiate objects dynamically in VB.NET.

T

Tomas

A newbie question: How can I instantiate objects dynamically in
VB.NET. E.g. I have the object 'Player' and I would like to
instantiate it with the several instances (James, Gunner, etc.),
without in advance knowing how many objects (employee1, employee2,
etc)

Dim player1 As New Persons.Players
Dim player2 As New Persons.Players
Dim player3 As New Persons.Players
....
With player1
.FirstName = "Andrew"
.LastName = "Cencini"
.Number = 43
.Position = "Shortstop"
End With

With player2
.FirstName = "Robert"
.LastName = "Lyon"
.Number = 11
.Position = "Catcher"
End With
....

I would like to instantiate a number of players (user decide the
amount) dynamically during program run. Like a loop where the user can
chose to instantiate 5, 50, or 100 players. Something like this....

For n=0 to 100
Player_number = "player" & n
Dim Player_number As New Persons.Players
Next

How would I do this in VB.NET?

Very grateful for any pointers,

Tomas

PS" I might not use the correct vocabulary, but I hope my question is
clear enough."
 
R

rudelrymer

A newbie question: How can I instantiate objects dynamically in
VB.NET. E.g. I have the object 'Player' and I would like to
instantiate it with the several instances (James, Gunner, etc.),
without in advance knowing how many objects (employee1, employee2,
etc)

Dim player1 As New Persons.Players
Dim player2 As New Persons.Players
Dim player3 As New Persons.Players
...
With player1
.FirstName = "Andrew"
.LastName = "Cencini"
.Number = 43
.Position = "Shortstop"
End With

With player2
.FirstName = "Robert"
.LastName = "Lyon"
.Number = 11
.Position = "Catcher"
End With
...

I would like to instantiate a number of players (user decide the
amount) dynamically during program run. Like a loop where the user can
chose to instantiate 5, 50, or 100 players. Something like this....

For n=0 to 100
Player_number = "player" & n
Dim Player_number As New Persons.Players
Next

How would I do this in VB.NET?

Very grateful for any pointers,

Tomas

PS" I might not use the correct vocabulary, but I hope my question is
clear enough."



Seperate your declaration from the instantiate, and make the Player
number a property of the class


Dim Player As Persons.Players , n As Integer

For n = 0 To 20
f = New clsValidation
f.PlayersNumber = "Player" & n
Next
 
G

Guest

Tomas,

Create a new player and add it to a collection, such as an array, arraylist,
list, hashtable, dictionary, etc.

Track the player by index or key.

Kerry Moorman
 
T

Tom Shelton

A newbie question: How can I instantiate objects dynamically in
VB.NET. E.g. I have the object 'Player' and I would like to
instantiate it with the several instances (James, Gunner, etc.),
without in advance knowing how many objects (employee1, employee2,
etc)

Dim player1 As New Persons.Players
Dim player2 As New Persons.Players
Dim player3 As New Persons.Players
...
With player1
.FirstName = "Andrew"
.LastName = "Cencini"
.Number = 43
.Position = "Shortstop"
End With

With player2
.FirstName = "Robert"
.LastName = "Lyon"
.Number = 11
.Position = "Catcher"
End With
...

I would like to instantiate a number of players (user decide the
amount) dynamically during program run. Like a loop where the user can
chose to instantiate 5, 50, or 100 players. Something like this....

For n=0 to 100
Player_number = "player" & n
Dim Player_number As New Persons.Players
Next

How would I do this in VB.NET?

Very grateful for any pointers,

Tomas

PS" I might not use the correct vocabulary, but I hope my question is
clear enough."

Take a look at the generic collection classes. You will want List or
Dictionary depending on how you want to access the individual objects.
 
C

Cor Ligthert [MVP]

Tomas,

You can use a list of class/object. Here a little sampel from it, it is of
course not complete

\\\
Public Class Form1
Private Players As New List(Of player)

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Players.Add(New player("Andrew", "Cencini", "ShortStop", 43))
Players.Add(New player("Robert, Leyon", "Catcher", "ShortStop", 11))
Dim MyPlayer As String = Players(0).FirstName
End Sub
End Class
///
\\\
Public Class player
Private mFName As String
Private mLName As String
Private mPosition As String
Private mNumber As Integer
Public Sub New(ByVal tFirstname As String, _
ByVal tLastname As String, ByVal tPosition As String, _
ByVal tnumber As Integer)
mFName = tFirstname
mLName = tLastname
mPosition = tPosition
mNumber = tnumber
End Sub
Public Property FirstName() As String
Get
Return mFName
End Get
Set(ByVal value As String)
mFName = value
End Set
End Property
''* 3 times for the other properties
End Class
///

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top