473,421 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

Rediming an array causes an exception on End Sub

Hi, please take a look at the following code. (I've ommitted the Windows
generated code)
--------------------------------
Imports System.Drawing.Drawing2D
Public Class Form1
Inherits System.Windows.Forms.Form
Dim points() As Point = {}
Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255))
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
If points.Length > 0 Then
e.Graphics.DrawCurve(pen, points)
End If
End Sub

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Right Then
' clear points
ReDim points(-1)
Me.Refresh()
End If
If e.Button = MouseButtons.Left Then
ReDim Preserve points(points.Length)
Dim p As New Point(e.X, e.Y)
points(points.Length - 1) = p
Me.Refresh()
End If
End Sub
End Class
---------------------
On mousedown, the array 'points' gets lengthened by 1 and the curve is
redrawn. But, on exiting from the sub, it gives an InvalidArgumentException.
But if I dont Redim the array, the code runs fine.

Whats going on???

Thanks in advance.
Nov 20 '05 #1
5 1249
It's probably because the DrawCurve method takes a Point array that requires
at least 4 points. You are starting with an empty array and only adding one
point with each mouse click. You then try to draw the curve if the array
contains at least one point. You should change the OnPaint code to the
following:

If points.length > 3 Then
e.Graphics.DrawCurve(pen, points)
End If

"Ashish" <as*****@hotmail.com> wrote in message
news:c1*************@ID-75446.news.uni-berlin.de...
Hi, please take a look at the following code. (I've ommitted the Windows
generated code)
--------------------------------
Imports System.Drawing.Drawing2D
Public Class Form1
Inherits System.Windows.Forms.Form
Dim points() As Point = {}
Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255))
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
If points.Length > 0 Then
e.Graphics.DrawCurve(pen, points)
End If
End Sub

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Right Then
' clear points
ReDim points(-1)
Me.Refresh()
End If
If e.Button = MouseButtons.Left Then
ReDim Preserve points(points.Length)
Dim p As New Point(e.X, e.Y)
points(points.Length - 1) = p
Me.Refresh()
End If
End Sub
End Class
---------------------
On mousedown, the array 'points' gets lengthened by 1 and the curve is
redrawn. But, on exiting from the sub, it gives an InvalidArgumentException. But if I dont Redim the array, the code runs fine.

Whats going on???

Thanks in advance.

Nov 20 '05 #2

I dont think thats the problem. I had the Me.Refresh() and DrawCurve()
methods commented. So, it never drew a graph. But still gave the exception.
On commenting the Redim statement, I no longer got any exceptions.

-Ashish

"Scott" <sc*****@yahoo.com> wrote in message
news:eX**************@tk2msftngp13.phx.gbl...
It's probably because the DrawCurve method takes a Point array that requires at least 4 points. You are starting with an empty array and only adding one point with each mouse click. You then try to draw the curve if the array
contains at least one point. You should change the OnPaint code to the
following:

If points.length > 3 Then
e.Graphics.DrawCurve(pen, points)
End If

"Ashish" <as*****@hotmail.com> wrote in message
news:c1*************@ID-75446.news.uni-berlin.de...
Hi, please take a look at the following code. (I've ommitted the Windows
generated code)
--------------------------------
Imports System.Drawing.Drawing2D
Public Class Form1
Inherits System.Windows.Forms.Form
Dim points() As Point = {}
Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255))
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
If points.Length > 0 Then
e.Graphics.DrawCurve(pen, points)
End If
End Sub

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Right Then
' clear points
ReDim points(-1)
Me.Refresh()
End If
If e.Button = MouseButtons.Left Then
ReDim Preserve points(points.Length)
Dim p As New Point(e.X, e.Y)
points(points.Length - 1) = p
Me.Refresh()
End If
End Sub
End Class
---------------------
On mousedown, the array 'points' gets lengthened by 1 and the curve is
redrawn. But, on exiting from the sub, it gives an

