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)
有帮助吗?

解决方案

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...

其他提示

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})
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top