I seem to have run into an issue with using structures with a command line Visual Basic .NET (core 5) project. Within it I have a class which handles a command queuing/buffering system.
Here's the Class code:
Public Class ClassCommandBuffer
' CmdMax is the maximum number of commands allowed within a set (0 to CmdMax -1)
Public Const MaxCmdSets As Integer = 30
Public Const MaxNoofCmdLines As Integer = 30
' CurrentNestIndex contains the current nest level. If this is set to -1 then buffer comamnds aren't being run
' NestedMaxIndex is the maximum number of nested buffer command sets allowed (0 to NextedMax -1)
' CmdMax is the maximum number of commands allowed within a set (0 to CmdMax -1)
Public Structure CmdBufferCmdStruct
Public CmdLines() As String ' Array of Command Lines within current set
Public NoofCmds As Integer ' Number of Command Lines
Public CurrentIndex As Integer ' Usually set to -1, but if cmdset being run then reflects the current cmd index
End Structure
Public Structure CmdBufferLoadingStruct
Public Index As Integer ' When loading a command set in, this will be set to the new command set index, otherwise it's -1
Public CmdIndex As Integer ' When loading a commadn set in, this is the command index of the next command to be loaded, otherwise -1
End Structure
Public Structure CmdBufferStruct
Public Loading As CmdBufferLoadingStruct ' Used when loading a new command set
Public CmdSet() As CmdBufferCmdStruct ' Array of command sets
Public CurrentNestIndex As Integer ' Current nested index being processed
End Structure
Public CmdBuffer As CmdBufferStruct
' --- Initialise values & setup ---
Public Sub New()
ReDim CmdBuffer.CmdSet(MaxCmdSets) ' declates 30 sets
ReDim CmdBuffer.CmdSet(MaxCmdSets).CmdLines(MaxNoofCmdLines) ' declates 30 sets of 30 cmds each
CmdBuffer.CurrentNestIndex = -1 ' initialise the current nest index to -1 (buffer cmds not running)
CmdBuffer.Loading.Index = -1
CmdBuffer.Loading.CmdIndex = 0
Console.WriteLine("> New")
End Sub
' --- Command Sets ---
Public Sub NewCmdSetStart()
Console.WriteLine("> NewCmdSetStart")
With CmdBuffer
.Loading.Index = .CurrentNestIndex + 1
.Loading.CmdIndex = 0
End With
End Sub
Public Sub NewCmdSetEnd()
Console.WriteLine("> NewCmdSetEnd")
With CmdBuffer
.CmdSet(.Loading.Index).NoofCmds = .Loading.CmdIndex
.Loading.Index = -1
.CurrentNestIndex = .CurrentNestIndex + 1
End With
End Sub
Public Sub NewCmdSetInsert(newcmd As String)
Console.WriteLine("> NewCmdSetInsert : " & newcmd)
With CmdBuffer
.CmdSet(.Loading.Index).CmdLines(.Loading.CmdIndex) = newcmd
Dim x As Integer = .Loading.CmdIndex + 1
.Loading.CmdIndex = x
End With
End Sub
' --- Command processing ---
Public Function GetNextCmd() As String
' check to see if we've hit the end cmds or not running from buffer
If CmdBuffer.CurrentNestIndex = -1 Then
Console.WriteLine("Error: We're not running buffer commands! Why was GetNextCmd called? Line " & CMDlineCount)
EndAssembler()
End If
Dim CurCmdLine As Integer = CmdBuffer.CmdSet(CmdBuffer.CurrentNestIndex).CurrentIndex
Dim CurCmdNoof As Integer = CmdBuffer.CmdSet(CmdBuffer.CurrentNestIndex).NoofCmds
If CurCmdLine >= CurCmdNoof Then
Console.WriteLine("Error: We've reach the end of cmds! Why was GetNextCmd called? Should check before calling. Line " & CMDlineCount)
EndAssembler()
End If
Return CmdBuffer.CmdSet(CmdBuffer.CurrentNestIndex).CmdLines(CurCmdLine)
End Function
Public Function IsBufferRunning() As Boolean
If CmdBuffer.CurrentNestIndex = -1 Then Return False Else Return True
End Function
End Class
The class is defined in the MainModule thus:
Module ModuleMain
Public CommandBuffer As New ClassCommandBuffer
Sub Main(args As String())
And the calling code looks like this:
With ModuleMain.CommandBuffer
.NewCmdSetStart()
.NewCmdSetInsert(ifCmd)
.NewCmdSetEnd()
End With
IfCmd is a string variable which contains the command to be stored.
The issue I'm running into is that when 'NewCmdSetInsert(ifCmd)' is called, it crashes out on the line within the function : 'Dim x As Integer = .Loading.CmdIndex + 1' with the following error: 'System.NullReferenceException: 'Object reference not set to an instance of an object.'' The original statement was
.Loading.CmdIndex = .Loading.CmdIndex + 1
But I separated it out with the temporary Dim x... line to try to see where the issue is.
It seems to be saying that I've not defined the structure as new, but I have in MainModule. Also, in the 'New' section of the class I've set an initial value for .Loading.CmdIndex I don't quite understand what the issue is as I get the following output which indicates that 'New' is run first (thus initialising the arrays & values): It breaks at this point with the above error and highlights (ignore 'BCC 20480' as this is command string passed to NewCmdSetInsert)
New
NewCmdSetStart
NewCmdSetInsert : BCC 20480
Anyone got any ideas?