Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2007

Tip: Looking for answers? Try searching our database.

GetClassName from an Interface

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Simon Woods - 22 Oct 2007 16:54 GMT
Hi

Still trying to get to grips with VB.Net so sorry if these seem silly
questions ...

I have an object which is implementing a secondary interface. I want to
get the name of the underlying object.

My Interface is:

Public Interface ICommand
    Sub Execute
End Interface

ATM, I'm doing

name = DirectCast(myInterfacedObject, Object).GetType.Name

Are there better ways of doing this? Examples I've seen in C# seem to
suggest that you can call the GetType off the Interface itself.

Obviously I could put the Name property onto the interface, but I'm
wondering if there are some other ways I could do this?

Also, what is going on when DirectCast is called? Is the object being
copied to somewhere else in memory or is a different address in memory
being used to retrieve the Type info?

Thanks

Simon
Armin Zingler - 22 Oct 2007 17:31 GMT
> Hi
>
[quoted text clipped - 3 lines]
> I have an object which is implementing a secondary interface. I want
> to get the name of the underlying object.

> My Interface is:
>
[quoted text clipped - 11 lines]
> Obviously I could put the Name property onto the interface, but I'm
> wondering if there are some other ways I could do this?

Why? Purpose for getting type information?

> Also, what is going on when DirectCast is called?

The type of the reference is changed to the destination type.

> Is the object
> being copied to somewhere else in memory

No

>  or is a different address
> in memory being used to retrieve the Type info?

The Object's Gettype method is used to get the type info. The interface does
not have a GetType method.

Armin
Simon Woods - 22 Oct 2007 18:16 GMT
>> Hi
>>
[quoted text clipped - 21 lines]
>
> Why? Purpose for getting type information?

I'm learning some design patterns and am translating some java code ...
It has been translated into c# okay, but vb doen't give you the GetType
off the interface.
Armin Zingler - 22 Oct 2007 18:51 GMT
> Armin Zingler wrote:
> > > Hi
[quoted text clipped - 27 lines]
> ... It has been translated into c# okay, but vb doen't give you the
> GetType off the interface.

Shared Function GetTheType(ByVal o As Object) As System.Type
  Return o.GetType
End Function    

Armin
Phill W. - 23 Oct 2007 17:23 GMT
> I have an object which is implementing a secondary interface. I want to
> get the name of the underlying object.

"Name" or "Type"?

> name = DirectCast(myInterfacedObject, Object).GetType.Name
>
> Are there better ways of doing this? Examples I've seen in C# seem to
> suggest that you can call the GetType off the Interface itself.

You /should/ be able to.
The only thing to watch out for is some "bright-spark" may have
Shadow'ed the GetType method and made it do something completely
different - stupid, I know, but it's the kind of thing Developers /love/
to do - "up-casting" to Object gets around this by using Object's most
basic implementation.

> Obviously I could put the Name property onto the interface, but I'm
> wondering if there are some other ways I could do this?

If you're after a Name property on a class that you know about, just
cast the reference to the required Type (after checking that you /can/,
of course):

If TypeOf myInterfacedObject Is NamedThing Then
   sName = DirectCast( myInterfacedObject, NamedThing ).Name
End If

> Also, what is going on when DirectCast is called? Is the object being
> copied to somewhere else in memory or is a different address in memory
> being used to retrieve the Type info?

Nope.  If you've ever worked with 'C++' or 'C#', you'll have come across
 the likes of:

int *  pointerToInteger = 0 ;
char *  pointerToChar = (char *)pointerToInteger ;

The second line takes a pointer to an integer value and blithely stuffs
it into a variable that's supposed to point to a character value.  Both
pointers refer to the same chunk of memory, but treat it as different
"types".

DirectCast does much the same thing.  It tells the compiler to treat the
given "object reference" (a.k.a. strongly-typed Pointer) as a reference
 to a /different/ Type.  However, it does go a little bit further and
checks the object's inheritance hierarchy and, if it can't make the cast
you've asked it to, it'll throw an Exception on you:

Class Class1
   Public Property Name
Class Class2 Inherits Class1
Class Class3 Inherits Class2
Class Class4

Dim c2 as New Class2
?DirectCast( c2, Class1 ).Name ' OK
?DirectCast( c2, Class4 ).Name ' Boom!
?DirectCast( c2, Class3 ).Name ' Boom!  Think about this one

HTH,
   Phill  W.
Simon Woods - 23 Oct 2007 19:57 GMT
>> I have an object which is implementing a secondary interface. I want
>> to get the name of the underlying object.
[quoted text clipped - 58 lines]
> HTH,
>    Phill  W.

That's very helpful ... thanks very much (to Armin as well)

Simon

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.