none
关于类型转换的一点疑惑 RRS feed

  • 问题

  • Public Class Form1
       Private points As New ArrayList()
       Private pen As New Pen(Color.Blue, 2)
       Private brush As New SolidBrush(Color.Blue)

       Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
          points.Add(New Point(e.X, e.Y))
          Panel1.Invalidate()
       End Sub

       Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
          If points.Count > 1 Then
             Dim pointarray As Point() = CType(points.ToArray(points(0).GetType), Point())
             e.Graphics.DrawPolygon(pen, pointarray)
          End If
       End Sub
    End Class
    上面这段代码让我不明白的地方是倒数第五行,既然ArrayList.ToArray方法返回的已经是指定类型的数组,为什么还要用CType函数来转换一下类型,这不是多此一举吗?


    xrhigh
    2009年4月3日 5:17

答案

全部回复