Results 1 to 2 of 2

Thread: [RESOLVED] [2005] Using Object.GetType on an interface

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Resolved [RESOLVED] [2005] Using Object.GetType on an interface

    I've built a structure to handle writing of data to a file from any class or structure that implements my IObject interface (I'll come up with a better name later).

    When I try to use GetType with it, as shown below, the syntax highlighter highlights GetType (line 5) and tells me it's not a member of IObject.

    I'm using an interface instead of a MustInherit class because I have several structures which also implement IObject and several classes that inherit existing classes (such as Collection(of t)). Is there any way to get the type name from an interface?

    I thought of using DirectCast to cast it to Object. Will that work if fromObject is a structure instead of a class?

    vb Code:
    1. Public Sub New(ByVal fromObject As IObject)
    2.     Me.Object = fromObject
    3.     Me.Data = fromObject.GetData
    4.     Me.ObjectType = ObjectTypes.Other
    5.     Me.OtherTypeName = fromObject.GetType.FullName
    6. End Sub

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: [2005] Using Object.GetType on an interface

    Changed line 5 to use direct cast. Tested using a small test project in another instance of vstudio. Sorry to waste yall's time.

    vb Code:
    1. Public Sub New(ByVal fromObject As IObject)
    2.     Me.Object = fromObject
    3.     Me.Data = fromObject.GetData
    4.     Me.ObjectType = ObjectTypes.Other
    5.     Me.OtherTypeName = DirectCast(fromObject, Object).GetType.FullName
    6. End Sub

    Future reference: structures can be casted as objects.

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