473,765 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3661
On Apr 16, 3:04 pm, "Tomas" <tomas_nordlan. ..@yahoo.comwro te:
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.comwro te:
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(ByVa l sender As System.Object, _
ByVal e As System.EventArg s) 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).Firs tName
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.comsch reef in bericht
news:11******** **************@ q75g2000hsh.goo glegroups.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
2493
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
2185
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 newDiv(){...} newDiv.prototype=new div(); and of course it isn't. How to instantiate that class? var newDiv_instance=document.createElement("div");
9
2633
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) making it invisible and have used this whenever i needed it. dat1.databasename = app.path & "mydatabase.mdb" dat1.recordsource = "my query" dat1.refresh
0
1166
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 of them can be instantiated and used by the ASP classic application. however, i've just added a new object, and when i try to instantiate that object from the ASP application i get "Error '80070002'" which might mean "file not found," based on...
6
1579
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 object dynamically without an enormous select case statement. I can't find nothing in the docs... Any ideas? To make things clear, what I want to do is something like this: dim objType as new String("CustomerObject") dim obj as new objType
1
1468
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 TableAdapter dynamically: protected void Page_Load(object sender, EventArgs e) { // check that we can find the Type of the TableAdapter Type junk = typeof(page_infoTableAdapters.bd_contentTableAdapter); // now try to create the Type
1
1625
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, and assign a variable to each, regardless of what names the classes are. Then I need to be able to delete these objects from memory completely at times.
7
8224
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
3241
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, **itemArgs) where "cls" is the class reference, and itemArgs is obviously a set of keyword arguments for its __init__ method. But now I want to generalize this to handle a set of mix-in classes.
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.