Having massive brain drain! How to create a new instance of a class array?

Hello all,

After all of these years, I’ve finally lost my marbles. Can someone please help me find them??

I have a Private Property TwilioSocket() As clsTwilioSocket in one class.
I need to create a new instance of this TwilioSocket array.

Tried this but got an error: Type mismatch. Expected class clsTwilioSocket but got class clsTwilioSocket. Makes no sense but???

TwilioSocket = New clsTwilioSocket
Dim Idx As Integer = TwilioSocket.LastIndex
TwilioSocket(Idx).Startup(eMFA_Transaction.RowID, Idx )

Also tried to use the Add function - Twilio.add and Twilio.AddAt function but they appear to be incorrect.

I’m sure its very simple but for some brain drain fog, I’ve forgotten and have not been able to find the correct solution.

Help anyone?

Thanks,
Tim

I guess you want to say:

I need to create a new instance of this class and add it to the property array…

Something like:

Public Class TSocket

  Public Sub DoSomething()
    System.DebugLog "Boo"
  End Sub

End Class


Private Property arrTSocket() As Tsocket


Private Sub AddNewTSocket()
  Var ts As New TSocket
  arrTSocket.Add ts     // put it in the array
  ts.DoSomething        // call something there
End Sub

On the first line, your using TwilioSocket as a singe value. But you also have a propery which is an array that has the same name. Use some other name on that first line. You may want to Var a local variable to do it, since you’re going to add it to the array anyway.

2 Likes

Your “initialization” line is incorrect.

Instead of this

TwilioSocket = New clsTwilioSocket

Use this

TwilioSocket.Add New clsTwilioSocket

2 Likes
TwilioSocket.Add(New clsTwilioSocket)
Dim Idx As Integer = TwilioSocket.LastIndex
TwilioSocket(Idx).Startup(eMFA_Transaction.RowID, Idx )
// This is the same but optimized (no need to fetch the recent object from the array)
Var ts As New clsTwilioSocket
TwilioSocket.Add(ts)
ts.Startup(eMFA_Transaction.RowID, TwilioSocket.LastIndex )

By the way, having a method passing the index of self does not make a lot of sense, probably passing the ref of the obj (self) around would be better (making this specific call even shorter and faster)… But without knowing the logic involved here I’m not sure.

It would end as:

Var ts As New clsTwilioSocket
TwilioSocket.Add(ts)
ts.Startup(eMFA_Transaction.RowID)      // Startup just knows self (the ts instance)

I tend to always make my array names plural to remind me that they’re arrays, e.g.

TwilioSockets() As clsTwilioSocket

9 Likes