Robert Bjork
Robert Bjork

Reputation:

VB.NET syntax for arrays of objects

What is the VB.NET syntax for declaring the size of an array of objects at runtime?

To get an idea of what I mean, here is the code so far:

Private PipeServerThread As Thread()

Public Sub StartPipeServer(NumberOfThreads As Integer)
    ' ??? equivalent of C#
    ' ???   PipeServerThread = new Thread[numberOfThreads];
    ' ??? goes here
    For i = 0 To NumberOfThreads - 1
        PipeServerThread(i) = New Thread(New ThreadStart(AddressOf ListeningThread))
        PipeServerThread(i).Start()
    Next i
End Sub

I've tried several things but just end up conflating it with object creation syntax.

Upvotes: 2

Views: 1591

Answers (2)

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421978

PipeServerThread = New Thread(numberOfThreads - 1) { }

Alternatively:

ReDim PipeServerThread(numberOfThreads - 1)

Remember that the value inside parenthesis is the upper bound of the array in VB.NET (unlike C# where it's array length).

Upvotes: 4

Noldorin
Noldorin

Reputation: 147270

This should be what you want:

ReDim PipeServerThread(numberOfThreads - 1)

You can't use the New keyword, since the VB.NET compiler interprets this as an attempt to create a new instance of the type Thread.

Upvotes: 2

Related Questions