VB.Net property with structure array
Question:
I am building a wrapper class for a web service and I’m having problems using an array of a structure in my class. Abbreviated form of the code is below. In the UserVehicles method trying to set the value for the name property of private var pVehicles causes a NullReferenceException. – pVehicles(1).Name = “test” is the offending line.
Ultimately, I want to be able to reference the vehicle properties like this: TruckInterface.vehicles(1).TruckName
Also there are other properties and methods (other methods not shown) that are working fine. Its only my attempt to use this array of structure that fails.
I’m probably missing something vary simple here.
Public Class TruckInterface
‘vars for status and session
Private pStatus As String
Private pCode As String
Private pMessage As String
Private pGuid As String
‘vars for truck list
Public Structure vehicle
Public TruckNumber As String
Public TruckIp As String
Public TruckMobileId As String
Public TruckName As String
End Structure
Private pVehicles() As vehicle
‘properties for Validate function
Public ReadOnly Property Status() As String
Get
Return pStatus
End Get
End Property
Public ReadOnly Property Code() As String
Get
Return pCode
End Get
End Property
Public ReadOnly Property Message() As String
Get
Return pMessage
End Get
End Property
Public ReadOnly Property Guid() As String
Get
Return pGuid
End Get
End Property
Public ReadOnly Property vehicles() As vehicle()
Get
Return pVehicles
End Get
End Property
Public Sub UserVehicles(ByVal vUser As String, ByVal vPassword As String, ByVal vAppID As String, ByVal vGuid As String)
pVehicles(1).TruckName = “test”
Form1.txtdebug.Text = pVehicles(1).TruckName
End Sub
End Class
Solution:
Have you tried to initializate the array? The array does not get created or get resized automatically (use a List for that), so you need to initialize in advance. That’s the reason for the NullReferenceException













Comments (0)
Trackbacks - Pingbacks (0)
Leave a Reply