Create Multiple Instances of Class at Runtime


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Create Multiple Instances of Class at Runtime

  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
    Share on Google+

  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


    Share on Google+

Similar Threads

  1. Replies: 0
    Last Post: 01-24-2003, 12:57 AM
  2. Replies: 5
    Last Post: 10-17-2002, 02:58 PM
  3. Replies: 6
    Last Post: 03-26-2002, 10: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, 09:49 AM
  5. Replies: 0
    Last Post: 05-09-2001, 03: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center