Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

12. OOA&D > Typeof...Is

Typeof...Is

We’ve seen that you can cast from a type to one of its ancestors or an interface it implements by using the CType() function (or one of the dedicated conversion functions):

CType(oldType, NewType)

We’ve also seen that the Object.GetType() method (inherited by every type in the .NET Framework) returns an instance of the Type class, so you can do something like this to check whether two objects are of the same type:

If MyStandardRecipe.GetType() = MyRecipe.GetType()

This test certainly has its place, but it compares the types of two objects, and it’s an absolute test. What if you wanted to test whether an object is a certain type but don’t have an instance of that type lying around? You could use the Name property of the Type object returned by GetType():


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint