Reputation: 3
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
Upvotes: 0
Views: 163
Reputation: 54417
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.
Upvotes: 2