Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

My question is how to declare a nested array of structures (If that is what you call it) in VB.Net

Essentially I would like to do this:

VB
Year(5).Month(3).Day(3) = 123 '(arbitary number)


I'm new to VB.NET and I apologise if this has been asked before

Any link or comment would be greatly appreciated
Posted

There are many ways to declare nested storage stucture in .NET (multi-deimensional arrays, collections of collections [of collections!], etc.)

Choice may be a matter of personal preference or exactly what you want to do with each level of that nested hierarchy. However, none of the above would give you the exact syntax that you state. More typical would be Day(5,3,3) = 123 or Day(5)(3)(3) = 123.

To give the syntax you require you could use discreet classes for Years and Months:
Years having a property of Month, which takes an index and manages a List (Of Months)
Months having a property of Day, which takes an index and manages a List (Of Integer)?

One consideration is what does assigning 123 to a "day" mean? You may need a Class "Days" to interpret, range check etc..

Are you sure that you can't use the in-built abstract date/time data type?

I strongly suggest reading up on the "List" class as a container which can be declared "(Of Type)". Also, be careful about using object names that may be reserved words (or members of the VB global namespace) like Month and Year (note that I used plurals for the class names!)

It's harder to give a more comprehensive solution for such a general question, but I think I've answered your specific enquiry?
 
Share this answer
 
Comments
Member 11056080 3-Sep-14 10:36am    
Thank you for the reply.

I figured I'd dig into object orientated programming seeing that I'm using VB.Net
I might as well learn how to do it 'properly'

Lists of lists seem to be the more Object Orientated way, and to me at least, Multi-Dimension arrays are the easiest to implement.

Anyways...
In short I am developing an Irrigation controller for a Btech Project of mine, the PC sends the times to the microcontroller. Every day can have 5 commands and this occurs withing a repeating 7 day cycle.

Now the microcontroller part is fine, working, handy dandy.

But I need/Want to do a GUI for it, hence VB and not say C.

So basically this is purely for Organisation purposes as well as "easy looping"

I can show you the code I have thus far? (it's going to be a mess, I promise you :D )
Member 11056080 3-Sep-14 10:45am    
How about:

<pre lang="vb">
Public Structure Struct_Command

'Data Structure:
'|DDDHHHHH|MMMMMMMM||NNNN|CCCC|TTTTTTTT|
'D - Day
'H - Hour
'M - Minute
'N - Valve number
'C - Command Number
'T - Time (length)

Dim Packed As UInteger

Dim Day As Byte
Dim Hour As Byte
Dim Minute As Byte
Dim Valve_Number As Byte
Dim Command_Number As Byte
Dim Command_Time As Byte


End Structure

'And then elsewhere

Dim CMD(6, 4) As Struct_Command

</pre>

That'll work fine for simply addressing the day in the cycle and the command number of that day... I think :D
PhilLenoir 3-Sep-14 11:14am    
I can see you're a C programmer. I'm an ex real-time C and Ada guy myself!

Part of our aim here is to help you learn, rather than just give quick answers.

Your approach is fine as far as it goes, but It's hard to write expressive code with this approach. OO would also make it easier to use intelligent behaviour. I see the "Packed" field. How had you planned to use this and to pack/unpack? I've not used it in years, but you may want to look at the LSET operator. I've used this to mimic C Unions in the past.

I've not used Structure definitions in VB.NET, but my understanding is that they are really classes under the hood (they certainly look a lot like it!) So if you are going down that route I'm of limited use to you. I think that you can use property methods to access and validate the members of your structure (much like a class), but you'll need to research that yourself.

You certainly seem to have a handle on a working approach. Feel free to message with any specific problems you're having and I'll help if I can.
Member 11056080 3-Sep-14 11:39am    
I really appreciate the help.

I will look into it.

Out of interest sake, the microchip has a memory size of 256 BYTES.
So to cram in all the information i'm "compressing" the information into 2 bytes, and those two bytes will then be sent to the microcontroller.

This is what the packed Uint is for, it's packed by Or-ing and AND-ing bits

Public Function UnpackCommand(Package As Struct_Command) As Struct_Command

Package.Day = CByte((Package.Packed And &HE0000000) >> 29) 'Mask with 1110 0000 0000 0000 0000 0000 0000 0000, bitshift to far right, then cast

Package.Hour = CByte((Package.Packed And &H1F000000) >> 24) '0001 1111 0000 0000 0000 0000 0000 0000

Package.Minute = CByte((Package.Packed And &HFF0000) >> 16) 'etc

Package.Valve_Number = CByte((Package.Packed And &HF000) >> 12)

Package.Command_Number = CByte((Package.Packed And &HF00) >> 8)

Package.Command_Time = CByte(Package.Packed And &HFF)

'Clear unpacked Int (make sure box is empty)

Package.Packed = 0

Return Package

End Function

Public Function PackCommand(Package As Struct_Command) As Struct_Command ' essentially the same as the above function, except reversed

Package.Packed = 0 'Make sure box is empty for OR operation

Package.Packed = ((CUInt(Package.Day << 29)) Or _
CUInt(Package.Hour << 24) Or _
CUInt(Package.Minute << 16) Or _
CUInt(Package.Valve_Number << 12) Or _
CUInt(Package.Command_Number << 8) Or _
CUInt(Package.Command_Time))

But thanx for the help again
It's 5:38 here in South Africa, so that's me for today.

Cheers!
PhilLenoir 3-Sep-14 11:47am    
You are welcome!

You could probably move these functions into property methods of the structure, but the approach looks sound. LSET was pretty much the only way of doing memory overlays in VB6 without using Windows API calls. I know that it's there in VB.NET, but it's not the same as the VB6 operator. Again, it has been a while since I've had to do bit stuffing etc.!

My first VB project (VB3) was to build a microcontroller simulator for testing our real C production code (the embedded systems were behind schedule and we wanted to test our hardware register access).

Good luck!
VB
Module Module1

    Sub Main()

        Dim Year As Year = New Year()

        Year(5).Month(11).Day(3) = 123

    End Sub

End Module

Class Year
    Inherits List(Of Year)

    Public Month(12) As Month

End Class

Class Month
    Inherits List(Of Month)

    Public Day(31) As Integer
End Class
 
Share this answer
 
Comments
Member 11056080 3-Sep-14 10:12am    
I'm a C guy, so this helps me a lot

Thank you for the reply!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900