|
-
Jul 20th, 2011, 11:01 PM
#1
Thread Starter
Member
[RESOLVED] ArrayList returns boolean instead of PictureBox object
VB Code:
Dim pic2 As PictureBox
pic2 = snake.Item(0)
If pic2.Location.Y = picFood.Location.Y Then
'move food to new location
RandomFoodLocation()
'add length to snake
snake.Add(New PictureBox().Location = New Point(pic2.Location.X, pic2.Location.Y + (growth + 17)))
growth += 17
End If
The pic2 = snake.item(0) is causing problems. The compiler says that snake.item(0) is returning a boolean value of false. But it the item(0) should be a picturebox.
I thought I forgot to add the first ball to the arraylist, but I checked and it gets added on form_load sub procedure.
VB Code:
Private Sub frmSnakeGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
snake.Add(picBall)
RandomFoodLocation()
End Sub
Code:
InvalidCastException was unhandled
Unable to cast object of type "System.Boolean' to type 'System.Windows.Forms.PictureBox'.
-
Jul 20th, 2011, 11:31 PM
#2
Re: ArrayList returns boolean instead of PictureBox object
I would advise using a List(Of PictureBox) instead of an ArrayList, example below uses a image setup as a project resource where the original image is in the app folder to make things simple here which goes with ImageLocation
Code:
Public Class Form1
Private MyPictureBoxes As New List(Of PictureBox)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim P1 As New PictureBox With _
{ _
.Top = 10, _
.Left = 0, _
.Image = My.Resources.wanted, _
.Width = 400, _
.Height = 300, _
.Name = "P1", _
.ImageLocation = IO.Path.Combine(Application.StartupPath, "wanted.jpg") _
}
MyPictureBoxes.Add(P1)
Me.Controls.Add(MyPictureBoxes(0))
Dim MyPictBox = MyPictureBoxes(MyPictureBoxes.IndexOf(P1))
Console.WriteLine("{0} {1} - {2}x{3}", _
MyPictBox.Name, _
MyPictBox.ImageLocation, _
MyPictBox.Location.Y, _
MyPictBox.Location.X)
End Sub
End Class
-
Jul 21st, 2011, 12:53 AM
#3
Thread Starter
Member
Re: ArrayList returns boolean instead of PictureBox object
Thanks for the replying and your suggesting on using a List instead of ArrayList. I keep forgetting about List(Of).
Also, I was looking at my code again and seen another problem.
Here is the corrected code:
VB Code:
snake.Add(New PictureBox())
snake.Item(snake.Count - 1).Location = New Point(pic2.Location.X, pic2.Location.Y + (growth + 17))
I was only adding the New PictureBox().Location = New Point..... stuff and not the actual PictureBox. I didn't see my errors until I changed from ArrayList to List(Of) and received the a conversion error. So I looked at the code and figured out that Visual Basic doesn't interpret code the way that I understand it. I wanted Visual Basic to add a a new PictureBox and set the location property with the appropriate settings.
I also noticed I haven't set the new PictureBox that the program creates to any particular image yet.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|