|
-
Create Multiple Instances of Class at Runtime
I am seeking a way to dynamically create multiple instances of a class
at runtime.
For example, I have a class defined as PLAYER. Depending upon what the
user enters on screen,
I want VB to instantiate a number of objects.
The following code give this error. ERROR: CONSTANT EXPRESSION REQUIRED
For p = 1 To 3
Static to_str(p) As Player
Set myplayer = New Player
myplayer.playername = "Eric" & p
MsgBox myplayer.playername
Next p
How can I create multiple instances of my own class without doing so at
design time?
Thanks for your help
Eric
-
Re: Create Multiple Instances of Class at Runtime
Eric:
Not sure what that "Static" definition in the loop is. Either some obscure
syntax I haven't heard of, or it's the source of your bug.
Decide where your array is going to be - class, global, or function scope. At
that level, add
Dim to_str(p) As Player
Then, before your loop:
Redim to_str(3) As Player
For p = 1 To 3
Set to_str(p) = New Player
to_str(p).playername = "Eric" & p
MsgBox to_str(p).playername
Next p
ought to work.
Dan
Eric Jenkins wrote:
>
> I am seeking a way to dynamically create multiple instances of a class
> at runtime.
>
> For example, I have a class defined as PLAYER. Depending upon what the
> user enters on screen,
> I want VB to instantiate a number of objects.
>
> The following code give this error. ERROR: CONSTANT EXPRESSION REQUIRED
>
> For p = 1 To 3
>
> Static to_str(p) As Player
> Set myplayer = New Player
> myplayer.playername = "Eric" & p
> MsgBox myplayer.playername
>
> Next p
>
> How can I create multiple instances of my own class without doing so at
> design time?
>
> Thanks for your help
> Eric
Similar Threads
-
By Larry Rebich in forum vb.announcements
Replies: 0
Last Post: 01-23-2003, 11:57 PM
-
Replies: 5
Last Post: 10-17-2002, 01:58 PM
-
By Marco Alves in forum .NET
Replies: 6
Last Post: 03-26-2002, 09:58 AM
-
By max caber in forum .NET
Replies: 1
Last Post: 12-29-2001, 08:49 AM
-
By T Mehta in forum VB Classic
Replies: 0
Last Post: 05-09-2001, 02:24 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks