Results 1 to 3 of 3

Thread: [RESOLVED] [02/03] Checking type

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    Belgium
    Posts
    68

    Resolved [RESOLVED] [02/03] Checking type

    I've got a mustinherit class with two subclasses.
    Lets say the class is "Shape" and the subclasses are "Circle" and "Triangle"

    Now I've got an array of shapes containing both circles and triangles.
    How can I check which type the item is?

    I noticed that this doesn't work:

    VB Code:
    1. if array(i).getType.equals(Shape) then
    2. end if

    But this does:
    VB Code:
    1. dim s as Shape
    2. ...
    3. if array(i).getType.equals(s.getType) then
    4. end if

    It seems stupid to me that I should create a variable of the type, so isn't there a better way?

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: [02/03] Checking type

    You can use "TypeOf", an example is below:
    VB Code:
    1. Dim MyArray As New ArrayList
    2.         MyArray.Add("This")
    3.         MyArray.Add(3)
    4.         For Each obj As Object In MyArray
    5.             If TypeOf obj Is String Then
    6.                 MessageBox.Show("Its a string type!")
    7.             End If
    8.             If TypeOf obj Is Integer Then
    9.                 MessageBox.Show("Its an integer!")
    10.             End If
    11.         Next

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    Belgium
    Posts
    68

    Re: [02/03] Checking type

    Ah yes, that was the thing I was looking for.

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width