How to declare arrays within a structure?

J

John Dann

Trying to declare a structure that will contain a couple of fixed-size
arrays. I'm trying eg:

Structure IndexRecord
Dim testarray(16) as Byte
etc
End Structure

But vb.net is refusing to let me do this saying that I can't declare a
structure with fixed size arrays. Yet the book I'm looking at shows
this as an example, albeit not with byte as the variable type.

Is there something in the VB environment that I haven't set (it is a
new vb.net installation). If not then how do I approach this please?

JGD
John Dann
www.weatherstations.co.uk (major update now online)
 
O

One Handed Man \( OHM - Terry Burns \)

Either use a class instead or do this or something like it.


Structure indexrecord
Private m() As Byte

Sub New(ByVal i() As Byte)
ReDim m(i.Length)
m = i
End Sub

End Structure


HTH
 
J

John Dann

Either use a class instead or do this or something like it.

OK - many thanks. But are there are pointers as to when to use
structure or class - I'm just trying to migrate to .net from VB6 and
it's more familiar to use a UDT/structure. But perhaps I really should
be using a class here?

Maybe I can summarise what I'm trying to do in what is really a
project for learning VB.net, though hopefully will be of some
practical use too.

I need to read a binary disk file which will be of the form:

Structure1 (a header structure)
Structure2 (data records)
Structure2
Structure2 etc

Both Structure1 and Structure2 will have complex but fully-defined
internal formats. There may be many thousands of Structure2 records in
the file. (NB This is a database in a generic sense, but not one in
any standard SQL/Access format, so AFAICS I need to read it structure
by structure as a binary stream.)

I was expecting to to declare structures for Structure1 and Structure2
and then maybe to read them in using a Deserialise approach. I was
also thinking, bearing in mind that fairly large data volumes might be
involved, that structure - being closer (?) to standard variables -
might be more efficient, maybe by quite a margin, than using classes.
But this was simply a hunch on my part that might well be wrong.

I'd be very grateful for any further comments.

JGD
 
O

One Handed Man \( OHM - Terry Burns \)

I think that the performance issue may possibly be what makes the
difference, you can do quite a lot with Structures and for what you are
likely to need them for they should suffice.

We can quess at the aggregate cost of performance, but in reality, the only
way to check is to have some empirical data to go on.

before you finalise your design, I would create a structure and a class and
create 4/5 thousand of them in a test scenario and then read the data in and
see how things differ from a class approach.

The last thing you want to do is put loads of effort into this and find you
need to change direction. I strongly recommend a test first , which is in
fact part of a good design in these sorts of situations.

HTH
 
J

Jay B. Harlow [MVP - Outlook]

John,
OK - many thanks. But are there are pointers as to when to use
structure or class
I will use a structure when the value needs to:

1. Act like a primitive type
2. Has an instance size under 16 bytes
3. Are immutable
4. Value semantics are desirable

http://msdn.microsoft.com/library/d...genref/html/cpconValueTypeUsageGuidelines.asp
But perhaps I really should
be using a class here?
From your description it does not sound like either type is a good candidate
for being a Structure.

I would make both types classes.

Hope this helps
Jay
 
J

John Dann

Either use a class instead or do this or something like it.

Structure indexrecord
Private m() As Byte

Sub New(ByVal i() As Byte)
ReDim m(i.Length)
m = i
End Sub

End Structure

Could I get some clarification as to what's happening here please?

OK I understand (or at least can accept) that you can't simultaneously
declare and initialise a fixed size array in a structure and need to
set an explicit constructor.

But my next naive idea would have been:

Structure indexrecord
Private m() As Byte
Sub New()
ReDim m(16) 'Since I know that the value always needs to be
eg 16
End Sub
End Structure

But this causes an error too. So presumably I simply can't set the
array dimension within the structure declaration at all but need to
wait until an instance of the structure is created (if that's the
right terminology in respect of structures).

But why can't I do eg:

Structure indexrecord
Private m() As Byte
Sub New(Byval i as Short) 'Short as an example
ReDim m(i)
End Sub
End Structure

Then

Dim testrecord as indexrecord = new indexrecord(16)

or some such syntax.

Thanks for any further clarification.

JGD
 
J

Jay B. Harlow [MVP - Outlook]

John,
But why can't I do eg:

Structure indexrecord
Private m() As Byte
Sub New(Byval i as Short) 'Short as an example
ReDim m(i)
End Sub
End Structure

Who says you cannot, have you tried it?

ReDim m(16) 'Since I know that the value always needs to be
eg 16

Remember that when you define the array you are defining the upper bound,
not the number of elements. So "ReDim m(16)" will create an array with 17
elements (0 to 16).

Hope this helps
Jay
 
H

Herfried K. Wagner [MVP]

* John Dann said:
But my next naive idea would have been:

Structure indexrecord
Private m() As Byte
Sub New()
ReDim m(16) 'Since I know that the value always needs to be
eg 16
End Sub
End Structure

Doesn't work.
But this causes an error too. So presumably I simply can't set the
array dimension within the structure declaration at all but need to
wait until an instance of the structure is created (if that's the
right terminology in respect of structures).

But why can't I do eg:

Structure indexrecord
Private m() As Byte
Sub New(Byval i as Short) 'Short as an example
ReDim m(i)
End Sub
End Structure

You actually can do that.
 
J

John Dann

You actually can do that.

OK - many thanks for the confirmation. I think that OHM's original
example was perhaps more generalised than I was expecting and I
concluded incorrectly that a simpler solution wouldn't work.

JGD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top