2

I dont know Why I am having trouble with this, but I keep getting 'not set to an instance of an object' exception every time.

Does this make sense?

I have this declared in the main form

Private _Paths() As System.Drawing.Drawing2D.GraphicsPath

and do this in a sub

 _Paths(20) = New GraphicsPath

But for whatever reason I get an object reference error on the second line. Any help?

After the decleration, I want to then go ahead and add a line to the graphics path like so

 _Paths(k).AddLine(x_loc(k), y_loc(k), x_loc(k + 1), y_loc(k + 1))

As per suggestion to use list:

Declared in main class

Private _Paths As List(Of System.Drawing.Drawing2D.GraphicsPath)

using in sub

for k = 0 to 10
      'x_loc and y_loc calculations are done here

    _Paths.Add(New GraphicsPath)
    _Paths(k).AddLine(x_loc(k), y_loc(k), x_loc(k + 1), y_loc(k + 1))
next

still get error when trying to create new instance of graphicspath

There should be no reason this error should pop up right? enter image description here

Private _Paths As NEW List(Of System.Drawing.Drawing2D.GraphicsPath)
Mr Dog
  • 396
  • 1
  • 3
  • 22

2 Answers2

2

Your not redimensioning your array, instead use a List(Of GraphicsPath) and just .Add them as you need.

Dim myPaths As New List(Of GraphicsPath)
'later in code
myPaths.Add(New GraphicsPath)
myPaths(0).AddLine(...)'etc...
OneFineDay
  • 9,004
  • 3
  • 26
  • 37
  • Sorry I am not sure how I would use the list. Right now after declaring the graphics path I do _Paths(k).AddLine(x_loc(k), y_loc(k), x_loc(k + 1), y_loc(k + 1) how would I add that to a list? – Mr Dog May 02 '13 at 14:15
  • You add a new GraphicsPath whenever you need to, then you can still access them by index like above. You just need to figure out when that happens. – OneFineDay May 02 '13 at 14:21
  • Thanks. I havent used lists much. I still seems to be getting a not set to an instance, except at the line _Paths.Add(New GraphicsPath) – Mr Dog May 02 '13 at 14:27
  • That is weird, I have attached a picture of a test I ran. Do you mind taking a look? It should just add those? – Mr Dog May 02 '13 at 14:45
  • Got it, forgot to put the 'New'. Thanks a lot for the help :D – Mr Dog May 02 '13 at 15:02
  • You have to declare your list as `New`. Look at the code again. – OneFineDay May 02 '13 at 15:03
1

A list must be declared with New

Dim YourList As New List(Of GraphicsPath)

I notice in your screenshot you are not actually adding new GraphicsPath objects You are not giving the parameters to create one

Dim Rec As New Rectangle(LocationX,LocationY, Width,Height) 'Create a binding rectangle to contain the graphic
Yourlist.Add(New GraphicsPath {Rec}) 'In place of 'Rec' you can also specify parameters directly

OR

Yourlist.Add(New GraphicsPath {LocationX,LocationY, Width,Height})
ChaosOverlord
  • 56
  • 1
  • 5