Mr Dog
Mr Dog

Reputation: 396

Initialize Property of array or strings from custom class

I have tried a number of different things but cant seem to find the right syntax to initialize this array of strings.

I have it in a custom class

Public Class datahere

    Public Property Name As String
    Public Property parameters() As String
    Public Property elem As XElement

End Class

and I declare it like so

Dim xdata(newdata.Count) As datahere

But not sure how to go about using it. I use the other variables like so

xdata(3).Name = "TEST"

Upvotes: 0

Views: 6564

Answers (2)

matzone
matzone

Reputation: 5719

Try like this ..

First change like this

Public Property parameters As List(Of String)

And create array class

Dim ListDH as List(Of DataHere)

Dim par as New Parameter
par.Add("Any value")

Dim DH as New DataHere

DH.Name = "Test"
DH.Parameter = par
DH.Property = ....

ListDH.Add(DH)

So you can access if by

ListDH(0).Name          '-----> to get Name of first array ("TEST")

ListDH(0).Parameter(0)  '-----> to get First array of Parameter from the list ("Any value")

Upvotes: 1

NoAlias
NoAlias

Reputation: 9193

Although I would recommend using a List(of String) for your Parameters Property, if you insist on using arrays you can do the following.

First change the parameters property to the following:

Public Property parameters As String()

Keep in mind that xdata(3).parameters(0) will be nothing. To change that you would specify the number of items in the array like so:

ReDim xdata(3).parameters(0)
'Give it a value
xdata(3).parameters(0) = "Test 1"

If you want to add additional items you have to redefine the array. To prevent losing your existing data use the Preserve Keyword:

ReDim Preserve xdata(3).parameters(1)
'Give the second item in the array a value
xdata(3).parameters(1) = "Test 2"

To get your values is pretty straight forward:

Dim strSecondParameters As Strign = xdata(3).parameters(1)

Upvotes: 1

Related Questions