Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Require a Container as a Global which contains an array 1

Status
Not open for further replies.

BJZeak

Programmer
May 3, 2008
230
0
16
CA
I need a container to hold an array ... thought I would be able to use a structure but an array's size can't be defined in the structure ... the examples suggest this is done using a redim statement but this is also an issue because redim is not allowed outside a sub or function ... I need these containers to be static/global. I was wondering if there is a way to define the array for this example? Or do I need to define this Struct as another Class? Either way I would like to generate a global container that includes an array.

' VB 2010 Express

Public CLASS X
Public Structure Fifo
DIM Head as Integer
DIM Tail as Integer
DIM Count as Integer
DIM Buf(50)as String ' causes error Array Size can't be assigned in structure
End Structure

Public Alpha As New Fifo
Public Beta As New Fifo

Private Sub Form_Load()

DIM inI as Integer

' init
Alpha.Head = 0
Alpha.Tail = 0
Alpha.Count = 0
Beta.Head = 0
Beta.Tail = 0
Beta.Count = 0

for inI = 0 to 50 step 1
Alpha.buf(inI) = ""
Beta.buf(inI) = ""
next

End Sub

End Class
 

Use a constructor for your class and call Fifo through an instance of the class:

Code:
Public Class Class1

    Protected Friend Structure structFifo
        Dim Head As Integer
        Dim Tail As Integer
        Dim Count As Integer
        Dim Buf() As String
    End Structure

    Friend Fifo As structFifo

    Public Sub New(Optional ByVal BufSize As Integer = 50)

        Fifo = New structFifo

        ReDim Fifo.Buf(BufSize)

    End Sub

End Class

To use the class:

Code:
Dim c1 As New Class1

MsgBox(c1.Fifo.Buf.Length)

Dim c2 As New Class1(75)

MsgBox(c2.Fifo.Buf.Length)

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top