InvalidArgumentException.
But if I dont Redim the array, the code runs fine.

Whats going on???

Thanks in advance.


Nov 20 '05 #3

"Scott" <sc*****@yahoo.com> wrote in message
news:eX**************@tk2msftngp13.phx.gbl...
It's probably because the DrawCurve method takes a Point array that requires at least 4 points. You are starting with an empty array and only adding one point with each mouse click. You then try to draw the curve if the array
contains at least one point. You should change the OnPaint code to the
following:
By the way, the DrawCurve method can take less than 4 points and works
perfectly well.

-Ashish

If points.length > 3 Then
e.Graphics.DrawCurve(pen, points)
End If

"Ashish" <as*****@hotmail.com> wrote in message
news:c1*************@ID-75446.news.uni-berlin.de...
Hi, please take a look at the following code. (I've ommitted the Windows
generated code)
--------------------------------
Imports System.Drawing.Drawing2D
Public Class Form1
Inherits System.Windows.Forms.Form
Dim points() As Point = {}
Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255))
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
If points.Length > 0 Then
e.Graphics.DrawCurve(pen, points)
End If
End Sub

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Right Then
' clear points
ReDim points(-1)
Me.Refresh()
End If
If e.Button = MouseButtons.Left Then
ReDim Preserve points(points.Length)
Dim p As New Point(e.X, e.Y)
points(points.Length - 1) = p
Me.Refresh()
End If
End Sub
End Class
---------------------
On mousedown, the array 'points' gets lengthened by 1 and the curve is
redrawn. But, on exiting from the sub, it gives an

InvalidArgumentException.
But if I dont Redim the array, the code runs fine.

Whats going on???

Thanks in advance.


Nov 20 '05 #4
Hi Ashish,

Indeed it does work with less that four points, but it really needs at least
two (what does a spline with only one point look like?). I have taken your
code and changed the "If points.Length > 0 Then" line to read "If
points.Length > 1 Then" and it works without error.

Please give that a try and let me know if it works for you.

HTH,
Derrick
"Ashish" <as*****@hotmail.com> wrote in message
news:c1*************@ID-75446.news.uni-berlin.de...

"Scott" <sc*****@yahoo.com> wrote in message
news:eX**************@tk2msftngp13.phx.gbl...
It's probably because the DrawCurve method takes a Point array that

requires
at least 4 points. You are starting with an empty array and only adding

one
point with each mouse click. You then try to draw the curve if the array
contains at least one point. You should change the OnPaint code to the
following:


By the way, the DrawCurve method can take less than 4 points and works
perfectly well.

-Ashish

If points.length > 3 Then
e.Graphics.DrawCurve(pen, points)
End If

"Ashish" <as*****@hotmail.com> wrote in message
news:c1*************@ID-75446.news.uni-berlin.de...
Hi, please take a look at the following code. (I've ommitted the Windows generated code)
--------------------------------
Imports System.Drawing.Drawing2D
Public Class Form1
Inherits System.Windows.Forms.Form
Dim points() As Point = {}
Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255))
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
If points.Length > 0 Then
e.Graphics.DrawCurve(pen, points)
End If
End Sub

Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)
If e.Button = MouseButtons.Right Then
' clear points
ReDim points(-1)
Me.Refresh()
End If
If e.Button = MouseButtons.Left Then
ReDim Preserve points(points.Length)
Dim p As New Point(e.X, e.Y)
points(points.Length - 1) = p
Me.Refresh()
End If
End Sub
End Class
---------------------
On mousedown, the array 'points' gets lengthened by 1 and the curve is
redrawn. But, on exiting from the sub, it gives an

InvalidArgumentException.
But if I dont Redim the array, the code runs fine.

Whats going on???

Thanks in advance.



Nov 20 '05 #5
Works like a charm! Thanks Derrick
-Ashish
"Derrick" <Derrick[underscore]Repep[at]toxicall[dot]com> wrote in message
news:e2**************@TK2MSFTNGP11.phx.gbl...
Hi Ashish,

