DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2
  1. #1
    Eric Jenkins Guest

    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

  2. #2
    Dan Appleman Guest

    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

  1. Replies: 0
    Last Post: 01-23-2003, 11:57 PM
  2. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  3. Replies: 6
    Last Post: 03-26-2002, 09:58 AM
  4. Can I create a C# class from an XML Schema?
    By max caber in forum .NET
    Replies: 1
    Last Post: 12-29-2001, 08:49 AM
  5. Replies: 0
    Last Post: 05-09-2001, 02:24 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links