Creating an array of graphic paths in vb.net - vb.net

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?
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})

Related

How to assign int value which is saved as a point in draw rectangle to point?

point.X = midpoint.X
point.Y = midpoint.Y - value.Height
Dim rc As New Rectangle(New Point(point.X, point.Y), value.Size)
rects1.Add(rc)
temp.X = midpoint.X + value.Width
PictureBox1.Invalidate()
no_clicks += 1
nPoint(i) = New Point() {point.X, point.Y}
nSize(i) = New Size(value.Width, value.Height)
i += 1
I have a point variable which is a drawing point of a rectangle.I have declared another array of point as DIM npoint() as point so that i can assign the rectangles value to it.Problem is it is saying value of integer cannot be converted into system.srawing.point.
This:
nPoint(i) = New Point() {point.X, point.Y}
is obviously wrong. Judging by this:
nSize(i) = New Size(value.Width, value.Height)
which is the line that follows it, that first line should be this:
nPoint(i) = New Point(point.X, point.Y)
That said, Point and Size are both value types, so there's no point to creating new instances from parts of existing instances. Just use the existing instances and copies will be created:
nPoint(i) = point
nSize(i) = value
To simplify, this is what you are doing
Private Sub OPCode()
Dim npoint() As Point
Dim i As Integer
npoint(i) = New Point(3, 4)
End Sub
You array has no elements so you cannot assign value to an element.
To fix this just change to a list.
Private Sub OPCode()
Dim npoint As New List(Of Point)
npoint.Add(New Point(3, 4))
End Sub
I need to draw attention to the value variable in these two excerpts:
Dim rc As New Rectangle(New Point(point.X, point.Y), value.Size)
and
nSize(i) = New Size(value.Width, value.Height)
We don't get to know which Rectangle constructor overload you're shooting for, but the one where value.Size is a Size struct seems reasonable. We also don't get to see the declaration (type) for the value variable, but it seems unlikely (though not impossible) it both has a Size property and an implicit conversion to Size. In other words, one of the two lines is wrong.
This isn't the only error in this code. It's clearly part of a loop. Why don't you edit the question to show the whole loop, including setup. That might let us give you a much more complete answer.

Using the New List command with a Passed Parameter

I'm trying to Pass a Field Parameter from my form textbox to a Function to create a New List object from the Data Table parameter I'm passing.
In the following code, the first tmpReadTable shows with no syntax error, but when I try to use the Parm with the Datatable name I'm not sure what I'm missing syntax wise. I'm new to this, thanks in advance!
Updated code below:
Thank you for all the helpful replies...sorry I'm not more experienced, I'm coming from a Visual Foxpro background.
To summarize:
I want to pass in my IMPORT table parameters from my form.
The cImportTable is an empty SQL Table to use to import and validate each CSV file row.
I found this example in Murach's VB book but he leaves out how the LIST is being created from a PRODUCTS table in an earlier exercise. So I thought I could just substitute my passed cImportTable to do the same...that's where I'm stuck and maybe you all know of a better way.
Private Function ReadImportFile(ByVal cImportFile As String, ByVal cGroupID As String, ByVal cControlTable As String, ByVal cImportTable As String)
MessageBox.Show(cImportFile + " " + cGroupID + " " + cControlTable)
If Not File.Exists(cImportFile) Then
MessageBox.Show("File: " + cImportFile + " does not exist - cancelling process.")
Return False
End If
Dim curFileStream As New StreamReader(New FileStream(cImportFile, FileMode.Open, FileAccess.Read))
Dim curImportTable = "NewDataSet." + cImportTable
'Here I'm trying to create a LIST or DATASET using my Empty SQL Import Table and read in each row of the CSV file in the DO WHILE loop
'...I'm coming from Visual Foxpro background so am not sure what I'm missing or what is the standard procedure to do this simple task.
'This line gives me a syntax issue - and I'm not even sure what it's suppose to do, I'm taking it from Murach's VB book example,
'but he leaves out this vital piece of how to create this LIST from a Datatable - or if it's even the right method to use.
Dim tmpReadTable = New List(Of curImportTable)
Do While curFileStream.Peek <> -1
Dim row As String = curFileStream.ReadLine
Dim columns() As String = row.Split(",")
Dim ImportRecord As New curImportTable
ImportRecord.GroupId = columns(0)
ImportRecord.MemberId = columns(1)
Loop
'More Processing after Importing CSV file.....
curFileStream.Close()
'If lNoErrors
Return True
End Function
You are using a variable instead of TYPE on the code line #3 here
' This seems to be ok, no syntax error
Dim tmpReadTable = New List(Of NewDataSet.FO_ImportDataTable)
' The variable below implicitely will be of STRING type
Dim curImportTable = "NewDataSet." + cImportTable.ToString
' This line is not going to work
Dim tmpReadTable = New List(Of curImportTable)
' BUT THIS WILL
Dim x = New List(Of String)
Another issue is that Dim tmpReadTable happened twice in your code! can't re-declare variable. On top you declared it as NewDataSet.FO_ImportDataTable
Besides, I recommend declare all variables like Dim curImportTable as String, this way you can recognize types easier. Option Infer is good when you use anonymous types, LINQ, etc

