Structures and Late Binding

dpflagg

Newcomer
Joined
Jun 28, 2003
Messages
4
Location
Interlaken, NY
I have a data structure that is known at compile time, so I thought I would put it in a structure. However, it seems like I am late binding, but I have no intention to (I didn't even know what it was before I got an error, because the Compact Framework doesn't support it).

Here is a snippet:



Visual Basic:
Enum commandEnum
        ReadCommand
        WriteCommand
        OtherCommand
    End Enum

    Public Structure commandStruct
        Dim commandType As commandEnum
        Dim commandCode As String
        Dim numBytes As Integer
        Dim text As String
        Public Sub New(ByVal commandType As commandEnum, _
                       ByVal commandCode As String, _
                       ByVal numBytes As Integer, _
                       ByVal text As String)
            Me.commandType = commandType
            Me.commandCode = commandCode
            Me.numBytes = numBytes
            Me.text = text
        End Sub
    End Structure

    Public resetCommand = New commandStruct(commandEnum.OtherCommand, "0x01", 0, "Reset MPU")


Now, when I try to access the data, like txtBox.Text = resetCommand.text, it says I'm late binding.

How should I be doing this differently?

Thanks!
 
Hi
Its late binding coz you havent given the type while declaring the object.

try this

Public resetCommand As commandStruct

resetCommand = New commandStruct(commandEnum.OtherCommand, "0x01", 0, "Reset MPU")

Then try to access the structure!
 
No, you are declaring it once, and then setting that variable to a new instance. When you declare a structure or class without setting it to New, you are creating a null reference to a commandStruct, meaning that it points nowhere, and you can't use any non-static class members. Once you use New, it creates a new instance and assigns it to the reference variable, rendering it usable.

Visual Basic:
'This creates an empty (null) reference to a commandStruct
Public resetCommand As commandStruct

'This creates an instance of a commandStruct and makes resetCommand reference it
resetCommand = New commandStruct(commandEnum.OtherCommand, "0x01", 0, "Reset MPU")
 
Back
Top