|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Structure with List(Of) Exception !?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
AGP wrote: Show quoteHide quote > VB2005 zTrip.Roads = New List(Of tRoad)> 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) zRoad.RoadSegs = New List(Of tRoadSeg)> > 'create a road > zRoad.id = 300 > zRoad.parts = 5 Show quoteHide quote > 'all road segments start with the city where we start the trip so we I suggest using a Class instead of a Structure. It enables you to declare > 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. 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 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 AGP wrote:
> thanks for the tip. let me digest this first and then ill look into the I agree with Armin, those should be classes. Always use classes unless > 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 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 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
Visual Studio 2008 and Classes Inheriting From System.Web.UI.WebControls.Style
Problem with embedded carriage returns trouble reading word documents Good tutorial for working with XML Using function with PChar data type problem reading array data from structure still problems reading word documents... how to know if to close sqlreader Multiple File Select Not Working In Published ClickOnce Application Loading an XML Document? |
|||||||||||||||||||||||