accessing array

atkin

New member
Joined
Mar 12, 2007
Messages
2
Programming Experience
Beginner
Im currently building my first VB.net program. i have an array created in one form using the following code:

VB.NET:
[SIZE=2][COLOR=#0000ff]
Public[/COLOR][/SIZE][SIZE=2] GraphicsItems [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ArrayList()
[/SIZE]

On another form i want to be able to clear the array when a button is pressed, i researched this and found that arrayname.clear() is what i need to use, so i thought the following would be what i want: (Note this is on another form to the code above)

VB.NET:
[SIZE=2][COLOR=#0000ff]
Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ClearCanvasToolStripMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ClearCanvasToolStripMenuItem.Click
GraphicsItems.Clear()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub
[/COLOR][/SIZE]

It says GraphicsItem is not declared, i thought that because i had declared it as public on the first form it would be accessible, i guess im confused about the scope. i also tried formname.graphicsitems.clear() but that didnt work either. i have tried googling around but im not sure what im looking for so any help would be appriciated.

Thanks.
 

Cabose

Active member
Joined
Oct 18, 2006
Messages
39
Programming Experience
Beginner
Well all you have to do is this.

VB.NET:
Private Sub ClearCavasToolStripMenuItem_Click(ByVal sender As system.Object, ByVal e As System.EventArgs) Handles ClearCanvasToolStripMenuItem.Click
'Form1 is an example form of the other form.
Form1.GraphicsItems.Clear()
End Sub

hope this helps :D

~Cabose
 

atkin

New member
Joined
Mar 12, 2007
Messages
2
Programming Experience
Beginner
As i said, i have already tried that, it returns this error

VB.NET:
Error 1 Reference to a non-shared member requires an object reference.

thanks for the help ;)
 

juggernot

Well-known member
Joined
Sep 28, 2006
Messages
173
Programming Experience
Beginner
This is what I usually do. Add a module, and declare your array there. Use 'Public' again. You should then have no problems accessing it throughout your project. Hope that helps.
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
As i said, i have already tried that, it returns this error

VB.NET:
Error 1 Reference to a non-shared member requires an object reference.

thanks for the help ;)

It returns that error because Form1 is the name of a class, not the name of an instance.

Tell me, does form1 create form2?

If so, then it needs to pass itself as a reference at create time:

VB.NET:
'FORM 2's CONSTRUCTOR NEEDS TO BE
 
  Private myOwnerForm1 as Form1
 
  Public Sub New(myOwner as Form1)
    myOwnerForm1 = myOwner
  End Sub


when form1 creates form 2:

VB.NET:
  Dim f2 as New Form2(Me) 'I, form1, am the owner of form2

now on form2 you can say:

myOwnerForm1.GraphicsItems.Clear()



-

however, quick bit of info about OO programming for you:

Generally, one object shouldnt mess with another form's attributes. An analogy could be.. suppose you had all your DVDs ordered by alphabetical title and you really liked that way. Now suppose your neighbour came over and just rearranged them because he prefers alphabetical by last name of lead actor. He just came and messed with your attributes. You might get upset at this.

In the same way, FOrm1 might want to know if its graphicsitems has been cleared, sho you shouldnt put it on public show for everyone to come and stomp all over and mess it up.. It might even crash your program ,if Form1 thought there were 10 items in there and went to pull out the ninth, but meanwhile form2 had come and cleared it..

So you should make a method (sub) to clear it. Form2 is allowed to call the method, but it is form1's own decision whether it should clear its items or not

VB.NET:
  Public Sub ClearYourItems()
    If itsNotSuitableToClearThemRightNow Then
      Return
    else
      Me.GraphicsItems.Clear()
    EndIf
  End Sub


Dont allow direct access to your objects data. Use properties to retrist access and use methods to limit what other objects can do to your object.

Picture all your classes (objects) as black boxes with buttons on.. Other objects should only be able to come and press the buttons, not just reach into the black box and mess with the wires. How the black box does its magic, we dont care. Form2 shouldnt care what form1 has to do to clear the graphics, it should only care that it does so when asked. It should not clear it for it.

Hope this helps your understanding of OO programming. I recommend getting a good book on OO, not necessarily in VB.NET (I.e. youre looking to learn OO concepts, via VBN as a medium, not looking to learn VBN and maybe guess some OO along the way)
 

atkin

New member
Joined
Mar 12, 2007
Messages
2
Programming Experience
Beginner
Thanks for that, i understand the analergy but can not see how to implement your example into my code.

Here is my form1

VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] Form1[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PaintCanvas1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] paletteColor_LeftClick([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] paletteColor.LeftClick[/SIZE]
[SIZE=2]Canvas.GraphicColor = paletteColor.LeftColor[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ToToolStripMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CircleToolStripMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] CircleToolStripMenuItem.Click[/SIZE]
[SIZE=2][COLOR=#008000]'Set the tool [/COLOR][/SIZE]
[SIZE=2]Canvas.GraphicTool = PaintCanvas.GraphicTools.CirclePen[/SIZE]
[SIZE=2][COLOR=#008000]'Uncheck the Hollow Circle menu item [/COLOR][/SIZE]
[SIZE=2]HollowCircleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]HollowRectangleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]FilledRectangleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]LineToolToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]PenToolToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] HollowCircleToolStripMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] HollowCircleToolStripMenuItem.Click[/SIZE]
[SIZE=2][COLOR=#008000]'Set the tool [/COLOR][/SIZE]
[SIZE=2]Canvas.GraphicTool = PaintCanvas.GraphicTools.HollowCirclePen[/SIZE]
[SIZE=2][COLOR=#008000]'Uncheck the Circle menu item [/COLOR][/SIZE]
[SIZE=2]CircleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]HollowRectangleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]FilledRectangleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]LineToolToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]PenToolToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ExitToolStripMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs)[/SIZE]
[SIZE=2][COLOR=#008000]'Close the application [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Close()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ToolsToolStripMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ToolsToolStripMenuItem.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] FilledRectangleToolStripMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] FilledRectangleToolStripMenuItem.Click[/SIZE]
[SIZE=2][COLOR=#008000]'Set the tool [/COLOR][/SIZE]
[SIZE=2]Canvas.GraphicTool = PaintCanvas.GraphicTools.RectanglePen[/SIZE]
[SIZE=2][COLOR=#008000]'Uncheck the Filled Rectangle menu item [/COLOR][/SIZE]
[SIZE=2]HollowCircleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]HollowRectangleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]CircleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]LineToolToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]PenToolToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] HollowRectangleToolStripMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] HollowRectangleToolStripMenuItem.Click[/SIZE]
[SIZE=2][COLOR=#008000]'Set the tool [/COLOR][/SIZE]
[SIZE=2]Canvas.GraphicTool = PaintCanvas.GraphicTools.HollowRectanglePen[/SIZE]
[SIZE=2][COLOR=#008000]'Uncheck the Filled Rectangle menu item [/COLOR][/SIZE]
[SIZE=2]HollowCircleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]FilledRectangleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]CircleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]LineToolToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]PenToolToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] LineToolToolStripMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] LineToolToolStripMenuItem.Click[/SIZE]
[SIZE=2]HollowCircleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]FilledRectangleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]CircleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]HollowRectangleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]PenToolToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PenToolToolStripMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] PenToolToolStripMenuItem.Click[/SIZE]
[SIZE=2]HollowCircleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]FilledRectangleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]CircleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]HollowRectangleToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2]LineToolToolStripMenuItem.Checked = [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] ClearCavasToolStripMenuItem_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] ClearCanvasToolStripMenuItem.Click[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
When ClearCanvasToolStripMenuItem.Click is ran i wanted to clear my canvas, which i understand is done by clearing the array that the graphics items are stored in, the array is decalred in the following form:
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] PaintCanvas[/SIZE]
[SIZE=2][COLOR=#008000]'Public enumerations [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Enum[/COLOR][/SIZE][SIZE=2] GraphicTools [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2]CirclePen = 0[/SIZE]
[SIZE=2]HollowCirclePen = 1[/SIZE]
[SIZE=2]RectanglePen = 2[/SIZE]
[SIZE=2]HollowRectanglePen = 3[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Enum[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Enum[/COLOR][/SIZE][SIZE=2] GraphicSizes [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE]
[SIZE=2]Small = 4[/SIZE]
[SIZE=2]Medium = 80[/SIZE]
[SIZE=2]Large = 20[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Enum[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Public members [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] GraphicsItems [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ArrayList()[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] GraphicTool [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] GraphicTools = GraphicTools.CirclePen[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] GraphicSize [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] GraphicSizes = GraphicSizes.Medium[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] GraphicColor [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Color[/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] DoMousePaint([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MouseEventArgs)[/SIZE]
[SIZE=2][COLOR=#008000]'Store the new item somewhere [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objGraphicsItem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] GraphicsItem[/SIZE]
[SIZE=2][COLOR=#008000]'What tool are you using? [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] GraphicTool[/SIZE]
[SIZE=2][COLOR=#008000]'CirclePen [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] GraphicTools.CirclePen[/SIZE]
[SIZE=2][COLOR=#008000]'Create a new graphics circle [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objGraphicsCircle [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] GraphicsCircle()[/SIZE]
[SIZE=2][COLOR=#008000]'Set the point for drawing [/COLOR][/SIZE]
[SIZE=2]objGraphicsCircle.SetPoint(e.X, e.Y, GraphicSize, GraphicColor, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#008000]'Store this for addition [/COLOR][/SIZE]
[SIZE=2]objGraphicsItem = objGraphicsCircle[/SIZE]
[SIZE=2][COLOR=#008000]'HollowCirclePen [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] GraphicTools.HollowCirclePen[/SIZE]
[SIZE=2][COLOR=#008000]'Create a new graphics circle [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objGraphicsCircle [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] GraphicsCircle()[/SIZE]
[SIZE=2][COLOR=#008000]'Set the point for drawing [/COLOR][/SIZE]
[SIZE=2]objGraphicsCircle.SetPoint(e.X, e.Y, GraphicSize, GraphicColor, [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#008000]'Store this for addition [/COLOR][/SIZE]
[SIZE=2]objGraphicsItem = objGraphicsCircle[/SIZE]
[SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] GraphicTools.RectanglePen[/SIZE]
[SIZE=2][COLOR=#008000]'Create a new graphics circle [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objGraphicsCircle [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] GraphicsRectangle()[/SIZE]
[SIZE=2][COLOR=#008000]'Set the point for drawing [/COLOR][/SIZE]
[SIZE=2]objGraphicsCircle.SetPoint(e.X, e.Y, GraphicSize, GraphicColor, [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#008000]'Store this for addition [/COLOR][/SIZE]
[SIZE=2]objGraphicsItem = objGraphicsCircle[/SIZE]
[SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] GraphicTools.HollowRectanglePen[/SIZE]
[SIZE=2][COLOR=#008000]'Create a new graphics circle [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objGraphicsCircle [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] GraphicsRectangle()[/SIZE]
[SIZE=2][COLOR=#008000]'Set the point for drawing [/COLOR][/SIZE]
[SIZE=2]objGraphicsCircle.SetPoint(e.X, e.Y, GraphicSize, GraphicColor, [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#008000]'Store this for addition [/COLOR][/SIZE]
[SIZE=2]objGraphicsItem = objGraphicsCircle[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Were you given an item?[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] objGraphicsItem [/SIZE][SIZE=2][COLOR=#0000ff]IsNot[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Add it to the list [/COLOR][/SIZE]
[SIZE=2]GraphicsItems.Add(objGraphicsItem)[/SIZE]
[SIZE=2][COLOR=#008000]'Invalidate the Control [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Invalidate(objGraphicsItem.Rectangle)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PaintCanvas_MouseDown([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.MouseEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].MouseDown[/SIZE]
[SIZE=2][COLOR=#008000]'Is the left mouse button down?[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] e.Button = Windows.Forms.MouseButtons.Left [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]DoMousePaint(e)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PaintCanvas_MouseMove([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.MouseEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].MouseMove[/SIZE]
[SIZE=2][COLOR=#008000]'Is the left mouse button down?[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] e.Button = Windows.Forms.MouseButtons.Left [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]DoMousePaint(e)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PaintCanvas_Paint([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.PaintEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Paint[/SIZE]
[SIZE=2][COLOR=#008000]'Go through the list [/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] objGraphicsItem [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] GraphicsItem [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] GraphicsItems[/SIZE]
[SIZE=2][COLOR=#008000]'Do we need to be drawn?[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] e.ClipRectangle.IntersectsWith(objGraphicsItem.Rectangle) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Ask each item to draw itself [/COLOR][/SIZE]
[SIZE=2]objGraphicsItem.Draw(e.Graphics)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
 

Anti-Rich

Well-known member
Joined
Jul 1, 2006
Messages
325
Location
Perth, Australia
Programming Experience
1-3
rather than using a module and making things static, is it good practice to make some things shared?

thats what ive been doing... and it works for me so far, but i dont know if thats the 'done way'.


cheers
adam
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
rather than using a module and making things static, is it good practice to make some things shared?

vb's shared = rest-of-world's static

some things it makes sense to share.. usually thread safe single instances of something, read only values, constants, simple processes unrelated to any particular object (like SendErrorEmail ).
Making something shared just because one cannot work out how to pass it from one object instance to another, is not recommended! :D
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
Thanks for that, i understand the analergy but can not see how to implement your example into my code.

Thanks for using a CODE box.. the nuisance is, if you post in WYSIWYG mode, then the indentation is lost and the syntax highlighting messes up. Its a forum fault.

Please re-post your code in BASIC mode:
Press edit
Delete the code
Press this icon:
switchmode.gif

Note that i mean this icon:
switchmode.gif
NOT THIS ICON:
removeformat.gif

The text window will go from light blue to white after 3 seconds
NOW paste your code between
VB.NET:
 tags...
 
Top Bottom