Hado Hado - 12 days ago 6
Vb.net Question

How do I redefine the class of a variable programmatically?

Ok, so I have 2 classes, Note and HeldNote. HeldNote inherits from Note and adds only 2 variables, plus a different way of setting visual dimensions. I have an array of an undefined amount of items, with each item defined as a member of class Note.

Occasionally during my program, I want one of these items in the array to be a HeldNote rather than a Note, and I do not know which one until the program is running.

VB.Net seems fine with me declaring array(i) = New HeldNote but then wont let me access the variables that HeldNote has but Note does not. Using Breaks in the program leads me to believe that array(i) is not actually being redefined as a HeldNote rather than a Note.

So, my question is, how can I make array(i) a HeldNote, rather than a Note?

I'm using VB.net 2008.

Edit: I asked my Teacher about this problem and he said he didn't know and told me to look it up. I couldn't anything online (had been looking since before i asked him) so I've come here for help.

Answer

A few suggestions:

1) Change your design and have just one class rather than one that inherits from another.

2) Declare your array as type Object. Each element of an Object array can be of a different type. This way you can assign each element's type at runtime.

Dim notearray As Object() = New Object() {New Note(), New HeldNote(), New HeldNote(), New Note()}