0

I have the following declared in a module (simplified for example):

Public Structure ActiveDeviceInfo
    Dim InStream As String
    Dim InMsg() As String
    Dim OutMsg() As String

    Public Sub Initialize()
        ReDim InMsg(30)
        ReDim OutMsg(30)
    End Sub
End Structure

then right after that I create the instance for the module. I need it to have the scope of the whole module instead of the individual subroutines.

Public ActiveRelay As New ActiveDeviceInfo
ActiveRelay.Initialize()

I'm getting the 'Declaration Expected' error on the Initialization call.
Any ideas on how to fix this?

2 Answers 2

2

You could add it in the static constructor for the module:

Public Module Module1
    Public ActiveRelay As New ActiveDeviceInfo

    Sub New()
        ActiveRelay.Initialize()
    End Sub

    'Struct Here
End Module
Sign up to request clarification or add additional context in comments.

Comments

1

Just inside the module definition I would put:

Dim ActiveRelay As New ActiveDeviceInfo

And then in individual subroutines just call:

ActiveRelay.Initialize()

If you want to call an initialized when an object is created one could switch to a Class with a constructor.Something like:

Dim ActiveRelay As New ActiveDeviceInfo

Public Class ActiveDeviceInfo
    Dim InStream As String
    Dim InMsg() As String
    Dim OutMsg() As String

    Public Sub Initialize()
        ReDim InMsg(30)
        ReDim OutMsg(30)
    End Sub

    Sub New()
        Initialize()
    End Sub
End Class

This would run New() when the class is instantiated.

4 Comments

Yes, but it would then reinitialize the arrays, and I'm looking more for a global/static concept.
Is it required you create this as a Structure. How about as a Class?
No, not required. I'm converting VB6 code over and for now I'm just trying to get it to work, then I'll go back later and re-factor.
Thanks, I may use that when I refactor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.