Indeed it does work with less that four points, but it really needs at least two (what does a spline with only one point look like?). I have taken your code and changed the "If points.Length > 0 Then" line to read "If
points.Length > 1 Then" and it works without error.

Please give that a try and let me know if it works for you.

HTH,
Derrick
"Ashish" <as*****@hotmail.com> wrote in message
news:c1*************@ID-75446.news.uni-berlin.de...

"Scott" <sc*****@yahoo.com> wrote in message
news:eX**************@tk2msftngp13.phx.gbl...
It's probably because the DrawCurve method takes a Point array that

requires
at least 4 points. You are starting with an empty array and only adding
one
point with each mouse click. You then try to draw the curve if the
array contains at least one point. You should change the OnPaint code to the following:


By the way, the DrawCurve method can take less than 4 points and works
perfectly well.

-Ashish

If points.length > 3 Then
e.Graphics.DrawCurve(pen, points)
End If

"Ashish" <as*****@hotmail.com> wrote in message
news:c1*************@ID-75446.news.uni-berlin.de...
> Hi, please take a look at the following code. (I've ommitted the

Windows > generated code)
> --------------------------------
> Imports System.Drawing.Drawing2D
> Public Class Form1
> Inherits System.Windows.Forms.Form
> Dim points() As Point = {}
> Dim pen As New Pen(Color.FromArgb(255, 0, 0, 255))
> Protected Overrides Sub OnPaint(ByVal e As
> System.Windows.Forms.PaintEventArgs)
> If points.Length > 0 Then
> e.Graphics.DrawCurve(pen, points)
> End If
> End Sub
>
> Protected Overrides Sub OnMouseDown(ByVal e As
> System.Windows.Forms.MouseEventArgs)
> If e.Button = MouseButtons.Right Then
> ' clear points
> ReDim points(-1)
> Me.Refresh()
> End If
> If e.Button = MouseButtons.Left Then
> ReDim Preserve points(points.Length)
> Dim p As New Point(e.X, e.Y)
> points(points.Length - 1) = p
> Me.Refresh()
> End If
> End Sub
> End Class
> ---------------------
> On mousedown, the array 'points' gets lengthened by 1 and the curve is > redrawn. But, on exiting from the sub, it gives an
InvalidArgumentException.
> But if I dont Redim the array, the code runs fine.
>
> Whats going on???
>
> Thanks in advance.
>
>



Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Peter Rooney | last post by:
Hi, I have 24 checkboxes all with the same naming convention, each box has a value ranging from 1 to 23, what I need to do is fill in the gaps with a 0 value where the user doesn't select a...
5
by: Fabio Papa | last post by:
Hi, I am fairly new to programming and and even newer to dotnet. I appoligise in advance if this is a dumb questions, and I would appreciate if you could answer it anyways. :) I am writing a...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
1
by: elziko | last post by:
My intention is to store an array of singles inside a DataTable so that it can me peristed somehow, maybe XML file, maybe Access/SQL Server I don't know yet so I'm just saving it as an XML file for...
10
by: Antoine | last post by:
I can't work out what is causing this problem. Can anyone suggest what the typical causes beyond the obvious might be? Could you get it with datasets? Maybe I should run in debug mode and test...
3
by: doubts | last post by:
Hi all, I am trying to convert my bulk of code from VC++ 6.0 to VC++.Net. when using std::string type variable, the application causes exception at one instance and does not cause an exception at...
5
by: Mike | last post by:
Hello All, Please, if anyone can point me to the problem, I'd sure appreciate it! I am very new to VB programming and not a programmer to begin with. This is part of a Visual Basic 2005 Express...
1
by: aeshiels | last post by:
Hopefully I have the correct newsgroup for this question. I've developed a very simple COM server in C++ as im trying to learn COM. The COM server has an array of IUnknown interfaces (MyArray)...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.