0

Good afternoon.

I would very much appreciate some assistance in constructing a custom array in VB.NET (not C#). I've recently made the switch from VBA to VB.NET and must admit I'm loving it! However, I've now come across a problem and evidently a large gap in my programming knowledge.

Essentailly, I need to create a custom array, compiled from custom arrays, that details a construction site.

The array at the top level will be buildings, within each building entry will be an array of floors, and then within each floor entry will be the zones of the floor. At each level there will also need to be another array for names (so building names, then floor names, but not zones names as this is all that needs to be stored at the bottom level).

The number of buildings on the site, floors in each building and zones in each floor in each building can all be different from job to job. And this where I'm stuck.

Back in my VBA days I would have just declared a few Public Types for zones, levels and buildings, and would have stacked them as undefined arrays within each other, but that's not an option in VB.NET (and probably a bodge anyway!)

So would very much appreciate any and all assistance with this. I've had a look at Structures, but don't think that's the way forward. I've included a rough diagram of what I'm trying to acheive, in case it helps.

Thanks very much

Graham

Rough example image

2
  • Building data structures always depends on how data going to be consumed. Can you tell how you are going to consume this data? Commented Jun 21, 2020 at 2:20
  • Essentially, the user just needs to enter the names of the buildings, then be asked to enter the names of the floors for each of those buildings, then the names of the zones for each floor. This information will simply be used a bit later on to generate codes that time can be booked against and listed on invoices for the clients information. Commented Jun 21, 2020 at 8:42

1 Answer 1

2

VB.NET is an object-oriented language so use it as such. You don't want any arrays here. Classes with properties that are collections of other classes. Something like this:

Public Class Zone

    Public Property ZoneName As String

End Class

Public Class Floor

    Public Property FloorName As String

    Public ReadOnly Property Zones As New List(Of Zone)

End Class

Public Class Building

    Public Property BuildingName As String

    Public ReadOnly Property Floors As New List(Of Floor)

End Class

Public Class BuildingSite

    Public Property BuildingSiteName As String

    Public ReadOnly Property Buildings As New List(Of Building)

End Class

You can then create a BuildingSite object and add Building objects to its Buildings collection and add Floor objects to their Floors collections and add Zone objects to their Zones collections. This is just like a DataSet has DataTables and DataRelations in its Tables and Relations collections and each DataTable has DataRows and DataColumns in its Rows and Columns collections.

EDIT:

Here is an example of how you might go about using these classes:

Dim z1 As New Zone

z1.ZoneName = "Zone A"

Dim f1 As New Floor

f1.FloorName = "Floor 0"
f1.Zones.Add(z1)

Dim b1 As New Building

b1.BuildingName = "Building A"
b1.Floors.Add(f1)

Dim bs1 As New BuildingSite

bs1.BuildingSiteName = "Building Site 1"
bs1.Buildings.Add(b1)

bs1.Buildings.Add(New Building With {.BuildingName = "Building B"})

Dim b2 = bs1.Buildings(1)

b2.Floors.Add(New Floor With {.FloorName = "Floor 1"})

Dim f2 = b2.Floors(0)

f2.Zones.Add(New Zone With {.ZoneName = "Zone B"})

Like I said, it's just like so many other examples of collection properties throughout the .NET Framework, e.g. you can create a DataSet and add a DataTable to its Tables collection and then add a DataColumn to the Columns collection of that.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks very much for the starting point. I've been doing some more research as I woefully ignorant on custom classes and collections. Am I right in thinking that I need to add more functions / subs in that class to allow data to be added and removed? As I've just commented above, not a lot is being done with the stored data, but I will need to cycle through and read it all at the end of the program I'm writing.
Each class would require additional properties to store additional data relating to objects of that type. For instance, the Building class might have an Address property. A class is supposed to represent real type of object or abstract concept. Properties represent data that it has and methods represent behaviour that it does. Think about the thing, what it has and what it does and design your classes accordingly.
Thank you very much for taking the time to help me with this. I'm starting to get the idea of using classes and collections over complex arrays, but may have further confused myself by reading / watching too many tutorials. I would be most grateful if you could please tell me how to initiate the collection of that class and add data to it? I've had a stab at it but think my syntax maybe wrong. Kind regards. Graham
Thank you very much indeed. I was actually pretty close! It was the .Add New bit I was getting wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.