Home All Groups Group Topic Archive Search About

Structure with List(Of) Exception !?

Author
22 May 2009 12:43 PM
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

Author
22 May 2009 1:34 PM
Armin Zingler
s. inline


AGP wrote:
Show quoteHide quote
> 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)


Show quoteHide quote
> '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
Author
24 May 2009 7:03 PM
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

Show quoteHide quote
"Armin Zingler" <az.nospam@freenet.de> wrote in message
news:Od4pmIu2JHA.140@TK2MSFTNGP03.phx.gbl...
> s. inline
>
>
> AGP wrote:
>> 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
Author
25 May 2009 12:50 AM
Göran_Andersson
AGP wrote:
> 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.

--
Göran Andersson
_____
http://www.guffa.com
Author
25 May 2009 6:16 PM
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

Show quoteHide quote
"Göran Andersson" <gu***@guffa.com> wrote in message
news:uQmoMLN3JHA.1096@TK2MSFTNGP06.phx.gbl...
> AGP wrote:
>> 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.
>
> --
> Göran Andersson
> _____
> http://www.guffa.com