Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Array of classes within a Class

Array of classes within a Class

Scheduled Pinned Locked Moved Visual Basic
data-structureshelp
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bonio55
    wrote on last edited by
    #1

    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"<

    B 1 Reply Last reply
    0
    • B bonio55

      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"<

      B Offline
      B Offline
      bonio55
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups