|
-
Apr 14th, 2003, 04:22 AM
#1
Thread Starter
Lively Member
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!
-
Apr 14th, 2003, 06:07 AM
#2
Sleep mode
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 ) .
-
Apr 14th, 2003, 07:00 AM
#3
Thread Starter
Lively Member
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!
-
Apr 14th, 2003, 07:24 AM
#4
Sleep mode
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 !
-
Apr 14th, 2003, 10:23 AM
#5
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:
'specificied type but haven't created any instances
Dim frms() As Form3
ReDim frms(1)
'create an instance for the new elements
frms(0) = New Form3()
frms(1) = New Form3()
'now its safe to use
frms(0).Show()
-
Apr 14th, 2003, 01:56 PM
#6
PowerPoster
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.
-
Apr 14th, 2003, 02:01 PM
#7
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?
-
Apr 14th, 2003, 02:08 PM
#8
PowerPoster
Because the = (and other operators) operator have been overloaded.
-
Apr 15th, 2003, 04:16 AM
#9
Thread Starter
Lively Member
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!
-
Apr 16th, 2003, 09:38 AM
#10
Lively Member
Why not use an ArrayList, or similiar collection class, instead, you can add what you like and it grows with your needs.
-
Apr 16th, 2003, 05:00 PM
#11
I wonder how many charact
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:
Dim r As Invoice = New Invoice() 'declare a new invoice
Dim invItem1 As InvoiceItem = New InvoiceItem("Elran", 2, "Assisa", _
3, 1, "Sofa", 200, 30, 10, 20) 'declare new invoice item instance
Dim invItem2 As InvoiceItem = New InvoiceItem("Legacy", 2, "Apachea", _
3, 1, "Sofa", 200, 20, 10, 15) 'declare another invoice item instance
r.Add(invItem1) 'add item to invoice
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|