How do I declare an array of structures? Do I use NEW?

N

neuromancer

I have a structure that has members that are other structures or
enums.
Here it is:

Public Structure ColumnType
dim Name As String
dim ConsecType As enumConsecutiveTypes
dim AllowedTextValues() As structTextValueAndCount /*ARRAY*/
dim Dependent As Boolean
End Structure

Then I declare:
Public gColumnStruct As New ColumnType()

But when I try and refer to "gColumnStruct.Dependent", I'm told that
'Dependent' is not a member. Whats wrong with the declaration?

Also, in the structure, I have another structure:
dim AllowedTextValues() As structTextValueAndCount
But I'm not sure if this is correct. Do I need to use the word NEW,
as in:
dim AllowedTextValues() As NEW structTextValueAndCount

And even if I did, would that allocate space for every member of the
array?
Thanks,
FN13
 
A

Armin Zingler

Am 15.10.2010 16:02, schrieb neuromancer:
I have a structure that has members that are other structures or
enums.
Here it is:

Public Structure ColumnType
dim Name As String
dim ConsecType As enumConsecutiveTypes
dim AllowedTextValues() As structTextValueAndCount /*ARRAY*/
dim Dependent As Boolean
End Structure

Then I declare:
Public gColumnStruct As New ColumnType()

But when I try and refer to "gColumnStruct.Dependent", I'm told that
'Dependent' is not a member. Whats wrong with the declaration?

I can't repro the problem. This works here:

Dim gColumnStruct As New ColumnType()
gColumnStruct.Dependent = True

You don't need "New" here because a Structure is a value type.
If you drop it, also remove the braces, otherwise you'd declare
an array variable.
Also, in the structure, I have another structure:
dim AllowedTextValues() As structTextValueAndCount
But I'm not sure if this is correct. Do I need to use the word NEW,
as in:
dim AllowedTextValues() As NEW structTextValueAndCount


In this way, 'New' is not allowed, because, if you create an array, you
must specify it's length. In this case, use ReDim:

Dim gColumnStruct As ColumnType

ReDim gColumnStruct.AllowedTextValues(12)

And even if I did, would that allocate space for every member of the
array?

Yes, it does. All members get their default values.


To be safe, I recommend to write a Class instead of a Structure because then
you can write

Public AllowedTextValues(35) As Integer
 

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