By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

How can I instantiate objects dynamically in VB.NET.

P: n/a
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."

Apr 16 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Apr 16, 3:04 pm, "Tomas" <tomas_nordlan...@yahoo.comwrote:
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

Apr 16 '07 #2

P: n/a
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
"Tomas" wrote:
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."

Apr 16 '07 #3

P: n/a
On Apr 16, 3:04 pm, "Tomas" <tomas_nordlan...@yahoo.comwrote:
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.

--
Tom Shelton

Apr 17 '07 #4

P: n/a
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
"Tomas" <to**************@yahoo.comschreef in bericht
news:11**********************@q75g2000hsh.googlegr oups.com...
>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."

Apr 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.