Structure with List(Of) Exception !?

  • Thread starter Thread starter AGP
  • Start date Start date
A

AGP

VB2005
Having trouble populating a structure that has a List(Of) component. The
concept is a trip consists of several roads and each road consists of
several segments. here is my declaration and a typical assignment and the
place where i get an exception:

Public Structure tRoadSeg
Dim startpt As String
Dim endpt As String
Dim markers As Integer
Dim itype As Integer
End Structure

Public Structure tRoad
Dim name As String
Dim id As Integer
Dim parts As Integer
Dim RoadSegs As List(Of tRoadSeg)
End Structure

Public Structure tTrip
Dim stdate As DateTime
Dim sFrom As String
Dim sTo As String
Dim wx As String
Dim vehid As Integer
Dim driver As String
Dim Roads As List(Of tRoad)
End Structure


'a collection of trips
Dim zTrips As List(Of tTrip) = New List(Of tTrip)
'a single trip
Dim zTrip As tTrip = Nothing
'a single road
Dim zRoad As tRoad = Nothing

'add one trip to the trip collection
zTrip.driver = "Me"
zTrip.sFrom = "Phoenix"
zTrip.sTo = "Seattle"
zTrip.vehid = 0
zTrips.Add(zTrip)

'create a road
zRoad.id = 300
zRoad.parts = 5

'all road segments start with the city where we start the trip so we create
a road segment
Dim roadSegAtStart As tRoadSeg = New tRoadSeg
roadSegAtStart.endpt = zTrip.sFrom

'and add that as the first road segment element in our road. we will add
more segments later
zRoad.RoadSegs.Add(roadSegAtStart) <----- ***** Exception here *****

'and add our road to our trip
zTrip.Roads.Add(zRoad) <----- ***** Exception here *****

The exception is of type System.NullReferenceException: Object reference not
set to an instabce of an object. I just cant figure out why Im getting this
exception. Im almost certain im not initializing something properly but cant
put my finger on it. Any help is appreciated.

AGP
 
s. inline

VB2005
Having trouble populating a structure that has a List(Of) component.
The concept is a trip consists of several roads and each road
consists of several segments. here is my declaration and a typical
assignment and the place where i get an exception:

Public Structure tRoadSeg
Dim startpt As String
Dim endpt As String
Dim markers As Integer
Dim itype As Integer
End Structure

Public Structure tRoad
Dim name As String
Dim id As Integer
Dim parts As Integer
Dim RoadSegs As List(Of tRoadSeg)
End Structure

Public Structure tTrip
Dim stdate As DateTime
Dim sFrom As String
Dim sTo As String
Dim wx As String
Dim vehid As Integer
Dim driver As String
Dim Roads As List(Of tRoad)
End Structure


'a collection of trips
Dim zTrips As List(Of tTrip) = New List(Of tTrip)
'a single trip
Dim zTrip As tTrip = Nothing
'a single road
Dim zRoad As tRoad = Nothing

'add one trip to the trip collection
zTrip.driver = "Me"
zTrip.sFrom = "Phoenix"
zTrip.sTo = "Seattle"
zTrip.vehid = 0


zTrip.Roads = New List(Of tRoad)

zTrips.Add(zTrip)

'create a road
zRoad.id = 300
zRoad.parts = 5


zRoad.RoadSegs = New List(Of tRoadSeg)

'all road segments start with the city where we start the trip so we
create a road segment
Dim roadSegAtStart As tRoadSeg = New tRoadSeg
roadSegAtStart.endpt = zTrip.sFrom

'and add that as the first road segment element in our road. we will
add more segments later
zRoad.RoadSegs.Add(roadSegAtStart) <----- ***** Exception here *****

'and add our road to our trip
zTrip.Roads.Add(zRoad) <----- ***** Exception here *****

The exception is of type System.NullReferenceException: Object
reference not set to an instabce of an object. I just cant figure out
why Im getting this exception. Im almost certain im not initializing
something properly but cant put my finger on it. Any help is
appreciated.


