Results 1 to 11 of 11

Thread: dynamic array of class

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    South Africa
    Posts
    113

    dynamic array of class

    Hi
    Is it possible to create array of a class. 'But a unspecified number of elements

    e.g Dim A() as String '(This creates a array of strings with a unspecified number of elements)

    But !!

    e.g Dim MyForm() As System.Windows.Forms.Form
    Dim a As Integer = 2
    Redim MyForm(a)
    MyForm(1).Text = "Hello" ***No instance of class Form exception occurs****


    I cannot reference a instance of a class in the array, any ideas why ?
    You are living a pacifist dream, and if you dreaming it means you sleeping and you should damn well wake up!

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: dynamic array of class

    Yes you can create array of class but you have to declare the all the methods as shared (because you can declare arrays with New keyword ) .

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    South Africa
    Posts
    113

    huh?

    bts its a class of type form and form already has all its methids declared
    You are living a pacifist dream, and if you dreaming it means you sleeping and you should damn well wake up!

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: dynamic array of class

    Originally posted by SmagO
    Hi
    Is it possible to create array of a class. 'But a unspecified number of elements
    I meant this !

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    String is a value type not a reference type variable thus it doesn't require the New keyword to make a new instance. When declaring arrays of reference types the array does not declare new instances of the objects you must do this for each element in the array. You can do it at declaration but you would have to know how many instances or just individually as you add the elements to the array.

    VB Code:
    1. 'specificied type but haven't created any instances
    2.         Dim frms() As Form3
    3.         ReDim frms(1)
    4.         'create an instance for the new elements
    5.         frms(0) = New Form3()
    6.         frms(1) = New Form3()
    7.         'now its safe to use
    8.         frms(0).Show()

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    String is a value type not a reference type variable thus it doesn't require the New keyword to make a new instance.
    Actually, the System.String class is a reference type. Behind the scenes, a string object is allocated on the heap, rather than the stack. For instance, if we assign one string variable to another string, we get two disparate references to the same string in memory. However, if we make changes to one of the strings, an entirely new string object will be placed on the heap.

    Thus, this is why massive string manipulation with a typical string type is frowned upon, welcoming the StringBuilder class a much better substitute.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Hmm, I didn't know that. I thought all of the basic data types were value types. Thanks.

    How come you don't have to use New with strings?

  8. #8
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Because the = (and other operators) operator have been overloaded.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    South Africa
    Posts
    113

    Wink maybe

    is it maybe just that you cant have an array of objects?
    You are living a pacifist dream, and if you dreaming it means you sleeping and you should damn well wake up!

  10. #10
    Lively Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    95
    Why not use an ArrayList, or similiar collection class, instead, you can add what you like and it grows with your needs.

  11. #11
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Hehe... I actually did something like this today, tried making an array of classes... well, I ended up making a class that holds a collection of class instances...

    Take a look at the MSDN step-by-step below, it really helped me out:

    "In this walkthrough, you use the CollectionBase class to create a class called WidgetCollection. This is a collection that accepts only widgets, and exposes its members as Widget types, instead of accepting objects and exposing members as Object type. You then implement methods to add widgets to your collection and to remove the widget at a specific index, and you also implement an Item property that returns the widget object at the appropriate index. "

    http://msdn.microsoft.com/library/de...ctionClass.asp

    With this help, I was able to create an instance of an invoice, and add instances of invoiceitems (items that belong to the sale), and have the whole collection of invoiceitems internal to the invoice itself. So to add an invoiceitem (which is a class instance since an item contains name, style, price, cost, color,etc), I simply used:
    VB Code:
    1. Dim r As Invoice = New Invoice() 'declare a new invoice
    2.  
    3.         Dim invItem1 As InvoiceItem = New InvoiceItem("Elran", 2, "Assisa", _
    4. 3, 1, "Sofa", 200, 30, 10, 20) 'declare new invoice item instance
    5.         Dim invItem2 As InvoiceItem = New InvoiceItem("Legacy", 2, "Apachea", _
    6.  3, 1, "Sofa", 200, 20, 10, 15) 'declare another invoice item instance
    7.         r.Add(invItem1) 'add item to invoice
    8.         r.Add(invItem2) 'add item to invoice
    Last edited by nemaroller; Apr 16th, 2003 at 05:11 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width