Creating many class instances without doing class1, class2, class3, etc.

  • Thread starter Thread starter Adam
  • Start date Start date
A

Adam

Hi all,

I need to create a bunch of class instances as myapp runs, but I want to
avoid code like this:

Dim oUser1 as new User
Dim oUser2 as new User
Dim oUser3 as new User
Dim oUser4 as new User
Dim oUser5 as new User
etc. etc.

Is there a more intelligent way to do this?
Thanks in advance!
 
Hi all,

I need to create a bunch of class instances as myapp runs, but I want to
avoid code like this:

Dim oUser1 as new User
Dim oUser2 as new User
Dim oUser3 as new User
Dim oUser4 as new User
Dim oUser5 as new User
etc. etc.

Is there a more intelligent way to do this?
Thanks in advance!

You can put them in a collection of some type... An arraylist or a
hashtable maybe, depending on how you want to look them up - by index,
or by a key of some sort.
 
Thanks, but I tried that and the last class I add to the ArrayList
overwrites all the others (being added to the ArrayList by reference i
think). If that's the case, then how do I reuse my oUser class as shown in
the repro example below?

Module Module1

Dim Users As ArrayList = New ArrayList

Dim oUser As New User

Dim counter As Integer

Sub Main()

For counter = 1 To 5

oUser.name = "user" & counter.ToString

Users.Add(oUser)

Next

For Each oUser In Users

Console.WriteLine(oUser.name)

Next

Console.ReadLine()

End Sub



Public Class User

Dim iname As String

Public Property name()

Get

Return iname

End Get

Set(ByVal Value)

iname = Value

End Set

End Property

End Class

End Module
 
you have to create a new user object in your loop
now you are adding 5 times the same object...

code:

....
Dim oUser As New User
Dim counter As Integer
Sub Main()
For counter = 1 To 5
oUser.name = "user" & counter.ToString
Users.Add(oUser)
Next
....

replace by

....
Dim oUser As User
Dim counter As Integer
Sub Main()
For counter = 1 To 5
oUser = new User()
oUser.name = "user" & counter.ToString
Users.Add(oUser)
Next
....



I didn't think it was physically possible, but this both sucks and
blows." - Bart Simpson


Adam said:
Thanks, but I tried that and the last class I add to the ArrayList
overwrites all the others (being added to the ArrayList by reference i
think). If that's the case, then how do I reuse my oUser class as shown in
the repro example below?

Module Module1

Dim Users As ArrayList = New ArrayList

Dim oUser As New User

Dim counter As Integer

Sub Main()

For counter = 1 To 5

oUser.name = "user" & counter.ToString

Users.Add(oUser)

Next

For Each oUser In Users

Console.WriteLine(oUser.name)

Next

Console.ReadLine()

End Sub



Public Class User

Dim iname As String

Public Property name()

Get

Return iname

End Get

Set(ByVal Value)

iname = Value

End Set

End Property

End Class

End Module
 
* "Adam said:
I need to create a bunch of class instances as myapp runs, but I want to
avoid code like this:

Dim oUser1 as new User
Dim oUser2 as new User
Dim oUser3 as new User
Dim oUser4 as new User
Dim oUser5 as new User
etc. etc.

\\\
Dim oUsers(4) As User
Dim i As Integer
For i = 0 To oUsers.Length - 1
oUsers(i) = New User()
Next i
///
 
Adam,
In addition to Dominique's suggestion.

I would go a step further and add a parameterized constructor (Sub New) to
User:
Public Class User

Private iname As String

Public Sub New(ByVal name As String)
iname = name
End Sub
Public Property name() As String
Get
Return iname
End Get
Set(ByVal Value As String)
iname = Value
End Set
End Property

End Class
Sub Main()
For counter = 1 To 5
oUser = New User("user" & counter.ToString)
Users.Add(oUser)
Next

The parameterized constructor allows you to create a User & set the name in
"one step" rather then two steps, it also enables to make the User.Name
property ReadOnly, so once the user is created its name cannot change. Also
because User only has the Parameterized constructor, you cannot create a
User with giving it a name!

Hope this helps
Jay


Adam said:
Thanks, but I tried that and the last class I add to the ArrayList
overwrites all the others (being added to the ArrayList by reference i
think). If that's the case, then how do I reuse my oUser class as shown in
the repro example below?

Module Module1
Dim Users As ArrayList = New ArrayList
Dim oUser As New User
Dim counter As Integer

Sub Main()
For counter = 1 To 5
oUser.name = "user" & counter.ToString
Users.Add(oUser)
Next
For Each oUser In Users
Console.WriteLine(oUser.name)
Next
Console.ReadLine()
End Sub


Public Class User

Dim iname As String

Public Property name()
Get
Return iname
End Get
Set(ByVal Value)
iname = Value
End Set
End Property

End Class

End Module
 
Back
Top