-
Nov 27th, 2011, 12:47 PM
#1
Thread Starter
New Member
Arrayed class inside a class, what am I doing wrong?
Haven't coded in a while, and ran into a wall when setting up my program data structure
So I have something like this in a class file;
Public Class X
Public Stuff as Integer
Public Stuff1 as String
Public Stuff2 as Double
End Class
Public Class Y
Public Things as Integer
Public More() as X
End Class
Dim Z as new Y
ReDim Z.Y(4)
Z.Y(2)=4
But obviously this doesn't work...
I want to structure my program this way, what's the best way to go about that?
I want to access multiple data types in the same array from within a class, as it should be a subset of my base class etc... and want to be able to change the size of the array
Or is this class array within class nesting a dead end?
Last edited by Athiril; Nov 27th, 2011 at 12:56 PM.
-
Nov 27th, 2011, 01:14 PM
#2
Re: Arrayed class inside a class, what am I doing wrong?
First off, you should look at List (of T) (or in your case List (of X)) rather than arrays. That seems likely to be a happier solution for you, as Redim is generally a sign that a List would be a better tool than an array.
Otherwise, Z is a Y, which HAS an array, but is not itself an array. Furthermore, Z IS a Y, and Y does not have a Y, so Z.Y makes no sense, because that would mean that you are looking for the Y member of Z. More correct would be Z.More(4), since More is the array member of Z, so you could get the fourth element of it.
My usual boring signature: Nothing
-
Nov 27th, 2011, 01:24 PM
#3
Thread Starter
New Member
Re: Arrayed class inside a class, what am I doing wrong?
Oh yes you are right, sorry I am tired. I was just simplifying my code to the concept I was having the problem with and made an error in doing so.
I have a Public Class called Lens, it has member Element() which is of type Element, in Element it has members Curve and Material which are of types Curve, and Material, all defined as separate classes.
So I access it as X.Element(index).Member = Something, only to not work. Making the member names separate to the classes doesn't help.
edit: Seem to have solved the problem in an -ugly- way.
So you have say, separate classes, have a need to not nest them, but access like I want to..
Public Class X
Public Stuff As String
End Class
Public Class Y
Public X As X
End Class
Public Class Z
Public type As Integer
Public radius As Double
End Class
Public Class A
Public Name As String
Public Description As String
Public NumElements as Integer
Public Y() As Y
End Class
then in the body code:
Dim TestA = New A
TestA.NumElements = 4
Dim i as Integer
ReDim TestLens.Element(TestA.NumElements)
For i = 1 To TestA.NumElements
TestLens.Y(i) = New Y
TestLens.Y(i).Z = New Z
TestLens.Y(i).X = New X
Next
Then access freely..
Last edited by Athiril; Nov 27th, 2011 at 01:41 PM.
-
Nov 27th, 2011, 01:40 PM
#4
Re: Arrayed class inside a class, what am I doing wrong?
It should... and shouldn't... what you have is an array of a class... you've defined the size of the array... but you haven't INSTANCIATED ... by dimming the size of the array, you've said "I need a box this big" ... but you still haven't filled the box with anything. Once you define the array, you need to instantiate the objects that go into each element.
-tg
-
Nov 27th, 2011, 01:43 PM
#5
Thread Starter
New Member
Re: Arrayed class inside a class, what am I doing wrong?
Originally Posted by techgnome
It should... and shouldn't... what you have is an array of a class... you've defined the size of the array... but you haven't INSTANCIATED ... by dimming the size of the array, you've said "I need a box this big" ... but you still haven't filled the box with anything. Once you define the array, you need to instantiate the objects that go into each element.
-tg
I couldn't use the new keyword for the base class, and have it 'filter down' through into the lower classes inside it etc. And it wouldn't let me with the array.
See above for my edit, that's the only way I've found working so far.. I need to keep it segregated like that due to the amount of data and math complexity going to be performed so I don't lose my head and know where I am in referencing something.
Last edited by Athiril; Nov 27th, 2011 at 01:47 PM.
-
Nov 27th, 2011, 01:56 PM
#6
Re: Arrayed class inside a class, what am I doing wrong?
Yeah, you COULD, though the terminology is a bit off. Base class tends to suggest inheritance, which is not what you have. Class Y should have a constructor that created a New X. It doesn't need a real constructor, it just has to be written like this:
Code:
Public Class Y
Public myX as New X
End Class
If you want to do more than just create a New X, then you would need an explicit constructor (Sub New).
That won't really work for class A, though, unless you can pass the size of the array as an argument to the constructor. If you can do that, then A would have a constructor like this:
Code:
Class A
Public myY as Y()
Public Sub New (noItems as Integer)
Redim myY(noItems-1)
For x = 0 to noItems-1
myY(x) = New Y
Next
End Sub
End Class
However, this only works if you know the number of elements at the time you create the instance of A. If you don't then you would need to add a method to A that would size the array and create the elements appropriately.
However, there still seems to be a bit of confusion, as Y doesn't have a Z, yet you try to put a Z into the array of Y in one line, and try to do something to the Z element of an array item in a different line, while also showing a TestLens item, which isn't defined or declared, as far as I can see.
My usual boring signature: Nothing
Tags for this Thread
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
|