473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How can I instantiate objects dynamically in VB.NET.

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
4 3648
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Sumit Nagpal | last post by:
Hi All, My application reads a 'Class' name (as a string) from a file at Run time. Can i create an instance this Class ? Thanks Sumit
16
by: gabon | last post by:
Due a big project I would like to create different javascript classes and assign them to divs. But how? :) I know the usage of prototype but given that this could be possible: function...
9
by: the_grove_man | last post by:
I guess my question can go in two directions. I create applications that run multiple queries against a database. Generally speaking in the past I have used a Data Control (calling it dat1)...
0
by: jason | last post by:
i have classic ASP code that is calling a C# class library, which is wrapped for COM interop, and registered in the COM+ MMC. i have written 3 objects for the class library so far, and all three...
6
by: Mariano | last post by:
Hi, I have stored a menu in a database. The menu is dynamically updated and last level menu items contain the object type that should be instantiated. I am looking for a way to instantiate the...
1
by: Mark Olbert | last post by:
I have a TableAdapter declared in my website's App_Code directory (I'm using the declarative approach to design it). If, in a web page from the site, I try to instantiate an instance of the...
1
by: Donald Westfield | last post by:
I have my own module, called mystuff.py, which is to be imported for an app. The number of classes in the module will vary, so I need a subroutine that will instantiate all of them as objects,...
7
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
7
by: Joe Strout | last post by:
I have a function that takes a reference to a class, and then instantiates that class (and then does several other things with the new instance). This is easy enough: item = cls(self,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.