Array of classes within a Class
-
Hello, I currently have a problem that i have thought of a soultion for but im unsure of how i can implement this. Problem: I have an new instance of a class, lets call it "Temp_Class" and within that class there are some properties x,y,z and say 6 instances of another class "Sub_Class". There may be 1 instance of "Sub_Class" or as many as 100 depending on Property X which is an integer. Just like the "Temp_Class", "Sub_Class" contains some Properties A,B,C and up to 5 instance of a thrid class "Sub_Sub_Class" which just contains properties D,E,F. I wanted to be able to referance each of the "Sub_Sub_Class" properties by using arrays so that i can set a varing amount of Sub_Classes or Sub_Sub_Classes where nessercary but for this i would need to create a new instance of each class. The problem with defining an array as a class is that it wont allow me to define it as a new class for some reason.
Public Temp_Class Private X_Value Private Y_Value Private Z_Value Public Property X .............(Get and Set are tested and are known to work) Public Property Y ............. Public Property Z ............. Public Array_1(X_Value) as Sub_Class End Class Public Sub_Class Private A_Value Private B_Value Private C_Value Public Property A ............. Public Property B ............. Public Property C ............. Public Array_2(Y_Value) as Sub_Sub_Class End Class Public Sub_Sub_Class Private D_Value Private E_Value Private F_Value Public Property D ............. Public Property E ............. Public Property F ............. End Class
The Idea is that you can then Referance any D,E,F propery as follows.Dim Test as new Temp_Class Test.Sub_Class(0).Sub_Sub_Class(0).D = "Hello" '(assuming 'D' is a String) Test.Sub_Class(0).Sub_Sub_Class(1).D = "Test" Test.Sub_Class(0).Sub_Sub_Class(2).D = "Hello" Test.Sub_Class(0).Sub_Sub_Class(3).D = "Test" Test.Sub_Class(1).Sub_Sub_Class(0).D = "Hello" Test.Sub_Class(1).Sub_Sub_Class(1).D = "Test" Test.Sub_Class(2).Sub_Sub_Class(2).D = "Hello" Test.Sub_Class(2).Sub_Sub_Class(3).D = "Test"
I have been able to get this working but only if dont use arrays, but then everything becomes alot more complicated. i.e. (tested and works, where items have been defined as new instances)Test.Sub_Class_0.Sub_Sub_Class_0.D = "Hello" Test.Sub_Class_0.Sub_Sub_Class_1.D = "Test" Test.Sub_Class_1.Sub_Sub_Class_0.D = "Hello" Test.Sub_Class_1.Sub_Sub_Class_1.D = "Test"<
-
Hello, I currently have a problem that i have thought of a soultion for but im unsure of how i can implement this. Problem: I have an new instance of a class, lets call it "Temp_Class" and within that class there are some properties x,y,z and say 6 instances of another class "Sub_Class". There may be 1 instance of "Sub_Class" or as many as 100 depending on Property X which is an integer. Just like the "Temp_Class", "Sub_Class" contains some Properties A,B,C and up to 5 instance of a thrid class "Sub_Sub_Class" which just contains properties D,E,F. I wanted to be able to referance each of the "Sub_Sub_Class" properties by using arrays so that i can set a varing amount of Sub_Classes or Sub_Sub_Classes where nessercary but for this i would need to create a new instance of each class. The problem with defining an array as a class is that it wont allow me to define it as a new class for some reason.
Public Temp_Class Private X_Value Private Y_Value Private Z_Value Public Property X .............(Get and Set are tested and are known to work) Public Property Y ............. Public Property Z ............. Public Array_1(X_Value) as Sub_Class End Class Public Sub_Class Private A_Value Private B_Value Private C_Value Public Property A ............. Public Property B ............. Public Property C ............. Public Array_2(Y_Value) as Sub_Sub_Class End Class Public Sub_Sub_Class Private D_Value Private E_Value Private F_Value Public Property D ............. Public Property E ............. Public Property F ............. End Class
The Idea is that you can then Referance any D,E,F propery as follows.Dim Test as new Temp_Class Test.Sub_Class(0).Sub_Sub_Class(0).D = "Hello" '(assuming 'D' is a String) Test.Sub_Class(0).Sub_Sub_Class(1).D = "Test" Test.Sub_Class(0).Sub_Sub_Class(2).D = "Hello" Test.Sub_Class(0).Sub_Sub_Class(3).D = "Test" Test.Sub_Class(1).Sub_Sub_Class(0).D = "Hello" Test.Sub_Class(1).Sub_Sub_Class(1).D = "Test" Test.Sub_Class(2).Sub_Sub_Class(2).D = "Hello" Test.Sub_Class(2).Sub_Sub_Class(3).D = "Test"
I have been able to get this working but only if dont use arrays, but then everything becomes alot more complicated. i.e. (tested and works, where items have been defined as new instances)Test.Sub_Class_0.Sub_Sub_Class_0.D = "Hello" Test.Sub_Class_0.Sub_Sub_Class_1.D = "Test" Test.Sub_Class_1.Sub_Sub_Class_0.D = "Hello" Test.Sub_Class_1.Sub_Sub_Class_1.D = "Test"<
Helo, Only me again, from all of my random attempts of trying to find a soultion for this i found one so i thought i would post the solution. when u define the array as a class you have to define it with a random figure i.e. public Temp(0) as Sub_Class then in the subroutine 'New' for the class "Sub_Class" you call a subroutine called create_X()
Public Test as new Temp_Class(8,1,2) ..... ..... Public Class Temp_Class ..... public Temp(0) as Sub_Class ..... Private x_Value as integer Private y_Value as integer Private z_Value as integer ..... Public Sub New(ByVal x_val, ByVal y_val, ByVal z_val) ReDim Temp(Temp_Class.X) x_value = x_val y_value = y_val z_value = z_val Create_X() End Sub .... Public Sub Create_X() Dim temp_X As New Sub_Class Dim n As Integer Public Sub Create_X() For n = 0 To Temp_Class.X Me.Sub_Class(n) = temp_X Next End Sub .... .... End Class Public Class sub_Class ..... public Temp2(0) as Sub_Sub_Class ..... Public Sub New() ReDim Temp2(Temp_Class.Y) ..... Create_Y() End Sub .... Public Sub Create_Y() Dim temp_Y As New Sub_Sub_Class Dim n As Integer Public Sub Create_Y() For n = 0 To Temp_Class.Y Me.Sub_Sub_Class(n) = temp_Y Next End Sub .... .... End Class
this relies on the fact hta default values have been put into the sub_Class and Sub_Sub_Class Properties in the subroutine new() and also relies on the fact the initaliation 'Test' class being called with overload. if this approach is then taken you are effectivly creating defualt values for thouse classes and by doesing so removes the problem of the computer bringing up Null References Sorry if this isnt very clear but someone else might be able to write it up more clearly if they can understand what i mean.