Problems with Declarations

AndrewD2

New member
Joined
Aug 10, 2004
Messages
1
Programming Experience
1-3
In the form I'm working on I've created a structure so that I can have arrays that contain a string and an integer.
VB.NET:
Structure SpecialAbilities 
	 Public Abilities As String
	 Public Value As Integer
End Structure
I then declare my variable
VB.NET:
Public MeleeSpecials() as SpecialAbilities
And my problems come when I attempt to populate the arrays
VB.NET:
MeleeSpecials().Abilities = { *various strings* } 
MeleeSpecials().Value = { *various integers* }
It keeps telling me that MeleeSpecials in the populating part, before ().Abilities and ().Value has a "Declaration Expected" error but I can't figure out why, I've declared it and it's getting quite annoying.

Any help would be great,
Andrew
 

bpl

Member
Joined
Jun 3, 2004
Messages
7
Programming Experience
10+
Do this:
MeleeSpecials(0) = New SpecialAbilities("string1", integer1)​


MeleeSpecials(1) = New SpecialAbilities("string2", integer2)



MeleeSpecials(2) = New SpecialAbilities("string3", integer3)





 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
bpl's suggestion will work only if you include a 'New' procedure that accepts the values of 'Abilities' and 'Value' as parameters as shown here:
VB.NET:
Friend Structure SpecialAbilities
    Public Abilities As String
    Public Value As Integer

    Public Sub New(ByVal strAbilities As String, ByVal iValue As Integer)
        Abilities = strAbilities
        Value = iValue
    End Sub
End Structure

Then you can do this:
VB.NET:
Friend MeleeSpecials() As SpecialAbilities

Sub test()
    MeleeSpecials(0) = New SpecialAbilities("string1", 10)
End Sub

Without the 'New' procedure, you would have to do something like this (remember to include the array index):

VB.NET:
Sub test2()
    MeleeSpecials(0).Abilities = "string1"
    MeleeSpecials(0).Value = 10
End Sub
 
Top Bottom