NullReferenceException was unhandled in small loop

Hello I recieve a "nullreferenceexception was unhandled" error when I try to run this code:
For i As Integer = 1 To aantaltags
csvopc(i) = csvtagssplit(17 * i)
csvsql(i) = csvtagssplit(17 * i + 15)
Next
Background:
I read a csv file that I clean up and split into csvtagssplit()
csvopc and csvsql are both declared as string() at the top of the program.
anything dumb I did and I'm not noticing?
replicate it if you want to:
code:
http://pastebin.com/JDPa6FSB
csv:
http://pastebin.com/2e66i9EB
Your problem is in the intial part of your code where you declare the two variables cvsopc and cvssql,
As from your comment you write
Dim csvopc As String()
Dim csvsql As String()
But this only declares the two variables without any dimension.
So when you try to reach csvopc(i) you are effectively referencing a index that doesn't exist
Why use arrays when you don't know the exact size of your elements?.
You can easily switch to a List(Of String) where you can dinamically add elements
Dim csvopc As List(Of String) = new List(Of String)
Dim csvsql As List(Of String) = new List(Of String)
and then in your loop
For i As Integer = 0 To aantaltags - 1
csvopc.Add(csvtagssplit(17 * i))
csvsql.Add(csvtagssplit(17 * i + 15))
Next
A List(Of String) could also be referenced by Index as in
Dim aValue = csvopc(0)
You should step through your code with a debugger in order to inspect the data as the code runs. You could put a breakpoint at a place where everything should be initialized but before the exception happens and then see what the values are.
Which iteration of the loop fails, and what line specifically fails? Put a breakpoint on that line and see what all the values are immediately before the line executes. If this debugging doesn't reveal the error to you, update your post with the data you find during debugging and maybe we can get somewhere.

How to assign a value to an array from a combobox

The code I have is:
Dim Dbase() As String = Nothing
Dbase(0) = Db_ComboBox.Text
I have declared Dbase as array and assigned Nothing, Db_ComboBox is a combobox.
For that assignment statement, I'm getting the following error: "Reference 'Dbase' has a value of 'Nothing'"
What is the reason for this error, and how can I take the value from the combobox and save it in the array?
You need to change this:
Dim Dbase() As String = Nothing
to this (declare an array of 1 element):
Dim Dbase(0) As String
And then this line will work:
Dbase(0) = Db_ComboBox.Text
If you need to change your array size you can use Redim or Redim preserve, as required.
If you anticipate contents of Dbase to change often, I am all with #Joel's suggestion about switching to List(Of String) instead of handling array sizes manually.
Let's look at your code:
Dim Dbase() As String = Nothing
Dbase(0) = Db_ComboBox.Text
Especially the first line. That first line creates a variable that can refer to an array, but the = Nothing portion explicitly tells it, "Do not create a real array here yet". You have, effectively, a pointer that doesn't point to anything.
I get here that what you really need is a List collection that you can append to over time:
Dim Dbase As New List(Of String)()
Dbase.Add(Db_ComboBox.Text)
Dbase() IS NOTHING. Look at this example:
cargoWeights = New Double(10) {}
atmospherePressures = New Short(2, 2, 4, 10) {}
inquiriesByYearMonthDay = New Byte(20)()() {}
That's how you declare arrays.
More examples: http://msdn.microsoft.com/en-us/library/vstudio/wak0wfyt.aspx

Generic Lists copying references rather than creating a copiedList

I was developing a small function when trying to run an enumerator across a list and then carry out some action. (Below is an idea of what I was trying to do.
When trying to remove I got a "Collection cannot be modified" which after I had actually woken up I realised that tempList must have just been assigned myLists reference rather than a copy of myLists. After that I tried to find a way to say
tempList = myList.copy
However nothing seems to exist?? I ended up writing a small for loop that then just added each item from myLsit into tempList but I would have thought there would have been another mechanism (like clone??)
So my question(s):
is my assumption about tempList receiving a reference to myList correct
How should a list be copied to another list?
private myList as List (Of something)
sub new()
myList.add(new Something)
end sub
sub myCalledFunction()
dim tempList as new List (Of Something)
tempList = myList
Using i as IEnumerator = myList.getEnumarator
while i.moveNext
'if some critria is met then
tempList.remove(i.current)
end
end using
end sub
By writing tempList = myList you don't make a copy oh the collection, you only make tempList reference myList. Try this instead : dim tempList as new List (Of Something)(myList)
I think if you called myCalledFunction(byVal aListCopy as Something) you can let the framework do the work.
If your list consists of value types you can just create a new list with the old list passed in the constructor. If you are going to be doing a deep copy of a reference object your best bet is to have your reference type implement ICloneable (example). You can then loop through and clone each object or you could add an extension method (like this c# example).
Try this - use LINQ to create a new list from the original, for example:
Sub Main()
Dim nums As New List(Of Integer)
nums.Add(1)
nums.Add(2)
nums.Add(3)
nums.Add(4)
Dim k = (From i In nums _
Select i).ToList()
For Each number As Integer In nums
k.Remove(number)
Next
End Sub
k will then be a new list of numbers which are not linked to the source.