-2

How do I make the do while loop in my code work with z being a variable?

Function make(ByVal z As Object)
    z.Location = zloc
    z.Hide()
    zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    If zloc.Y > 595 Then
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
    End If
    z.location = zloc
    Do While z.bounds.intersectswith(PictureBox1.Bounds, PictureBox2.Bounds, PictureBox3.Bounds)
        zloc = New Point(RandomNumber(playspace.Width), RandomNumber(playspace.Height))
        z.location = zloc
    Loop
    z.Location = zloc
    z.Show()
    Return (z)
End Function

The problem is I cant use the ".bounds.intersectswith" on a variable.

Ashley
  • 1
  • 1

1 Answers1

0

Your problem really comes from the fact that z is declared as Object. Object by itself does not have any properties (well, only the really basic ones). It certainly does not have a bounds property or method, so you can't use that.

What you need to do is declare z as its actual type (whatever that is in your code) and you will be able to access its properties. Otherwise, if you really need to use Object as argument in your method, you should cast z to the correct type in the method, using CType(z, TypeOfZ).

From your code, I assume that z is a bounding box of some sort? Anyway, you should declare it as what it is in the method, like

Function make(ByVal z As BoundingBox)
    ...
    If z.Bounds.IntersetcsWith(...) then
        ...
    End If
    ...
End Function
yu_ominae
  • 2,975
  • 6
  • 39
  • 76