I suggest using a Class instead of a Structure. It enables you to declare
the members "As New list(Of)". You can also directly change field/property
values of items in the list then, because they are reference types.


Armin
 
s. inline

VB2005
Having trouble populating a structure that has a List(Of) component.
The concept is a trip consists of several roads and each road
consists of several segments. here is my declaration and a typical
assignment and the place where i get an exception:

Public Structure tRoadSeg
Dim startpt As String
Dim endpt As String
Dim markers As Integer
Dim itype As Integer
End Structure

Public Structure tRoad
Dim name As String
Dim id As Integer
Dim parts As Integer
Dim RoadSegs As List(Of tRoadSeg)
End Structure

Public Structure tTrip
Dim stdate As DateTime
Dim sFrom As String
Dim sTo As String
Dim wx As String
Dim vehid As Integer
Dim driver As String
Dim Roads As List(Of tRoad)
End Structure


'a collection of trips
Dim zTrips As List(Of tTrip) = New List(Of tTrip)
'a single trip
Dim zTrip As tTrip = Nothing
'a single road
Dim zRoad As tRoad = Nothing

'add one trip to the trip collection
zTrip.driver = "Me"
zTrip.sFrom = "Phoenix"
zTrip.sTo = "Seattle"
zTrip.vehid = 0


zTrip.Roads = New List(Of tRoad)

zTrips.Add(zTrip)

'create a road
zRoad.id = 300
zRoad.parts = 5


zRoad.RoadSegs = New List(Of tRoadSeg)

'all road segments start with the city where we start the trip so we
create a road segment
Dim roadSegAtStart As tRoadSeg = New tRoadSeg
roadSegAtStart.endpt = zTrip.sFrom

'and add that as the first road segment element in our road. we will
add more segments later
zRoad.RoadSegs.Add(roadSegAtStart) <----- ***** Exception here *****

'and add our road to our trip
zTrip.Roads.Add(zRoad) <----- ***** Exception here *****

The exception is of type System.NullReferenceException: Object
reference not set to an instabce of an object. I just cant figure out
why Im getting this exception. Im almost certain im not initializing
something properly but cant put my finger on it. Any help is
appreciated.


I suggest using a Class instead of a Structure. It enables you to declare
the members "As New list(Of)". You can also directly change field/property
values of items in the list then, because they are reference types.


Armin
 
thanks for the tip. let me digest this first and then ill look into the
class vs structure. for this project the process is a fairly simple one so I
may not ahve to go with the class but i will look into it.

AGP
 
thanks for the tip. let me digest this first and then ill look into the
class vs structure. for this project the process is a fairly simple one so I
may not ahve to go with the class but i will look into it.

AGP
 
AGP said:
thanks for the tip. let me digest this first and then ill look into the
class vs structure. for this project the process is a fairly simple one so I
may not ahve to go with the class but i will look into it.

AGP

I agree with Armin, those should be classes. Always use classes unless
you have a specific reason to use structures.

Also, the structure tTrip exceeds the recommended limit of 16 bytes, so
you lose any performance benefits of making a structure.
 
AGP said:
thanks for the tip. let me digest this first and then ill look into the
class vs structure. for this project the process is a fairly simple one so I
may not ahve to go with the class but i will look into it.

AGP

I agree with Armin, those should be classes. Always use classes unless
you have a specific reason to use structures.

Also, the structure tTrip exceeds the recommended limit of 16 bytes, so
you lose any performance benefits of making a structure.
 
You both make a good point. I just finished the code to get the process
working with a Structure. Ill convert it to a class now that the process
itself is working as designed. Thanks for the Help.

AGP
 
You both make a good point. I just finished the code to get the process
working with a Structure. Ill convert it to a class now that the process
itself is working as designed. Thanks for the Help.

AGP
 
Back
Top